-
Notifications
You must be signed in to change notification settings - Fork 18
/
helpers.go
74 lines (61 loc) · 2.06 KB
/
helpers.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
package otel
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"github.com/circleci/ex/o11y"
)
type helpers struct{}
// ExtractPropagation pulls propagation information out of the context
func (h helpers) ExtractPropagation(ctx context.Context) o11y.PropagationContext {
m := map[string]string{}
otel.GetTextMapPropagator().Inject(ctx, mapCarrier(m))
return o11y.PropagationContext{
// TODO support single ca.Parent
Headers: m,
}
}
// InjectPropagation adds propagation header fields into the returned root span returning
// the context carrying that span. This should always return a span. If no propagation is
// found then a new span named root is returned. It is expected that callers of this will
// rename the returned span.
func (h helpers) InjectPropagation(ctx context.Context, ca o11y.PropagationContext) (context.Context, o11y.Span) {
p := o11y.FromContext(ctx)
// We need to be sure to get to the underlying raw otel provider if we want to wrap
// the otel spn
oteller, ok := p.(interface{ RawProvider() *OTel })
if !ok {
return p.StartSpan(ctx, "root")
}
op := oteller.RawProvider()
// TODO support single ca.Parent
ctx = otel.GetTextMapPropagator().Extract(ctx, mapCarrier(ca.Headers))
sp := trace.SpanFromContext(ctx)
if sp.SpanContext().IsValid() {
return ctx, op.wrapSpan(sp)
}
// If there was no context propagation make a span
return p.StartSpan(ctx, "root")
}
// TraceIDs return standard o11y ids
func (h helpers) TraceIDs(ctx context.Context) (traceID, parentID string) {
sc := trace.SpanFromContext(ctx).SpanContext()
return sc.TraceID().String(), "" // TODO - do we ever use parent
}
type mapCarrier map[string]string
// Get returns the value associated with the passed key.
func (m mapCarrier) Get(key string) string {
return m[key]
}
// Set stores the key-value pair.
func (m mapCarrier) Set(key string, value string) {
m[key] = value
}
// Keys lists the keys stored in this carrier.
func (m mapCarrier) Keys() []string {
ks := make([]string, 0, len(m))
for k := range m {
ks = append(ks, k)
}
return ks
}