Skip to content

Commit

Permalink
chore: remove explicit parent option (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyladan authored Nov 2, 2020
1 parent c4cdc4c commit 18b49af
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 100 deletions.
2 changes: 1 addition & 1 deletion packages/opentelemetry-api/src/api/global-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ export function makeGetter<T>(
* version. If the global API is not compatible with the API package
* attempting to get it, a NOOP API implementation will be returned.
*/
export const API_BACKWARDS_COMPATIBILITY_VERSION = 1;
export const API_BACKWARDS_COMPATIBILITY_VERSION = 2;
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)', () => {
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, {
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)
);
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)
);
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 @@ -177,15 +177,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

0 comments on commit 18b49af

Please sign in to comment.