-
Notifications
You must be signed in to change notification settings - Fork 521
/
spanmetrics.go
288 lines (241 loc) · 8.98 KB
/
spanmetrics.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package spanmetrics
import (
"context"
"fmt"
"time"
"unicode/utf8"
"github.com/prometheus/prometheus/util/strutil"
"go.opentelemetry.io/otel"
gen "github.com/grafana/tempo/modules/generator/processor"
processor_util "github.com/grafana/tempo/modules/generator/processor/util"
"github.com/grafana/tempo/modules/generator/registry"
"github.com/grafana/tempo/pkg/spanfilter"
"github.com/grafana/tempo/pkg/tempopb"
v1 "github.com/grafana/tempo/pkg/tempopb/resource/v1"
v1_trace "github.com/grafana/tempo/pkg/tempopb/trace/v1"
tempo_util "github.com/grafana/tempo/pkg/util"
"github.com/prometheus/client_golang/prometheus"
)
const (
metricCallsTotal = "traces_spanmetrics_calls_total"
metricDurationSeconds = "traces_spanmetrics_latency"
metricSizeTotal = "traces_spanmetrics_size_total"
targetInfo = "traces_target_info"
)
var tracer = otel.Tracer("modules/generator/processor/spanmetrics")
type Processor struct {
Cfg Config
registry registry.Registry
spanMetricsCallsTotal registry.Counter
spanMetricsDurationSeconds registry.Histogram
spanMetricsSizeTotal registry.Counter
spanMetricsTargetInfo registry.Gauge
labels []string
filter *spanfilter.SpanFilter
filteredSpansCounter prometheus.Counter
invalidUTF8Counter prometheus.Counter
// for testing
now func() time.Time
}
func New(cfg Config, reg registry.Registry, filteredSpansCounter, invalidUTF8Counter prometheus.Counter) (gen.Processor, error) {
labels := make([]string, 0, 4+len(cfg.Dimensions))
if cfg.IntrinsicDimensions.Service {
labels = append(labels, dimService)
}
if cfg.IntrinsicDimensions.SpanName {
labels = append(labels, dimSpanName)
}
if cfg.IntrinsicDimensions.SpanKind {
labels = append(labels, dimSpanKind)
}
if cfg.IntrinsicDimensions.StatusCode {
labels = append(labels, dimStatusCode)
}
if cfg.IntrinsicDimensions.StatusMessage {
labels = append(labels, dimStatusMessage)
}
for _, d := range cfg.Dimensions {
labels = append(labels, sanitizeLabelNameWithCollisions(d))
}
for _, m := range cfg.DimensionMappings {
labels = append(labels, sanitizeLabelNameWithCollisions(m.Name))
}
err := validateLabelValues(labels)
if err != nil {
return nil, err
}
p := &Processor{
Cfg: cfg,
registry: reg,
spanMetricsTargetInfo: reg.NewGauge(targetInfo),
now: time.Now,
labels: labels,
filteredSpansCounter: filteredSpansCounter,
invalidUTF8Counter: invalidUTF8Counter,
}
if cfg.Subprocessors[Latency] {
p.spanMetricsDurationSeconds = reg.NewHistogram(metricDurationSeconds, cfg.HistogramBuckets, cfg.HistogramOverride)
}
if cfg.Subprocessors[Count] {
p.spanMetricsCallsTotal = reg.NewCounter(metricCallsTotal)
}
if cfg.Subprocessors[Size] {
p.spanMetricsSizeTotal = reg.NewCounter(metricSizeTotal)
}
filter, err := spanfilter.NewSpanFilter(cfg.FilterPolicies)
if err != nil {
return nil, err
}
p.filter = filter
return p, nil
}
func (p *Processor) Name() string {
return Name
}
func (p *Processor) PushSpans(ctx context.Context, req *tempopb.PushSpansRequest) {
_, span := tracer.Start(ctx, "spanmetrics.PushSpans")
defer span.End()
p.aggregateMetrics(req.Batches)
}
func (p *Processor) Shutdown(_ context.Context) {
}
func (p *Processor) aggregateMetrics(resourceSpans []*v1_trace.ResourceSpans) {
for _, rs := range resourceSpans {
// already extract job name & instance id, so we only have to do it once per batch of spans
svcName, _ := processor_util.FindServiceName(rs.Resource.Attributes)
jobName := processor_util.GetJobValue(rs.Resource.Attributes)
instanceID, _ := processor_util.FindInstanceID(rs.Resource.Attributes)
resourceLabels := make([]string, 0) // TODO move outside the loop and reuse?
resourceValues := make([]string, 0) // TODO don't allocate unless needed?
if p.Cfg.EnableTargetInfo {
resourceLabels, resourceValues = processor_util.GetTargetInfoAttributesValues(rs.Resource.Attributes, p.Cfg.TargetInfoExcludedDimensions)
}
for _, ils := range rs.ScopeSpans {
for _, span := range ils.Spans {
if p.filter.ApplyFilterPolicy(rs.Resource, span) {
p.aggregateMetricsForSpan(svcName, jobName, instanceID, rs.Resource, span, resourceLabels, resourceValues)
continue
}
p.filteredSpansCounter.Inc()
}
}
}
}
func (p *Processor) aggregateMetricsForSpan(svcName string, jobName string, instanceID string, rs *v1.Resource, span *v1_trace.Span, resourceLabels []string, resourceValues []string) {
// Spans with negative latency are treated as zero.
latencySeconds := 0.0
if start, end := span.GetStartTimeUnixNano(), span.GetEndTimeUnixNano(); start < end {
latencySeconds = float64(end-start) / float64(time.Second.Nanoseconds())
}
labelValues := make([]string, 0, 4+len(p.Cfg.Dimensions))
targetInfoLabelValues := make([]string, len(resourceLabels))
labels := make([]string, len(p.labels))
targetInfoLabels := make([]string, len(resourceLabels))
copy(labels, p.labels)
copy(targetInfoLabels, resourceLabels)
copy(targetInfoLabelValues, resourceValues)
// important: the order of labelValues must correspond to the order of labels / intrinsic dimensions
if p.Cfg.IntrinsicDimensions.Service {
labelValues = append(labelValues, svcName)
}
if p.Cfg.IntrinsicDimensions.SpanName {
labelValues = append(labelValues, span.GetName())
}
if p.Cfg.IntrinsicDimensions.SpanKind {
labelValues = append(labelValues, span.GetKind().String())
}
if p.Cfg.IntrinsicDimensions.StatusCode {
labelValues = append(labelValues, span.GetStatus().GetCode().String())
}
if p.Cfg.IntrinsicDimensions.StatusMessage {
labelValues = append(labelValues, span.GetStatus().GetMessage())
}
for _, d := range p.Cfg.Dimensions {
value, _ := processor_util.FindAttributeValue(d, rs.Attributes, span.Attributes)
labelValues = append(labelValues, value)
}
for _, m := range p.Cfg.DimensionMappings {
values := ""
for _, s := range m.SourceLabel {
if value, _ := processor_util.FindAttributeValue(s, rs.Attributes, span.Attributes); value != "" {
if values == "" {
values += value
} else {
values = values + m.Join + value
}
}
}
labelValues = append(labelValues, values)
}
// add job label only if job is not blank
if jobName != "" && p.Cfg.EnableTargetInfo {
labels = append(labels, dimJob)
labelValues = append(labelValues, jobName)
}
// add instance label only if job is not blank
if instanceID != "" && p.Cfg.EnableTargetInfo {
labels = append(labels, dimInstance)
labelValues = append(labelValues, instanceID)
}
spanMultiplier := processor_util.GetSpanMultiplier(p.Cfg.SpanMultiplierKey, span, rs)
err := validateLabelValues(labelValues)
if err != nil {
p.invalidUTF8Counter.Inc()
return
}
registryLabelValues := p.registry.NewLabelValueCombo(labels, labelValues)
if p.Cfg.Subprocessors[Count] {
p.spanMetricsCallsTotal.Inc(registryLabelValues, 1*spanMultiplier)
}
if p.Cfg.Subprocessors[Latency] {
p.spanMetricsDurationSeconds.ObserveWithExemplar(registryLabelValues, latencySeconds, tempo_util.TraceIDToHexString(span.TraceId), spanMultiplier)
}
if p.Cfg.Subprocessors[Size] {
p.spanMetricsSizeTotal.Inc(registryLabelValues, float64(span.Size()))
}
// update target_info label values
if p.Cfg.EnableTargetInfo {
// TODO - The resource labels only need to be sanitized once
// TODO - attribute names are stable across applications
// so let's cache the result of previous sanitizations
resourceAttributesCount := len(targetInfoLabels)
for index, label := range targetInfoLabels {
// sanitize label name
targetInfoLabels[index] = sanitizeLabelNameWithCollisions(label)
}
// add joblabel to target info only if job is not blank
if jobName != "" {
targetInfoLabels = append(targetInfoLabels, dimJob)
targetInfoLabelValues = append(targetInfoLabelValues, jobName)
}
// add instance label to target info only if job is not blank
if instanceID != "" {
targetInfoLabels = append(targetInfoLabels, dimInstance)
targetInfoLabelValues = append(targetInfoLabelValues, instanceID)
}
targetInfoRegistryLabelValues := p.registry.NewLabelValueCombo(targetInfoLabels, targetInfoLabelValues)
// only register target info if at least (job or instance) AND one other attribute are present
// TODO - We can move this check to the top
if resourceAttributesCount > 0 && len(targetInfoLabels) > resourceAttributesCount {
p.spanMetricsTargetInfo.SetForTargetInfo(targetInfoRegistryLabelValues, 1)
}
}
}
func sanitizeLabelNameWithCollisions(name string) string {
sanitized := strutil.SanitizeLabelName(name)
if isIntrinsicDimension(sanitized) {
return "__" + sanitized
}
return sanitized
}
func validateLabelValues(v []string) error {
for _, value := range v {
if !utf8.ValidString(value) {
return fmt.Errorf("invalid utf8 string: %s", value)
}
}
return nil
}
func isIntrinsicDimension(name string) bool {
return processor_util.Contains(name, []string{dimJob, dimSpanName, dimSpanKind, dimStatusCode, dimStatusMessage, dimInstance})
}