forked from grafana/tempo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspanmetrics.go
More file actions
180 lines (148 loc) · 5.25 KB
/
spanmetrics.go
File metadata and controls
180 lines (148 loc) · 5.25 KB
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
package spanmetrics
import (
"context"
"time"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/prometheus/util/strutil"
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"
)
type Processor struct {
Cfg Config
registry registry.Registry
spanMetricsCallsTotal registry.Counter
spanMetricsDurationSeconds registry.Histogram
spanMetricsSizeTotal registry.Counter
filter *spanfilter.SpanFilter
filteredSpansCounter prometheus.Counter
// for testing
now func() time.Time
}
func New(cfg Config, registry registry.Registry, spanDiscardCounter 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))
}
p := &Processor{}
if cfg.Subprocessors[Latency] {
p.spanMetricsDurationSeconds = registry.NewHistogram(metricDurationSeconds, labels, cfg.HistogramBuckets)
}
if cfg.Subprocessors[Count] {
p.spanMetricsCallsTotal = registry.NewCounter(metricCallsTotal, labels)
}
if cfg.Subprocessors[Size] {
p.spanMetricsSizeTotal = registry.NewCounter(metricSizeTotal, labels)
}
filter, err := spanfilter.NewSpanFilter(cfg.FilterPolicies)
if err != nil {
return nil, err
}
p.Cfg = cfg
p.registry = registry
p.now = time.Now
p.filteredSpansCounter = spanDiscardCounter
p.filter = filter
return p, nil
}
func (p *Processor) Name() string {
return Name
}
func (p *Processor) PushSpans(ctx context.Context, req *tempopb.PushSpansRequest) {
span, _ := opentracing.StartSpanFromContext(ctx, "spanmetrics.PushSpans")
defer span.Finish()
p.aggregateMetrics(req.Batches)
}
func (p *Processor) Shutdown(_ context.Context) {
}
func (p *Processor) aggregateMetrics(resourceSpans []*v1_trace.ResourceSpans) {
for _, rs := range resourceSpans {
// already extract service name, so we only have to do it once per batch of spans
svcName, _ := processor_util.FindServiceName(rs.Resource.Attributes)
for _, ils := range rs.ScopeSpans {
for _, span := range ils.Spans {
if p.filter.ApplyFilterPolicy(rs.Resource, span) {
p.aggregateMetricsForSpan(svcName, rs.Resource, span)
continue
}
p.filteredSpansCounter.Inc()
}
}
}
}
func (p *Processor) aggregateMetricsForSpan(svcName string, rs *v1.Resource, span *v1_trace.Span) {
latencySeconds := float64(span.GetEndTimeUnixNano()-span.GetStartTimeUnixNano()) / float64(time.Second.Nanoseconds())
labelValues := make([]string, 0, 4+len(p.Cfg.Dimensions))
// 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)
}
spanMultiplier := processor_util.GetSpanMultiplier(p.Cfg.SpanMultiplierKey, span)
registryLabelValues := p.registry.NewLabelValues(labelValues)
if p.Cfg.Subprocessors[Count] {
p.spanMetricsCallsTotal.Inc(registryLabelValues, 1*spanMultiplier)
}
p.spanMetricsSizeTotal.Inc(registryLabelValues, float64(span.Size())*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()))
}
}
func sanitizeLabelNameWithCollisions(name string) string {
sanitized := strutil.SanitizeLabelName(name)
if isIntrinsicDimension(sanitized) {
return "__" + sanitized
}
return sanitized
}
func isIntrinsicDimension(name string) bool {
return name == dimService ||
name == dimSpanName ||
name == dimSpanKind ||
name == dimStatusCode ||
name == dimStatusMessage
}