-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.go
88 lines (77 loc) · 2.54 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package ctrl
import (
"strconv"
"time"
"github.com/metal-toolbox/rivets/v2/condition"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
var (
metricsNATSErrors *prometheus.CounterVec
metricsEventCounter *prometheus.CounterVec
metricsConditionRunTimeSummary *prometheus.SummaryVec
metricsNATSConnectTime *prometheus.SummaryVec
)
func init() {
metricsNATSErrors = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "nats_errors",
Help: "A count of errors while trying to use NATS.",
},
[]string{"operation"},
)
metricsEventCounter = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "events_received",
Help: "A counter metric to measure the total count of events received",
},
[]string{"valid", "response"}, // valid is true/false, response is ack/nack
)
metricsConditionRunTimeSummary = promauto.NewSummaryVec(
prometheus.SummaryOpts{
Name: "condition_duration_seconds",
Help: "A summary metric to measure the total time spent in completing each condition",
},
[]string{"condition", "state"},
)
metricsNATSConnectTime = promauto.NewSummaryVec(
prometheus.SummaryOpts{
Name: "nats_connection_time",
Help: "A summary metric to measure the time taken to connect and subscribe to the NATS Jetstream",
},
[]string{},
)
}
// spanEvent adds a span event along with the given attributes.
//
// event here is arbitrary and can be in the form of strings like - publishCondition, updateCondition etc
func spanEvent(span trace.Span, cond *condition.Condition, controllerID, event string) {
span.AddEvent(event, trace.WithAttributes(
attribute.String("controllerID", controllerID),
attribute.String("conditionID", cond.ID.String()),
attribute.String("conditionKind", string(cond.Kind)),
))
}
func metricsNATSError(op string) {
metricsNATSErrors.WithLabelValues(op).Inc()
}
func registerNATSConnectTimeMetric(startTS time.Time) {
metricsNATSConnectTime.With(prometheus.Labels{}).Observe(time.Since(startTS).Seconds())
}
func metricsEventsCounter(valid bool, response string) {
metricsEventCounter.With(
prometheus.Labels{
"valid": strconv.FormatBool(valid),
"response": response,
}).Inc()
}
func registerConditionRuntimeMetric(startTS time.Time, state string) {
metricsConditionRunTimeSummary.With(
prometheus.Labels{
"condition": string(condition.FirmwareInstall),
"state": state,
},
).Observe(time.Since(startTS).Seconds())
}