-
Notifications
You must be signed in to change notification settings - Fork 484
/
servicegraphs.go
445 lines (378 loc) · 15 KB
/
servicegraphs.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package servicegraphs
import (
"context"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/prometheus/util/strutil"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.25.0"
gen "github.com/grafana/tempo/modules/generator/processor"
"github.com/grafana/tempo/modules/generator/processor/servicegraphs/store"
processor_util "github.com/grafana/tempo/modules/generator/processor/util"
"github.com/grafana/tempo/modules/generator/registry"
"github.com/grafana/tempo/pkg/tempopb"
v1_common "github.com/grafana/tempo/pkg/tempopb/common/v1"
v1_trace "github.com/grafana/tempo/pkg/tempopb/trace/v1"
tempo_util "github.com/grafana/tempo/pkg/util"
)
var (
metricDroppedSpans = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "tempo",
Name: "metrics_generator_processor_service_graphs_dropped_spans",
Help: "Number of spans dropped when trying to add edges",
}, []string{"tenant"})
metricTotalEdges = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "tempo",
Name: "metrics_generator_processor_service_graphs_edges",
Help: "Total number of unique edges",
}, []string{"tenant"})
metricExpiredEdges = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "tempo",
Name: "metrics_generator_processor_service_graphs_expired_edges",
Help: "Number of edges that expired before finding its matching span",
}, []string{"tenant"})
)
const (
metricRequestTotal = "traces_service_graph_request_total"
metricRequestFailedTotal = "traces_service_graph_request_failed_total"
metricRequestServerSeconds = "traces_service_graph_request_server_seconds"
metricRequestClientSeconds = "traces_service_graph_request_client_seconds"
metricRequestMessagingSystemSeconds = "traces_service_graph_request_messaging_system_seconds"
)
const virtualNodeLabel = "virtual_node"
var defaultPeerAttributes = []attribute.Key{
semconv.PeerServiceKey, semconv.DBNameKey, semconv.DBSystemKey,
}
type tooManySpansError struct {
droppedSpans int
}
func (t *tooManySpansError) Error() string {
return fmt.Sprintf("dropped %d spans", t.droppedSpans)
}
type Processor struct {
Cfg Config
registry registry.Registry
labels []string
store store.Store
closeCh chan struct{}
serviceGraphRequestTotal registry.Counter
serviceGraphRequestFailedTotal registry.Counter
serviceGraphRequestServerSecondsHistogram registry.Histogram
serviceGraphRequestClientSecondsHistogram registry.Histogram
serviceGraphRequestMessagingSystemSecondsHistogram registry.Histogram
metricDroppedSpans prometheus.Counter
metricTotalEdges prometheus.Counter
metricExpiredEdges prometheus.Counter
logger log.Logger
}
func New(cfg Config, tenant string, registry registry.Registry, logger log.Logger) gen.Processor {
labels := []string{"client", "server", "connection_type"}
if cfg.EnableVirtualNodeLabel {
cfg.Dimensions = append(cfg.Dimensions, virtualNodeLabel)
}
for _, d := range cfg.Dimensions {
if cfg.EnableClientServerPrefix {
if cfg.EnableVirtualNodeLabel {
// leave the extra label for this feature as-is
if d == virtualNodeLabel {
labels = append(labels, strutil.SanitizeLabelName(d))
continue
}
}
labels = append(labels, strutil.SanitizeLabelName("client_"+d), strutil.SanitizeLabelName("server_"+d))
} else {
labels = append(labels, strutil.SanitizeLabelName(d))
}
}
p := &Processor{
Cfg: cfg,
registry: registry,
labels: labels,
closeCh: make(chan struct{}, 1),
serviceGraphRequestTotal: registry.NewCounter(metricRequestTotal),
serviceGraphRequestFailedTotal: registry.NewCounter(metricRequestFailedTotal),
serviceGraphRequestServerSecondsHistogram: registry.NewHistogram(metricRequestServerSeconds, cfg.HistogramBuckets),
serviceGraphRequestClientSecondsHistogram: registry.NewHistogram(metricRequestClientSeconds, cfg.HistogramBuckets),
serviceGraphRequestMessagingSystemSecondsHistogram: registry.NewHistogram(metricRequestMessagingSystemSeconds, cfg.HistogramBuckets),
metricDroppedSpans: metricDroppedSpans.WithLabelValues(tenant),
metricTotalEdges: metricTotalEdges.WithLabelValues(tenant),
metricExpiredEdges: metricExpiredEdges.WithLabelValues(tenant),
logger: log.With(logger, "component", "service-graphs"),
}
p.store = store.NewStore(cfg.Wait, cfg.MaxItems, p.onComplete, p.onExpire)
expirationTicker := time.NewTicker(2 * time.Second)
for i := 0; i < cfg.Workers; i++ {
go func() {
for {
select {
// Periodically clean expired edges from the store
case <-expirationTicker.C:
p.store.Expire()
case <-p.closeCh:
return
}
}
}()
}
return p
}
func (p *Processor) Name() string {
return Name
}
func (p *Processor) PushSpans(ctx context.Context, req *tempopb.PushSpansRequest) {
span, _ := opentracing.StartSpanFromContext(ctx, "servicegraphs.PushSpans")
defer span.Finish()
if err := p.consume(req.Batches); err != nil {
var tmsErr *tooManySpansError
if errors.As(err, &tmsErr) {
level.Warn(p.logger).Log("msg", "skipped processing of spans", "maxItems", p.Cfg.MaxItems, "err", err)
} else {
level.Error(p.logger).Log("msg", "failed consuming traces", "err", err)
}
}
}
func (p *Processor) consume(resourceSpans []*v1_trace.ResourceSpans) (err error) {
var (
isNew bool
totalDroppedSpans int
)
for _, rs := range resourceSpans {
svcName, ok := processor_util.FindServiceName(rs.Resource.Attributes)
if !ok {
continue
}
for _, ils := range rs.ScopeSpans {
for _, span := range ils.Spans {
connectionType := store.Unknown
spanMultiplier := processor_util.GetSpanMultiplier(p.Cfg.SpanMultiplierKey, span)
switch span.Kind {
case v1_trace.Span_SPAN_KIND_PRODUCER:
// override connection type and continue processing as span kind client
connectionType = store.MessagingSystem
fallthrough
case v1_trace.Span_SPAN_KIND_CLIENT:
key := buildKey(hex.EncodeToString(span.TraceId), hex.EncodeToString(span.SpanId))
isNew, err = p.store.UpsertEdge(key, func(e *store.Edge) {
e.TraceID = tempo_util.TraceIDToHexString(span.TraceId)
e.ConnectionType = connectionType
e.ClientService = svcName
e.ClientLatencySec = spanDurationSec(span)
e.ClientEndTimeUnixNano = span.EndTimeUnixNano
e.Failed = e.Failed || p.spanFailed(span)
p.upsertDimensions("client_", e.Dimensions, rs.Resource.Attributes, span.Attributes)
e.SpanMultiplier = spanMultiplier
p.upsertPeerNode(e, span.Attributes)
p.upsertDatabaseRequest(e, rs.Resource.Attributes, span)
})
case v1_trace.Span_SPAN_KIND_CONSUMER:
// override connection type and continue processing as span kind server
connectionType = store.MessagingSystem
fallthrough
case v1_trace.Span_SPAN_KIND_SERVER:
key := buildKey(hex.EncodeToString(span.TraceId), hex.EncodeToString(span.ParentSpanId))
isNew, err = p.store.UpsertEdge(key, func(e *store.Edge) {
e.TraceID = tempo_util.TraceIDToHexString(span.TraceId)
e.ConnectionType = connectionType
e.ServerService = svcName
e.ServerLatencySec = spanDurationSec(span)
e.ServerStartTimeUnixNano = span.StartTimeUnixNano
e.Failed = e.Failed || p.spanFailed(span)
p.upsertDimensions("server_", e.Dimensions, rs.Resource.Attributes, span.Attributes)
e.SpanMultiplier = spanMultiplier
p.upsertPeerNode(e, span.Attributes)
})
default:
// this span is not part of an edge
continue
}
if errors.Is(err, store.ErrTooManyItems) {
totalDroppedSpans++
p.metricDroppedSpans.Inc()
continue
}
// UpsertEdge will only return ErrTooManyItems
if err != nil {
return err
}
if isNew {
p.metricTotalEdges.Inc()
}
}
}
}
if totalDroppedSpans > 0 {
return &tooManySpansError{
droppedSpans: totalDroppedSpans,
}
}
return nil
}
func (p *Processor) upsertDimensions(prefix string, m map[string]string, resourceAttr, spanAttr []*v1_common.KeyValue) {
for _, dim := range p.Cfg.Dimensions {
if v, ok := processor_util.FindAttributeValue(dim, resourceAttr, spanAttr); ok {
if p.Cfg.EnableClientServerPrefix {
m[prefix+dim] = v
} else {
m[dim] = v
}
}
}
}
func (p *Processor) upsertPeerNode(e *store.Edge, spanAttr []*v1_common.KeyValue) {
for _, peerKey := range p.Cfg.PeerAttributes {
if v, ok := processor_util.FindAttributeValue(peerKey, spanAttr); ok {
e.PeerNode = v
return
}
}
}
// upsertDatabaseRequest handles the logic of adding a database edge on the
// graph. If we have a db.name or db.system attribute, we assume this is a
// database request. The name of the edge is determined by the following
// order:
//
// if we have a peer.service, use it as the database ServerService
// if we have a server.address, use it as the database ServerService
// if we have a network.peer.address, use it as the database ServerService. Include :port if network.peer.port is present
// if we have a db.name, use it as the database ServerService, which is the backwards-compatible behavior
func (p *Processor) upsertDatabaseRequest(e *store.Edge, resourceAttr []*v1_common.KeyValue, span *v1_trace.Span) {
var (
isDatabase bool
// The fallback database name
dbName string
)
// Check for db.name first. The dbName is set initially to maintain backwards compatbility.
if name, ok := processor_util.FindAttributeValue(string(semconv.DBNameKey), resourceAttr, span.Attributes); ok {
dbName = name
isDatabase = true
}
// Check for db.system only if we don't have db.name above
if !isDatabase {
if _, ok := processor_util.FindAttributeValue(string(semconv.DBSystemKey), resourceAttr, span.Attributes); ok {
isDatabase = true
}
}
// If neither db.system nor db.name are present, we can't determine if this is a database request
if !isDatabase {
return
}
e.ConnectionType = store.Database
e.ServerLatencySec = spanDurationSec(span)
// Set the service name by order of precedence
// Check for peer.service
if name, ok := processor_util.FindAttributeValue(string(semconv.PeerServiceKey), resourceAttr, span.Attributes); ok {
e.ServerService = name
return
}
// Check for server.address
if name, ok := processor_util.FindAttributeValue(string(semconv.ServerAddressKey), resourceAttr, span.Attributes); ok {
e.ServerService = name
return
}
// Check for network.peer.address and network.peer.port. Use port if it is present.
if host, ok := processor_util.FindAttributeValue(string(semconv.NetworkPeerAddressKey), resourceAttr, span.Attributes); ok {
if port, ok := processor_util.FindAttributeValue(string(semconv.NetworkPeerPortKey), resourceAttr, span.Attributes); ok {
e.ServerService = fmt.Sprintf("%s:%s", host, port)
return
}
e.ServerService = host
return
}
// Fallback to db.name
if dbName != "" {
e.ServerService = dbName
}
}
func (p *Processor) Shutdown(_ context.Context) {
close(p.closeCh)
}
func (p *Processor) onComplete(e *store.Edge) {
labelValues := make([]string, 0, 2+len(p.Cfg.Dimensions))
labelValues = append(labelValues, e.ClientService, e.ServerService, string(e.ConnectionType))
for _, dimension := range p.Cfg.Dimensions {
if p.Cfg.EnableClientServerPrefix {
if p.Cfg.EnableVirtualNodeLabel {
// leave the extra label for this feature as-is
if dimension == virtualNodeLabel {
labelValues = append(labelValues, e.Dimensions[dimension])
continue
}
}
labelValues = append(labelValues, e.Dimensions["client_"+dimension], e.Dimensions["server_"+dimension])
} else {
labelValues = append(labelValues, e.Dimensions[dimension])
}
}
labels := append([]string{}, p.labels...)
registryLabelValues := p.registry.NewLabelValueCombo(labels, labelValues)
p.serviceGraphRequestTotal.Inc(registryLabelValues, 1*e.SpanMultiplier)
if e.Failed {
p.serviceGraphRequestFailedTotal.Inc(registryLabelValues, 1*e.SpanMultiplier)
}
p.serviceGraphRequestServerSecondsHistogram.ObserveWithExemplar(registryLabelValues, e.ServerLatencySec, e.TraceID, e.SpanMultiplier)
p.serviceGraphRequestClientSecondsHistogram.ObserveWithExemplar(registryLabelValues, e.ClientLatencySec, e.TraceID, e.SpanMultiplier)
if p.Cfg.EnableMessagingSystemLatencyHistogram && e.ConnectionType == store.MessagingSystem {
messagingSystemLatencySec := unixNanosDiffSec(e.ClientEndTimeUnixNano, e.ServerStartTimeUnixNano)
if messagingSystemLatencySec == 0 {
level.Warn(p.logger).Log("msg", "producerSpanEndTime must be smaller than consumerSpanStartTime. maybe the peers clocks are not synced", "messagingSystemLatencySec", messagingSystemLatencySec, "traceID", e.TraceID)
} else {
p.serviceGraphRequestMessagingSystemSecondsHistogram.ObserveWithExemplar(registryLabelValues, messagingSystemLatencySec, e.TraceID, e.SpanMultiplier)
}
}
}
func (p *Processor) onExpire(e *store.Edge) {
p.metricExpiredEdges.Inc()
// If an edge is expired, we check if there are signs that the missing span is belongs to a "virtual node".
// These are nodes that are outside the user's reach (eg. an external service for payment processing),
// or that are not instrumented (eg. a frontend application).
e.ConnectionType = store.VirtualNode
if len(e.ClientService) == 0 {
// If the client service is not set, it means that the span could have been initiated by an external system,
// like a frontend application or an engineer via `curl`.
// We check if the span we have is the root span, and if so, we set the client service to "user".
if _, parentSpan := parseKey(e.Key()); len(parentSpan) == 0 {
e.ClientService = "user"
if p.Cfg.EnableVirtualNodeLabel {
e.Dimensions[virtualNodeLabel] = "client"
}
p.onComplete(e)
}
} else if len(e.ServerService) == 0 && len(e.PeerNode) > 0 {
// If client span does not have its matching server span, but has a peer attribute present,
// we make the assumption that a call was made to an external service, for which Tempo won't receive spans.
e.ServerService = e.PeerNode
if p.Cfg.EnableVirtualNodeLabel {
e.Dimensions[virtualNodeLabel] = "server"
}
p.onComplete(e)
}
}
func (p *Processor) spanFailed(span *v1_trace.Span) bool {
return span.GetStatus().GetCode() == v1_trace.Status_STATUS_CODE_ERROR
}
func unixNanosDiffSec(unixNanoStart uint64, unixNanoEnd uint64) float64 {
if unixNanoStart > unixNanoEnd {
// To prevent underflow, return 0.
return 0
}
// Safe subtraction.
return float64(unixNanoEnd-unixNanoStart) / float64(time.Second)
}
func spanDurationSec(span *v1_trace.Span) float64 {
return unixNanosDiffSec(span.StartTimeUnixNano, span.EndTimeUnixNano)
}
func buildKey(k1, k2 string) string {
return fmt.Sprintf("%s-%s", k1, k2)
}
func parseKey(key string) (string, string) {
parts := strings.Split(key, "-")
return parts[0], parts[1]
}