Skip to content
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
4 changes: 3 additions & 1 deletion packages/opentelemetry-tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ export class Tracer implements api.Tracer {
const spanId = this._idGenerator.generateSpanId();
let traceId;
let traceState;
let parentSpanId;
if (!parentContext || !api.trace.isSpanContextValid(parentContext)) {
// New root span.
traceId = this._idGenerator.generateTraceId();
} else {
// New child span.
traceId = parentContext.traceId;
traceState = parentContext.traceState;
parentSpanId = parentContext.spanId;
}

const spanKind = options.kind ?? api.SpanKind.INTERNAL;
Expand Down Expand Up @@ -113,7 +115,7 @@ export class Tracer implements api.Tracer {
name,
spanContext,
spanKind,
parentContext ? parentContext.spanId : undefined,
parentSpanId,
links,
options.startTime
);
Expand Down
42 changes: 42 additions & 0 deletions packages/opentelemetry-tracing/test/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import {
TraceFlags,
ROOT_CONTEXT,
suppressInstrumentation,
SpanContext,
INVALID_TRACEID,
setSpanContext,
} from '@opentelemetry/api';
import { BasicTracerProvider, Tracer, Span } from '../src';
import {
Expand Down Expand Up @@ -132,6 +135,45 @@ describe('Tracer', () => {
});
});

it('should use traceId and spanId from parent', () => {
const parent: SpanContext = {
traceId: '00112233445566778899001122334455',
spanId: '0011223344556677',
traceFlags: TraceFlags.SAMPLED,
};
const tracer = new Tracer(
{ name: 'default', version: '0.0.1' },
{},
tracerProvider
);
const span = tracer.startSpan(
'aSpan',
undefined,
setSpanContext(ROOT_CONTEXT, parent)
);
assert.strictEqual((span as Span).parentSpanId, parent.spanId);
assert.strictEqual(span.context().traceId, parent.traceId);
});

it('should not use spanId from invalid parent', () => {
const parent: SpanContext = {
traceId: INVALID_TRACEID,
spanId: '0011223344556677',
traceFlags: TraceFlags.SAMPLED,
};
const tracer = new Tracer(
{ name: 'default', version: '0.0.1' },
{},
tracerProvider
);
const span = tracer.startSpan(
'aSpan',
undefined,
setSpanContext(ROOT_CONTEXT, parent)
);
assert.strictEqual((span as Span).parentSpanId, undefined);
});

if (typeof process !== 'undefined' && process.release.name === 'node') {
it('should sample a trace when OTEL_SAMPLING_PROBABILITY is invalid', () => {
process.env.OTEL_SAMPLING_PROBABILITY = 'invalid value';
Expand Down