-
Notifications
You must be signed in to change notification settings - Fork 485
/
metrics.go
174 lines (140 loc) · 4.69 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package telemetry
import (
"context"
"errors"
"time"
"github.com/hashicorp/go-metrics"
"github.com/spiffe/spire/pkg/common/util"
)
const timerGranularity = time.Millisecond
// Label is a label/tag for a metric
type Label = metrics.Label
// Sink is an interface for emitting metrics
type Sink = metrics.MetricSink
// Metrics is an interface for all metrics plugins and services
type Metrics interface {
// A Gauge should retain the last value it is set to
SetGauge(key []string, val float32)
SetGaugeWithLabels(key []string, val float32, labels []Label)
// Should emit a Key/Value pair for each call
EmitKey(key []string, val float32)
// Counters should accumulate values
IncrCounter(key []string, val float32)
IncrCounterWithLabels(key []string, val float32, labels []Label)
// Samples are for timing information, where quantiles are used
AddSample(key []string, val float32)
AddSampleWithLabels(key []string, val float32, labels []Label)
// A convenience function for measuring elapsed time with a single line
MeasureSince(key []string, start time.Time)
MeasureSinceWithLabels(key []string, start time.Time, labels []Label)
}
type MetricsImpl struct {
*metrics.Metrics
c *MetricsConfig
runners []sinkRunner
// Each instance of metrics.Metrics in the slice corresponds to one metrics sink type
metricsSinks []*metrics.Metrics
}
var _ Metrics = (*MetricsImpl)(nil)
// NewMetrics returns a Metric implementation
func NewMetrics(c *MetricsConfig) (*MetricsImpl, error) {
if c.Logger == nil {
return nil, errors.New("logger must be configured")
}
impl := &MetricsImpl{c: c}
for _, f := range sinkRunnerFactories {
runner, err := f(c)
if err != nil {
return nil, err
}
if !runner.isConfigured() {
continue
}
fanout := metrics.FanoutSink{}
fanout = append(fanout, runner.sinks()...)
metricsPrefix := c.ServiceName
if c.FileConfig.MetricPrefix != "" {
metricsPrefix = c.FileConfig.MetricPrefix
}
conf := metrics.DefaultConfig(metricsPrefix)
conf.EnableHostname = false
if c.FileConfig.EnableHostnameLabel != nil {
conf.EnableHostnameLabel = *c.FileConfig.EnableHostnameLabel
} else {
conf.EnableHostnameLabel = true
}
conf.EnableTypePrefix = runner.requiresTypePrefix()
conf.AllowedLabels = c.FileConfig.AllowedLabels
conf.BlockedLabels = c.FileConfig.BlockedLabels
conf.AllowedPrefixes = c.FileConfig.AllowedPrefixes
conf.BlockedPrefixes = c.FileConfig.BlockedPrefixes
metricsSink, err := metrics.New(conf, fanout)
if err != nil {
return nil, err
}
impl.metricsSinks = append(impl.metricsSinks, metricsSink)
impl.runners = append(impl.runners, runner)
}
return impl, nil
}
// ListenAndServe starts the metrics process
func (m *MetricsImpl) ListenAndServe(ctx context.Context) error {
var tasks []func(context.Context) error
for _, runner := range m.runners {
tasks = append(tasks, runner.run)
}
return util.RunTasks(ctx, tasks...)
}
func (m *MetricsImpl) SetGauge(key []string, val float32) {
for _, s := range m.metricsSinks {
s.SetGauge(key, val)
}
}
// SetGaugeWithLabels delegates to embedded metrics, sanitizing labels
func (m *MetricsImpl) SetGaugeWithLabels(key []string, val float32, labels []Label) {
sanitizedLabels := SanitizeLabels(labels)
for _, s := range m.metricsSinks {
s.SetGaugeWithLabels(key, val, sanitizedLabels)
}
}
func (m *MetricsImpl) EmitKey(key []string, val float32) {
for _, s := range m.metricsSinks {
s.EmitKey(key, val)
}
}
func (m *MetricsImpl) IncrCounter(key []string, val float32) {
for _, s := range m.metricsSinks {
s.IncrCounter(key, val)
}
}
// IncrCounterWithLabels delegates to embedded metrics, sanitizing labels
func (m *MetricsImpl) IncrCounterWithLabels(key []string, val float32, labels []Label) {
sanitizedLabels := SanitizeLabels(labels)
for _, s := range m.metricsSinks {
s.IncrCounterWithLabels(key, val, sanitizedLabels)
}
}
func (m *MetricsImpl) AddSample(key []string, val float32) {
for _, s := range m.metricsSinks {
s.AddSample(key, val)
}
}
// AddSampleWithLabels delegates to embedded metrics, sanitizing labels
func (m *MetricsImpl) AddSampleWithLabels(key []string, val float32, labels []Label) {
sanitizedLabels := SanitizeLabels(labels)
for _, s := range m.metricsSinks {
s.AddSampleWithLabels(key, val, sanitizedLabels)
}
}
func (m *MetricsImpl) MeasureSince(key []string, start time.Time) {
for _, s := range m.metricsSinks {
s.MeasureSince(key, start)
}
}
// MeasureSinceWithLabels delegates to embedded metrics, sanitizing labels
func (m *MetricsImpl) MeasureSinceWithLabels(key []string, start time.Time, labels []Label) {
sanitizedLabels := SanitizeLabels(labels)
for _, s := range m.metricsSinks {
s.MeasureSinceWithLabels(key, start, sanitizedLabels)
}
}