You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-11-23 22:34:47 +02:00
Add SpanContextFromContext() (#1255)
* SpanFromContext returns nil if span not exists * Add tests for SpanContextFromContext * Update CHANGELOG * Update trace.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * SpanFromContext() continue to return a noopSpan{} Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
10
trace.go
10
trace.go
@@ -205,7 +205,7 @@ func ContextWithSpan(parent context.Context, span Span) context.Context {
|
||||
return context.WithValue(parent, currentSpanKey, span)
|
||||
}
|
||||
|
||||
// SpanFromContext returns the current span from ctx, or nil if none set.
|
||||
// SpanFromContext returns the current span from ctx, or noop span if none set.
|
||||
func SpanFromContext(ctx context.Context) Span {
|
||||
if span, ok := ctx.Value(currentSpanKey).(Span); ok {
|
||||
return span
|
||||
@@ -213,6 +213,14 @@ func SpanFromContext(ctx context.Context) Span {
|
||||
return noopSpan{}
|
||||
}
|
||||
|
||||
// SpanContextFromContext returns the current SpanContext from ctx, or an empty SpanContext if none set.
|
||||
func SpanContextFromContext(ctx context.Context) SpanContext {
|
||||
if span := SpanFromContext(ctx); span != nil {
|
||||
return span.SpanContext()
|
||||
}
|
||||
return SpanContext{}
|
||||
}
|
||||
|
||||
// ContextWithRemoteSpanContext returns a copy of parent with a remote set as
|
||||
// the remote span context.
|
||||
func ContextWithRemoteSpanContext(parent context.Context, remote SpanContext) context.Context {
|
||||
|
||||
Reference in New Issue
Block a user