metrics.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package v1
  2. import (
  3. jwt "github.com/appleboy/gin-jwt/v2"
  4. "github.com/gin-gonic/gin"
  5. "github.com/prometheus/client_golang/prometheus"
  6. )
  7. /*prometheus*/
  8. var LapiRouteHits = prometheus.NewCounterVec(
  9. prometheus.CounterOpts{
  10. Name: "cs_lapi_route_requests_total",
  11. Help: "Number of calls to each route per method.",
  12. },
  13. []string{"route", "method"},
  14. )
  15. /*hits per machine*/
  16. var LapiMachineHits = prometheus.NewCounterVec(
  17. prometheus.CounterOpts{
  18. Name: "cs_lapi_machine_requests_total",
  19. Help: "Number of calls to each route per method grouped by machines.",
  20. },
  21. []string{"machine", "route", "method"},
  22. )
  23. /*hits per bouncer*/
  24. var LapiBouncerHits = prometheus.NewCounterVec(
  25. prometheus.CounterOpts{
  26. Name: "cs_lapi_bouncer_requests_total",
  27. Help: "Number of calls to each route per method grouped by bouncers.",
  28. },
  29. []string{"bouncer", "route", "method"},
  30. )
  31. /* keep track of the number of calls (per bouncer) that lead to nil/non-nil responses.
  32. 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*/
  33. var LapiNilDecisions = prometheus.NewCounterVec(
  34. prometheus.CounterOpts{
  35. Name: "cs_lapi_decisions_ko_total",
  36. Help: "Number of calls to /decisions that returned nil result.",
  37. },
  38. []string{"bouncer"},
  39. )
  40. /*hits per bouncer*/
  41. var LapiNonNilDecisions = prometheus.NewCounterVec(
  42. prometheus.CounterOpts{
  43. Name: "cs_lapi_decisions_ok_total",
  44. Help: "Number of calls to /decisions that returned non-nil result.",
  45. },
  46. []string{"bouncer"},
  47. )
  48. func PrometheusBouncersHasEmptyDecision(c *gin.Context) {
  49. name, ok := c.Get("BOUNCER_NAME")
  50. if ok {
  51. LapiNilDecisions.With(prometheus.Labels{
  52. "bouncer": name.(string)}).Inc()
  53. }
  54. }
  55. func PrometheusBouncersHasNonEmptyDecision(c *gin.Context) {
  56. name, ok := c.Get("BOUNCER_NAME")
  57. if ok {
  58. LapiNonNilDecisions.With(prometheus.Labels{
  59. "bouncer": name.(string)}).Inc()
  60. }
  61. }
  62. func PrometheusMachinesMiddleware() gin.HandlerFunc {
  63. return func(c *gin.Context) {
  64. claims := jwt.ExtractClaims(c)
  65. if claims != nil {
  66. if rawID, ok := claims["id"]; ok {
  67. machineID := rawID.(string)
  68. LapiMachineHits.With(prometheus.Labels{
  69. "machine": machineID,
  70. "route": c.Request.URL.Path,
  71. "method": c.Request.Method}).Inc()
  72. }
  73. }
  74. c.Next()
  75. }
  76. }
  77. func PrometheusBouncersMiddleware() gin.HandlerFunc {
  78. return func(c *gin.Context) {
  79. name, ok := c.Get("BOUNCER_NAME")
  80. if ok {
  81. LapiBouncerHits.With(prometheus.Labels{
  82. "bouncer": name.(string),
  83. "route": c.Request.URL.Path,
  84. "method": c.Request.Method}).Inc()
  85. }
  86. c.Next()
  87. }
  88. }
  89. func PrometheusMiddleware() gin.HandlerFunc {
  90. return func(c *gin.Context) {
  91. LapiRouteHits.With(prometheus.Labels{
  92. "route": c.Request.URL.Path,
  93. "method": c.Request.Method}).Inc()
  94. c.Next()
  95. }
  96. }