...

Source file src/edge-infra.dev/pkg/sds/interlock/internal/observability/metrics.go

Documentation: edge-infra.dev/pkg/sds/interlock/internal/observability

     1  package observability
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"github.com/gin-gonic/gin"
     7  	"github.com/prometheus/client_golang/prometheus"
     8  	"github.com/prometheus/client_golang/prometheus/promauto"
     9  	"github.com/prometheus/client_golang/prometheus/promhttp"
    10  )
    11  
    12  var (
    13  	// Variables that are to be registered with prometheus. promauto
    14  	// automactically registers the variable with prometheus. If promauto is not
    15  	// used variable needs to be registered using prometheus.register.
    16  	Subscribers = promauto.NewGaugeVec(
    17  		prometheus.GaugeOpts{
    18  			Name: "edge_interlock_subscribers",
    19  			Help: "Current number of subscribers to Interlock API topics",
    20  		}, []string{})
    21  	EventsTotal = promauto.NewCounterVec(
    22  		prometheus.CounterOpts{
    23  			Name: "edge_interlock_events_total",
    24  			Help: "Total number of update events sent off to subscribers by the Interlock API",
    25  		}, []string{"topic"})
    26  	RequestsTotal = promauto.NewCounterVec(
    27  		prometheus.CounterOpts{
    28  			Name: "edge_interlock_http_requests_total",
    29  			Help: "Total number of requests made to the Interlock API",
    30  		}, []string{"handler", "method", "status"})
    31  	RequestDuration = promauto.NewHistogramVec(
    32  		prometheus.HistogramOpts{
    33  			Name:    "edge_interlock_request_duration_seconds",
    34  			Help:    "Latency/duration of requests made to the Interlock API",
    35  			Buckets: prometheus.LinearBuckets(0.0, 0.2, 10),
    36  		},
    37  		[]string{"handler", "method", "status"})
    38  )
    39  
    40  // MetricsEndpoint is the API endpoint where the prometheus metrics are served.
    41  var MetricsEndpoint = "/metrics"
    42  
    43  type Metrics struct{}
    44  
    45  // NewMetrics returns a new Metrics instance.
    46  func NewMetrics() *Metrics {
    47  	return &Metrics{}
    48  }
    49  
    50  // RegisterEndpoints registers the promhttp handler onto the router.
    51  func (m *Metrics) RegisterEndpoints(r *gin.Engine) {
    52  	r.GET(
    53  		MetricsEndpoint,
    54  		gin.WrapH(promhttp.Handler()),
    55  	)
    56  }
    57  
    58  // RecordSubscribers updates the Subscribers metric with the current number of
    59  // subscribers.
    60  func RecordSubscribers(count int) {
    61  	countFloat := float64(count)
    62  	Subscribers.WithLabelValues().Set(countFloat)
    63  }
    64  
    65  // RecordEvent increments the event counter for the provided topic.
    66  func RecordEvent(topic string) {
    67  	EventsTotal.WithLabelValues(topic).Inc()
    68  }
    69  
    70  // RecordRequest increments the request counter for the request total metric and
    71  // observes the latency for the request duration metric, both with the provided
    72  // handler, method and status.
    73  func RecordRequest(handler, method string, status int, latency float64) {
    74  	statusAsStr := strconv.Itoa(status)
    75  	labels := map[string]string{
    76  		"handler": handler,
    77  		"method":  method,
    78  		"status":  statusAsStr,
    79  	}
    80  	RequestsTotal.With(labels).Inc()
    81  	RequestDuration.With(labels).Observe(latency)
    82  }
    83  

View as plain text