bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/apiserver/controllers/v1/metrics.go (about)

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