-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtelemetry.go
More file actions
31 lines (27 loc) · 947 Bytes
/
telemetry.go
File metadata and controls
31 lines (27 loc) · 947 Bytes
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
package copilot
import (
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
)
// getTraceContext extracts the current W3C Trace Context (traceparent/tracestate)
// from the Go context using the global OTel propagator.
func getTraceContext(ctx context.Context) (traceparent, tracestate string) {
carrier := propagation.MapCarrier{}
otel.GetTextMapPropagator().Inject(ctx, carrier)
return carrier.Get("traceparent"), carrier.Get("tracestate")
}
// contextWithTraceParent returns a new context with trace context extracted from
// the provided W3C traceparent and tracestate headers.
func contextWithTraceParent(ctx context.Context, traceparent, tracestate string) context.Context {
if traceparent == "" {
return ctx
}
carrier := propagation.MapCarrier{
"traceparent": traceparent,
}
if tracestate != "" {
carrier["tracestate"] = tracestate
}
return otel.GetTextMapPropagator().Extract(ctx, carrier)
}