@@ -18,6 +18,11 @@ func ContextWithSpan(ctx context.Context, s *Span) context.Context {
18
18
return orchestrion .CtxWithValue (ctx , internal .ActiveSpanKey , s )
19
19
}
20
20
21
+ // contextWithSpanContext returns a copy of the given context which includes the span context sctx.
22
+ func contextWithSpanContext (ctx context.Context , sctx * SpanContext ) context.Context {
23
+ return orchestrion .CtxWithValue (ctx , internal .ActiveSpanContextKey , sctx )
24
+ }
25
+
21
26
// SpanFromContext returns the span contained in the given context. A second return
22
27
// value indicates if a span was found in the context. If no span is found, a no-op
23
28
// span is returned.
@@ -35,6 +40,19 @@ func SpanFromContext(ctx context.Context) (*Span, bool) {
35
40
return nil , false
36
41
}
37
42
43
+ // spanContextFromContext returns the span context contained in the given context. A second return
44
+ // value indicates if a span context was found in the context. If no span context is found, a nil is returned.
45
+ func spanContextFromContext (ctx context.Context ) (* SpanContext , bool ) {
46
+ if ctx == nil {
47
+ return nil , false
48
+ }
49
+ v := orchestrion .WrapContext (ctx ).Value (internal .ActiveSpanContextKey )
50
+ if sctx , ok := v .(* SpanContext ); ok {
51
+ return sctx , true
52
+ }
53
+ return nil , false
54
+ }
55
+
38
56
// StartSpanFromContext returns a new span with the given operation name and options. If a span
39
57
// is found in the context, it will be used as the parent of the resulting span. If the ChildOf
40
58
// option is passed, it will only be used as the parent if there is no span found in `ctx`.
@@ -47,6 +65,8 @@ func StartSpanFromContext(ctx context.Context, operationName string, opts ...Sta
47
65
ctx = context .Background ()
48
66
} else if s , ok := SpanFromContext (ctx ); ok {
49
67
optsLocal = append (optsLocal , ChildOf (s .Context ()))
68
+ } else if sctx , ok := spanContextFromContext (ctx ); ok {
69
+ optsLocal = append (optsLocal , ChildOf (sctx ))
50
70
}
51
71
optsLocal = append (optsLocal , withContext (ctx ))
52
72
s := StartSpan (operationName , optsLocal ... )
0 commit comments