This repository was archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtop_level.go
62 lines (55 loc) · 1.76 KB
/
top_level.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
package model
const (
// TraceMetricsKey is a tag key which, if set to true,
// ensures all statistics are computed for this span.
// [FIXME] *not implemented yet*
TraceMetricsKey = "datadog.trace_metrics"
// This is a special metric, it's 1 if the span is top-level, 0 if not.
topLevelKey = "_top_level"
trueTagValue = "true"
)
// ComputeTopLevel updates all the spans top-level attribute.
//
// A span is considered top-level if:
// - it's a root span
// - its parent is unknown (other part of the code, distributed trace)
// - its parent belongs to another service (in that case it's a "local root"
// being the highest ancestor of other spans belonging to this service and
// attached to it).
func (t Trace) ComputeTopLevel() {
// build a lookup map
spanIDToIdx := make(map[uint64]int, len(t))
for i, span := range t {
spanIDToIdx[span.SpanID] = i
}
// iterate on each span and mark them as top-level if relevant
for i, span := range t {
if span.ParentID != 0 {
if parentIdx, ok := spanIDToIdx[span.ParentID]; ok && t[parentIdx].Service == span.Service {
continue
}
}
t[i].setTopLevel(true)
}
}
// setTopLevel sets the top-level attribute of the span.
func (s *Span) setTopLevel(topLevel bool) {
if !topLevel {
if s.Metrics == nil {
return
}
delete(s.Metrics, topLevelKey)
return
}
// Setting the metrics value, so that code downstream in the pipeline
// can identify this as top-level without recomputing everything.
s.SetMetric(topLevelKey, 1)
}
// TopLevel returns true if span is top-level.
func (s *Span) TopLevel() bool {
return s.Metrics[topLevelKey] == 1
}
// ForceMetrics returns true if statistics computation should be forced for this span.
func (s *Span) ForceMetrics() bool {
return s.Meta[TraceMetricsKey] == trueTagValue
}