metrics.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /* keep track of the number of calls (per bouncer) that lead to nil/non-nil responses.
  33. 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*/
  34. var LapiNilDecisions = prometheus.NewCounterVec(
  35. prometheus.CounterOpts{
  36. Name: "cs_lapi_decisions_ko_total",
  37. Help: "Number of calls to /decisions that returned nil result.",
  38. },
  39. []string{"bouncer"},
  40. )
  41. /*hits per bouncer*/
  42. var LapiNonNilDecisions = prometheus.NewCounterVec(
  43. prometheus.CounterOpts{
  44. Name: "cs_lapi_decisions_ok_total",
  45. Help: "Number of calls to /decisions that returned non-nil result.",
  46. },
  47. []string{"bouncer"},
  48. )
  49. var LapiResponseTime = prometheus.NewHistogramVec(
  50. prometheus.HistogramOpts{
  51. Name: "cs_lapi_request_duration_seconds",
  52. Help: "Response time of LAPI",
  53. 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},
  54. },
  55. []string{"endpoint", "method"})
  56. func PrometheusBouncersHasEmptyDecision(c *gin.Context) {
  57. name, ok := c.Get("BOUNCER_NAME")
  58. if ok {
  59. LapiNilDecisions.With(prometheus.Labels{
  60. "bouncer": name.(string)}).Inc()
  61. }
  62. }
  63. func PrometheusBouncersHasNonEmptyDecision(c *gin.Context) {
  64. name, ok := c.Get("BOUNCER_NAME")
  65. if ok {
  66. LapiNonNilDecisions.With(prometheus.Labels{
  67. "bouncer": name.(string)}).Inc()
  68. }
  69. }
  70. func PrometheusMachinesMiddleware() gin.HandlerFunc {
  71. return func(c *gin.Context) {
  72. claims := jwt.ExtractClaims(c)
  73. if claims != nil {
  74. if rawID, ok := claims["id"]; ok {
  75. machineID := rawID.(string)
  76. LapiMachineHits.With(prometheus.Labels{
  77. "machine": machineID,
  78. "route": c.Request.URL.Path,
  79. "method": c.Request.Method}).Inc()
  80. }
  81. }
  82. c.Next()
  83. }
  84. }
  85. func PrometheusBouncersMiddleware() gin.HandlerFunc {
  86. return func(c *gin.Context) {
  87. name, ok := c.Get("BOUNCER_NAME")
  88. if ok {
  89. LapiBouncerHits.With(prometheus.Labels{
  90. "bouncer": name.(string),
  91. "route": c.Request.URL.Path,
  92. "method": c.Request.Method}).Inc()
  93. }
  94. c.Next()
  95. }
  96. }
  97. func PrometheusMiddleware() gin.HandlerFunc {
  98. return func(c *gin.Context) {
  99. startTime := time.Now()
  100. LapiRouteHits.With(prometheus.Labels{
  101. "route": c.Request.URL.Path,
  102. "method": c.Request.Method}).Inc()
  103. c.Next()
  104. elapsed := time.Since(startTime)
  105. LapiResponseTime.With(prometheus.Labels{"method": c.Request.Method, "endpoint": c.Request.URL.Path}).Observe(elapsed.Seconds())
  106. }
  107. }