File tree 3 files changed +32
-3
lines changed
opentelemetry-api/src/trace
3 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -44,6 +44,9 @@ export interface SpanOptions {
44
44
* A parent `SpanContext` (or `Span`, for convenience) that the newly-started
45
45
* span will be the child of. This overrides the parent span extracted from
46
46
* the currently active context.
47
+ *
48
+ * A null value here should prevent the SDK from extracting a parent from
49
+ * the current context, forcing the new span to be a root span.
47
50
*/
48
51
parent ?: Span | SpanContext | null ;
49
52
Original file line number Diff line number Diff line change @@ -66,9 +66,7 @@ export class Tracer implements api.Tracer {
66
66
options : api . SpanOptions = { } ,
67
67
context = api . context . active ( )
68
68
) : api . Span {
69
- const parentContext = options . parent
70
- ? getContext ( options . parent )
71
- : getParentSpanContext ( context ) ;
69
+ const parentContext = getParent ( options , context ) ;
72
70
// make sampling decision
73
71
const samplingDecision = this . _sampler . shouldSample ( parentContext ) ;
74
72
const spanId = randomSpanId ( ) ;
@@ -149,6 +147,19 @@ export class Tracer implements api.Tracer {
149
147
}
150
148
}
151
149
150
+ /**
151
+ * Get the parent to assign to a started span. If options.parent is null,
152
+ * do not assign a parent.
153
+ *
154
+ * @param options span options
155
+ * @param context context to check for parent
156
+ */
157
+ function getParent ( options : api . SpanOptions , context : api . Context ) {
158
+ if ( options . parent === null ) return undefined ;
159
+ if ( options . parent ) return getContext ( options . parent ) ;
160
+ return getParentSpanContext ( context ) ;
161
+ }
162
+
152
163
function getContext ( span : api . Span | api . SpanContext ) {
153
164
return isSpan ( span ) ? span . context ( ) : span ;
154
165
}
Original file line number Diff line number Diff line change @@ -239,6 +239,21 @@ describe('BasicTracerProvider', () => {
239
239
childSpan . end ( ) ;
240
240
} ) ;
241
241
242
+ it ( 'should create a root span when parent is null' , ( ) => {
243
+ const tracer = new BasicTracerProvider ( ) . getTracer ( 'default' ) ;
244
+ const span = tracer . startSpan ( 'my-span' ) ;
245
+ const overrideParent = tracer . startSpan ( 'my-parent-override-span' ) ;
246
+ const rootSpan = tracer . startSpan (
247
+ 'root-span' ,
248
+ { parent : null } ,
249
+ setActiveSpan ( Context . ROOT_CONTEXT , span )
250
+ ) ;
251
+ const context = rootSpan . context ( ) ;
252
+ assert . notStrictEqual ( context . traceId , overrideParent . context ( ) . traceId ) ;
253
+ span . end ( ) ;
254
+ rootSpan . end ( ) ;
255
+ } )
256
+
242
257
it ( 'should start a span with name and with invalid parent span' , ( ) => {
243
258
const tracer = new BasicTracerProvider ( ) . getTracer ( 'default' ) ;
244
259
const span = tracer . startSpan (
You can’t perform that action at this time.
0 commit comments