-
Notifications
You must be signed in to change notification settings - Fork 468
/
plugin_metrics.go
109 lines (91 loc) · 3.22 KB
/
plugin_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package metricsservice
import (
"context"
"time"
"github.com/hashicorp/go-hclog"
"github.com/spiffe/spire/pkg/common/plugin/hostservices"
"github.com/spiffe/spire/pkg/common/telemetry"
)
type pluginMetrics struct {
m hostservices.MetricsService
log hclog.Logger
fixedLabels []*hostservices.Label
}
// WrapPluginMetrics returns a Metrics implementation that wraps the Metrics Host Service
// Additionally, labels common to the module can also be set, and will be added to all
// resulting metrics calls.
// This enables usage of common functionality related to the Metrics interface from a plugin.
// Any errors are logged, but not returned.
func WrapPluginMetrics(m hostservices.MetricsService, log hclog.Logger, labels ...telemetry.Label) telemetry.Metrics {
return pluginMetrics{
m: m,
log: log,
fixedLabels: convertToRPCLabels(labels),
}
}
// A Gauge should retain the last value it is set to
func (p pluginMetrics) SetGauge(key []string, val float32) {
p.SetGaugeWithLabels(key, val, nil)
}
func (p pluginMetrics) SetGaugeWithLabels(key []string, val float32, labels []telemetry.Label) {
_, err := p.m.SetGauge(context.Background(), &hostservices.SetGaugeRequest{
Key: key,
Val: val,
Labels: append(convertToRPCLabels(labels), p.fixedLabels...),
})
if err != nil {
p.log.Error("error with metrics", telemetry.Error, err)
}
}
// Should emit a Key/Value pair for each call
func (p pluginMetrics) EmitKey(key []string, val float32) {
_, err := p.m.EmitKey(context.Background(), &hostservices.EmitKeyRequest{
Key: key,
Val: val,
})
if err != nil {
p.log.Error("error with metrics", telemetry.Error, err)
}
}
// Counters should accumulate values
func (p pluginMetrics) IncrCounter(key []string, val float32) {
p.IncrCounterWithLabels(key, val, nil)
}
func (p pluginMetrics) IncrCounterWithLabels(key []string, val float32, labels []telemetry.Label) {
_, err := p.m.IncrCounter(context.Background(), &hostservices.IncrCounterRequest{
Key: key,
Val: val,
Labels: append(convertToRPCLabels(labels), p.fixedLabels...),
})
if err != nil {
p.log.Error("error with metrics", telemetry.Error, err)
}
}
// Samples are for timing information, where quantiles are used
func (p pluginMetrics) AddSample(key []string, val float32) {
p.AddSampleWithLabels(key, val, nil)
}
func (p pluginMetrics) AddSampleWithLabels(key []string, val float32, labels []telemetry.Label) {
_, err := p.m.AddSample(context.Background(), &hostservices.AddSampleRequest{
Key: key,
Val: val,
Labels: append(convertToRPCLabels(labels), p.fixedLabels...),
})
if err != nil {
p.log.Error("error with metrics", telemetry.Error, err)
}
}
// A convenience function for measuring elapsed time with a single line
func (p pluginMetrics) MeasureSince(key []string, start time.Time) {
p.MeasureSinceWithLabels(key, start, nil)
}
func (p pluginMetrics) MeasureSinceWithLabels(key []string, start time.Time, labels []telemetry.Label) {
_, err := p.m.MeasureSince(context.Background(), &hostservices.MeasureSinceRequest{
Key: key,
Time: start.UnixNano(),
Labels: append(convertToRPCLabels(labels), p.fixedLabels...),
})
if err != nil {
p.log.Error("error with metrics", telemetry.Error, err)
}
}