-
Notifications
You must be signed in to change notification settings - Fork 18
/
helpers.go
46 lines (37 loc) · 1.46 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
package otel
import (
"context"
"net/http"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"github.com/circleci/ex/o11y"
)
type helpers struct {
p OTel
}
// ExtractPropagation pulls propagation information out of the context
func (h helpers) ExtractPropagation(ctx context.Context) o11y.PropagationContext {
m := http.Header{}
otel.GetTextMapPropagator().Inject(ctx, propagation.HeaderCarrier(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) {
// TODO support single ca.Parent
ctx = otel.GetTextMapPropagator().Extract(ctx, propagation.HeaderCarrier(ca.Headers))
// Make a new span - the trace propagation info in the context will be used
// N.B we update the name of this span at the calling site.
return h.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
}