github.com/crowdsecurity/crowdsec@v1.6.1/pkg/apiserver/controllers/v1/metrics.go (about)

     1  package v1
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/gin-gonic/gin"
     7  	"github.com/prometheus/client_golang/prometheus"
     8  )
     9  
    10  /*prometheus*/
    11  var LapiRouteHits = prometheus.NewCounterVec(
    12  	prometheus.CounterOpts{
    13  		Name: "cs_lapi_route_requests_total",
    14  		Help: "Number of calls to each route per method.",
    15  	},
    16  	[]string{"route", "method"},
    17  )
    18  
    19  /*hits per machine*/
    20  var LapiMachineHits = prometheus.NewCounterVec(
    21  	prometheus.CounterOpts{
    22  		Name: "cs_lapi_machine_requests_total",
    23  		Help: "Number of calls to each route per method grouped by machines.",
    24  	},
    25  	[]string{"machine", "route", "method"},
    26  )
    27  
    28  /*hits per bouncer*/
    29  var LapiBouncerHits = prometheus.NewCounterVec(
    30  	prometheus.CounterOpts{
    31  		Name: "cs_lapi_bouncer_requests_total",
    32  		Help: "Number of calls to each route per method grouped by bouncers.",
    33  	},
    34  	[]string{"bouncer", "route", "method"},
    35  )
    36  
    37  /*
    38  	keep track of the number of calls (per bouncer) that lead to nil/non-nil responses.
    39  
    40  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
    41  */
    42  var LapiNilDecisions = prometheus.NewCounterVec(
    43  	prometheus.CounterOpts{
    44  		Name: "cs_lapi_decisions_ko_total",
    45  		Help: "Number of calls to /decisions that returned nil result.",
    46  	},
    47  	[]string{"bouncer"},
    48  )
    49  
    50  /*hits per bouncer*/
    51  var LapiNonNilDecisions = prometheus.NewCounterVec(
    52  	prometheus.CounterOpts{
    53  		Name: "cs_lapi_decisions_ok_total",
    54  		Help: "Number of calls to /decisions that returned non-nil result.",
    55  	},
    56  	[]string{"bouncer"},
    57  )
    58  
    59  var LapiResponseTime = prometheus.NewHistogramVec(
    60  	prometheus.HistogramOpts{
    61  		Name:    "cs_lapi_request_duration_seconds",
    62  		Help:    "Response time of LAPI",
    63  		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},
    64  	},
    65  	[]string{"endpoint", "method"})
    66  
    67  func PrometheusBouncersHasEmptyDecision(c *gin.Context) {
    68  	bouncer, _ := getBouncerFromContext(c)
    69  	if bouncer != nil {
    70  		LapiNilDecisions.With(prometheus.Labels{
    71  			"bouncer": bouncer.Name}).Inc()
    72  	}
    73  }
    74  
    75  func PrometheusBouncersHasNonEmptyDecision(c *gin.Context) {
    76  	bouncer, _ := getBouncerFromContext(c)
    77  	if bouncer != nil {
    78  		LapiNonNilDecisions.With(prometheus.Labels{
    79  			"bouncer": bouncer.Name}).Inc()
    80  	}
    81  }
    82  
    83  func PrometheusMachinesMiddleware() gin.HandlerFunc {
    84  	return func(c *gin.Context) {
    85  		machineID, _ := getMachineIDFromContext(c)
    86  		if machineID != "" {
    87  			LapiMachineHits.With(prometheus.Labels{
    88  				"machine": machineID,
    89  				"route":   c.Request.URL.Path,
    90  				"method":  c.Request.Method}).Inc()
    91  		}
    92  
    93  		c.Next()
    94  	}
    95  }
    96  
    97  func PrometheusBouncersMiddleware() gin.HandlerFunc {
    98  	return func(c *gin.Context) {
    99  		bouncer, _ := getBouncerFromContext(c)
   100  		if bouncer != nil {
   101  			LapiBouncerHits.With(prometheus.Labels{
   102  				"bouncer": bouncer.Name,
   103  				"route":   c.Request.URL.Path,
   104  				"method":  c.Request.Method}).Inc()
   105  		}
   106  
   107  		c.Next()
   108  	}
   109  }
   110  
   111  func PrometheusMiddleware() gin.HandlerFunc {
   112  	return func(c *gin.Context) {
   113  		startTime := time.Now()
   114  
   115  		LapiRouteHits.With(prometheus.Labels{
   116  			"route":  c.Request.URL.Path,
   117  			"method": c.Request.Method}).Inc()
   118  		c.Next()
   119  
   120  		elapsed := time.Since(startTime)
   121  		LapiResponseTime.With(prometheus.Labels{"method": c.Request.Method, "endpoint": c.Request.URL.Path}).Observe(elapsed.Seconds())
   122  	}
   123  }