...
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
14
15
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
41 var MetricsEndpoint = "/metrics"
42
43 type Metrics struct{}
44
45
46 func NewMetrics() *Metrics {
47 return &Metrics{}
48 }
49
50
51 func (m *Metrics) RegisterEndpoints(r *gin.Engine) {
52 r.GET(
53 MetricsEndpoint,
54 gin.WrapH(promhttp.Handler()),
55 )
56 }
57
58
59
60 func RecordSubscribers(count int) {
61 countFloat := float64(count)
62 Subscribers.WithLabelValues().Set(countFloat)
63 }
64
65
66 func RecordEvent(topic string) {
67 EventsTotal.WithLabelValues(topic).Inc()
68 }
69
70
71
72
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