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

chore: remove explicit parent option #1612

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 7 additions & 4 deletions packages/opentelemetry-api/src/trace/NoopTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ export class NoopTracer implements Tracer {

// startSpan starts a noop span.
startSpan(name: string, options?: SpanOptions, context?: Context): Span {
const parent = options?.parent;
const root = Boolean(options?.root);
if (root) {
return NOOP_SPAN;
}

const parentFromContext = context && getActiveSpan(context)?.context();
if (isSpanContext(parent) && isSpanContextValid(parent)) {
return new NoopSpan(parent);
} else if (

if (
isSpanContext(parentFromContext) &&
isSpanContextValid(parentFromContext)
) {
Expand Down
19 changes: 3 additions & 16 deletions packages/opentelemetry-api/src/trace/SpanOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import { Attributes } from './attributes';
import { Link } from './link';
import { SpanKind } from './span_kind';
import { Span } from './span';
import { SpanContext } from './span_context';

/**
* Options needed for span creation
Expand All @@ -36,20 +34,9 @@ export interface SpanOptions {
/** {@link Link}s span to other spans */
links?: Link[];

/**
* This option is NOT RECOMMENDED for normal use and should ONLY be used
* if your application manages context manually without the global context
* manager, or you are trying to override the parent extracted from context.
*
* 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;

/** A manually specified start time for the created `Span` object. */
startTime?: number;

/** The new span should be a root span. (Ignore parent from context). */
root?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,6 @@ describe('NoopTracer', () => {
return patchedFn();
});

it('should propagate valid spanContext on the span (from parent)', () => {
dyladan marked this conversation as resolved.
Show resolved Hide resolved
const tracer = new NoopTracer();
const parent: SpanContext = {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.NONE,
};
const span = tracer.startSpan('test-1', { parent });
assert(span.context().traceId === parent.traceId);
assert(span.context().spanId === parent.spanId);
assert(span.context().traceFlags === parent.traceFlags);
});

it('should propagate valid spanContext on the span (from context)', () => {
const tracer = new NoopTracer();
const parent: SpanContext = {
Expand Down
37 changes: 23 additions & 14 deletions packages/opentelemetry-node/test/NodeTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
* limitations under the License.
*/

import { context, TraceFlags, setActiveSpan } from '@opentelemetry/api';
import {
context,
TraceFlags,
setActiveSpan,
setExtractedSpanContext,
} from '@opentelemetry/api';
import {
AlwaysOnSampler,
AlwaysOffSampler,
Expand All @@ -26,7 +31,7 @@ import { Span } from '@opentelemetry/tracing';
import { Resource, TELEMETRY_SDK_RESOURCE } from '@opentelemetry/resources';
import * as assert from 'assert';
import * as path from 'path';
import { ContextManager } from '@opentelemetry/context-base';
import { ContextManager, ROOT_CONTEXT } from '@opentelemetry/context-base';
import { NodeTracerProvider } from '../src/NodeTracerProvider';

const sleep = (time: number) =>
Expand Down Expand Up @@ -160,25 +165,29 @@ describe('NodeTracerProvider', () => {
logger: new NoopLogger(),
});

const sampledParent = provider
.getTracer('default')
.startSpan('not-sampled-span', {
parent: {
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.NONE,
},
});
const sampledParent = provider.getTracer('default').startSpan(
'not-sampled-span',
{},
setExtractedSpanContext(ROOT_CONTEXT, {
obecny marked this conversation as resolved.
Show resolved Hide resolved
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
spanId: '6e0c63257de34c92',
traceFlags: TraceFlags.NONE,
})
);
assert.ok(sampledParent instanceof Span);
assert.strictEqual(
sampledParent.context().traceFlags,
TraceFlags.SAMPLED
);
assert.strictEqual(sampledParent.isRecording(), true);

const span = provider.getTracer('default').startSpan('child-span', {
parent: sampledParent,
});
const span = provider
.getTracer('default')
.startSpan(
'child-span',
{},
setActiveSpan(ROOT_CONTEXT, sampledParent)
obecny marked this conversation as resolved.
Show resolved Hide resolved
);
assert.ok(span instanceof Span);
assert.strictEqual(span.context().traceFlags, TraceFlags.SAMPLED);
assert.strictEqual(span.isRecording(), true);
Expand Down
12 changes: 8 additions & 4 deletions packages/opentelemetry-plugin-fetch/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import * as web from '@opentelemetry/web';
import { AttributeNames } from './enums/AttributeNames';
import { FetchError, FetchResponse, SpanData } from './types';
import { VERSION } from './version';
import { setActiveSpan } from '@opentelemetry/api';

// how long to wait for observer to collect information about resources
// this is needed as event "load" is called before observer
Expand Down Expand Up @@ -63,10 +64,13 @@ export class FetchPlugin extends core.BasePlugin<Promise<Response>> {
span: api.Span,
corsPreFlightRequest: PerformanceResourceTiming
): void {
const childSpan = this._tracer.startSpan('CORS Preflight', {
parent: span,
startTime: corsPreFlightRequest[web.PerformanceTimingNames.FETCH_START],
});
const childSpan = this._tracer.startSpan(
'CORS Preflight',
{
startTime: corsPreFlightRequest[web.PerformanceTimingNames.FETCH_START],
},
setActiveSpan(api.context.active(), span)
obecny marked this conversation as resolved.
Show resolved Hide resolved
);
web.addSpanNetworkEvents(childSpan, corsPreFlightRequest);
childSpan.end(
corsPreFlightRequest[web.PerformanceTimingNames.RESPONSE_END]
Expand Down
11 changes: 1 addition & 10 deletions packages/opentelemetry-tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,6 @@ function getParent(
options: api.SpanOptions,
context: api.Context
): api.SpanContext | undefined {
if (options.parent === null) return undefined;
if (options.parent) return getContext(options.parent);
if (options.root) return undefined;
return api.getParentSpanContext(context);
}

function getContext(span: api.Span | api.SpanContext): api.SpanContext {
return isSpan(span) ? span.context() : span;
}

function isSpan(span: api.Span | api.SpanContext): span is api.Span {
return typeof (span as api.Span).context === 'function';
}
40 changes: 2 additions & 38 deletions packages/opentelemetry-tracing/test/BasicTracerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,49 +210,13 @@ describe('BasicTracerProvider', () => {
childSpan.end();
});

it('should override context parent with option parent', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span');
const overrideParent = tracer.startSpan('my-parent-override-span');
const childSpan = tracer.startSpan(
'child-span',
{
parent: overrideParent,
},
setActiveSpan(ROOT_CONTEXT, span)
);
const context = childSpan.context();
assert.strictEqual(context.traceId, overrideParent.context().traceId);
assert.strictEqual(context.traceFlags, TraceFlags.SAMPLED);
span.end();
childSpan.end();
});

it('should override context parent with option parent context', () => {
const tracer = new BasicTracerProvider().getTracer('default');
const span = tracer.startSpan('my-span');
const overrideParent = tracer.startSpan('my-parent-override-span');
const childSpan = tracer.startSpan(
'child-span',
{
parent: overrideParent.context(),
},
setActiveSpan(ROOT_CONTEXT, span)
);
const context = childSpan.context();
assert.strictEqual(context.traceId, overrideParent.context().traceId);
assert.strictEqual(context.traceFlags, TraceFlags.SAMPLED);
span.end();
childSpan.end();
});

it('should create a root span when parent is null', () => {
it('should create a root span when root is true', () => {
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 },
{ root: true },
setActiveSpan(ROOT_CONTEXT, span)
);
const context = rootSpan.context();
Expand Down