Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: start a root span with spanOptions.parent = null #889

Merged
merged 5 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/opentelemetry-api/src/trace/SpanOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ export interface SpanOptions {
* A parent `SpanContext` (or `Span`, for convenience) that the newly-started
* span will be the child of. This overrides the parent span extracted from
* the currently active context.
*
* A null value here should prevent the SDK from extracting a parent from
* the current context, forcing the new span to be a root span.
*/
parent?: Span | SpanContext | null;

Expand Down
20 changes: 17 additions & 3 deletions packages/opentelemetry-tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ export class Tracer implements api.Tracer {
options: api.SpanOptions = {},
context = api.context.active()
): api.Span {
const parentContext = options.parent
? getContext(options.parent)
: getParentSpanContext(context);
const parentContext = getParent(options, context);
// make sampling decision
const samplingDecision = this._sampler.shouldSample(parentContext);
const spanId = randomSpanId();
Expand Down Expand Up @@ -149,6 +147,22 @@ export class Tracer implements api.Tracer {
}
}

/**
* Get the parent to assign to a started span. If options.parent is null,
* do not assign a parent.
*
* @param options span options
* @param context context to check for parent
*/
function getParent(
options: api.SpanOptions,
context: api.Context
): api.SpanContext | undefined {
if (options.parent === null) return undefined;
if (options.parent) return getContext(options.parent);
return getParentSpanContext(context);
}

function getContext(span: api.Span | api.SpanContext) {
return isSpan(span) ? span.context() : span;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,21 @@ describe('BasicTracerProvider', () => {
childSpan.end();
});

it('should create a root span when parent is null', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span');
const overrideParent = tracer.startSpan('my-parent-override-span');
const rootSpan = tracer.startSpan(
'root-span',
{ parent: null },
setActiveSpan(Context.ROOT_CONTEXT, span)
);
const context = rootSpan.context();
assert.notStrictEqual(context.traceId, overrideParent.context().traceId);
span.end();
rootSpan.end();
});

it('should start a span with name and with invalid parent span', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan(
Expand Down