Skip to content

Commit 99d2704

Browse files
committed
Add WithTraceContext as TraceContextFromContext's companion
1 parent 38ccc41 commit 99d2704

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

telemetry/telemetry.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,23 @@ func Tracer(service string, component string, opts ...trace.TracerOption) trace.
5656
return otel.Tracer(name, opts...)
5757
}
5858

59+
// TraceContextFromContext returns the tracecontext present in the passed
60+
// context, if any.
5961
func TraceContextFromContext(ctx context.Context) propagation.MapCarrier {
6062
c := propagation.MapCarrier{}
6163
propagator := otel.GetTextMapPropagator()
6264
propagator.Inject(ctx, c)
6365
return c
6466
}
6567

68+
// WithTraceContext adds the tracecontext from the provided carrier to a
69+
// returned Context. If no valid tracecontext is contained in the carrier, the
70+
// passed ctx will be returned directly.
71+
func WithTraceContext(ctx context.Context, carrier propagation.TextMapCarrier) context.Context {
72+
propagator := otel.GetTextMapPropagator()
73+
return propagator.Extract(ctx, carrier)
74+
}
75+
6676
func createTracerProvider(ctx context.Context) (*sdktrace.TracerProvider, error) {
6777
// The exporter uses the OTEL_EXPORTER_OTLP_ENDPOINT and
6878
// OTEL_EXPORTER_OTLP_HEADERS environment variables.

0 commit comments

Comments
 (0)