metrics.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package v1
  2. import (
  3. "time"
  4. jwt "github.com/appleboy/gin-jwt/v2"
  5. "github.com/gin-gonic/gin"
  6. "github.com/prometheus/client_golang/prometheus"
  7. )
  8. /*prometheus*/
  9. var LapiRouteHits = prometheus.NewCounterVec(
  10. prometheus.CounterOpts{
  11. Name: "cs_lapi_route_requests_total",
  12. Help: "Number of calls to each route per method.",
  13. },
  14. []string{"route", "method"},
  15. )
  16. /*hits per machine*/
  17. var LapiMachineHits = prometheus.NewCounterVec(
  18. prometheus.CounterOpts{
  19. Name: "cs_lapi_machine_requests_total",
  20. Help: "Number of calls to each route per method grouped by machines.",
  21. },
  22. []string{"machine", "route", "method"},
  23. )
  24. /*hits per bouncer*/
  25. var LapiBouncerHits = prometheus.NewCounterVec(
  26. prometheus.CounterOpts{
  27. Name: "cs_lapi_bouncer_requests_total",
  28. Help: "Number of calls to each route per method grouped by bouncers.",
  29. },
  30. []string{"bouncer", "route", "method"},
  31. )
  32. /*
  33. keep track of the number of calls (per bouncer) that lead to nil/non-nil responses.
  34. while it's not exact, it's a good way to know - when you have a rutpure bouncer - what is the rate of ok/ko answers you got from lapi
  35. */
  36. var LapiNilDecisions = prometheus.NewCounterVec(
  37. prometheus.CounterOpts{
  38. Name: "cs_lapi_decisions_ko_total",
  39. Help: "Number of calls to /decisions that returned nil result.",
  40. },
  41. []string{"bouncer"},
  42. )
  43. /*hits per bouncer*/
  44. var LapiNonNilDecisions = prometheus.NewCounterVec(
  45. prometheus.CounterOpts{
  46. Name: "cs_lapi_decisions_ok_total",
  47. Help: "Number of calls to /decisions that returned non-nil result.",
  48. },
  49. []string{"bouncer"},
  50. )
  51. var LapiResponseTime = prometheus.NewHistogramVec(
  52. prometheus.HistogramOpts{
  53. Name: "cs_lapi_request_duration_seconds",
  54. Help: "Response time of LAPI",
  55. Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.2, 0.3, 0.4, 0.5, 0.75, 1},
  56. },
  57. []string{"endpoint", "method"})
  58. func PrometheusBouncersHasEmptyDecision(c *gin.Context) {
  59. name, ok := c.Get("BOUNCER_NAME")
  60. if ok {
  61. LapiNilDecisions.With(prometheus.Labels{
  62. "bouncer": name.(string)}).Inc()
  63. }
  64. }
  65. func PrometheusBouncersHasNonEmptyDecision(c *gin.Context) {
  66. name, ok := c.Get("BOUNCER_NAME")
  67. if ok {
  68. LapiNonNilDecisions.With(prometheus.Labels{
  69. "bouncer": name.(string)}).Inc()
  70. }
  71. }
  72. func PrometheusMachinesMiddleware() gin.HandlerFunc {
  73. return func(c *gin.Context) {
  74. claims := jwt.ExtractClaims(c)
  75. if claims != nil {
  76. if rawID, ok := claims["id"]; ok {
  77. machineID := rawID.(string)
  78. LapiMachineHits.With(prometheus.Labels{
  79. "machine": machineID,
  80. "route": c.Request.URL.Path,
  81. "method": c.Request.Method}).Inc()
  82. }
  83. }
  84. c.Next()
  85. }
  86. }
  87. func PrometheusBouncersMiddleware() gin.HandlerFunc {
  88. return func(c *gin.Context) {
  89. name, ok := c.Get("BOUNCER_NAME")
  90. if ok {
  91. LapiBouncerHits.With(prometheus.Labels{
  92. "bouncer": name.(string),
  93. "route": c.Request.URL.Path,
  94. "method": c.Request.Method}).Inc()
  95. }
  96. c.Next()
  97. }
  98. }
  99. func PrometheusMiddleware() gin.HandlerFunc {
  100. return func(c *gin.Context) {
  101. startTime := time.Now()
  102. LapiRouteHits.With(prometheus.Labels{
  103. "route": c.Request.URL.Path,
  104. "method": c.Request.Method}).Inc()
  105. c.Next()
  106. elapsed := time.Since(startTime)
  107. LapiResponseTime.With(prometheus.Labels{"method": c.Request.Method, "endpoint": c.Request.URL.Path}).Observe(elapsed.Seconds())
  108. }
  109. }