Skip to content

Commit c0b021b

Browse files
Flarnavmarchaud
andauthored
fix: don't use spanId from invalid parent (#2105)
Co-authored-by: Valentin Marchaud <[email protected]>
1 parent 181f11e commit c0b021b

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

packages/opentelemetry-instrumentation-http/test/utils/DummyPropagation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class DummyPropagation implements TextMapPropagator {
2828
extract(context: Context, carrier: http.OutgoingHttpHeaders) {
2929
const extractedSpanContext = {
3030
traceId: carrier[DummyPropagation.TRACE_CONTEXT_KEY] as string,
31-
spanId: DummyPropagation.SPAN_CONTEXT_KEY,
31+
spanId: carrier[DummyPropagation.SPAN_CONTEXT_KEY] as string,
3232
traceFlags: TraceFlags.SAMPLED,
3333
isRemote: true,
3434
};

packages/opentelemetry-instrumentation-http/test/utils/assertSpan.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { SpanKind, SpanStatus } from '@opentelemetry/api';
16+
import { isValidSpanId, SpanKind, SpanStatus } from '@opentelemetry/api';
1717
import { hrTimeToNanoseconds } from '@opentelemetry/core';
1818
import { ReadableSpan } from '@opentelemetry/tracing';
1919
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
@@ -161,7 +161,8 @@ export const assertSpan = (
161161
'must have HOST_IP'
162162
);
163163
}
164-
assert.strictEqual(span.parentSpanId, DummyPropagation.SPAN_CONTEXT_KEY);
164+
assert.ok(typeof span.parentSpanId === 'string');
165+
assert.ok(isValidSpanId(span.parentSpanId));
165166
} else if (validations.reqHeaders) {
166167
assert.ok(validations.reqHeaders[DummyPropagation.TRACE_CONTEXT_KEY]);
167168
assert.ok(validations.reqHeaders[DummyPropagation.SPAN_CONTEXT_KEY]);

packages/opentelemetry-tracing/src/Tracer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ export class Tracer implements api.Tracer {
7171
const spanId = this._idGenerator.generateSpanId();
7272
let traceId;
7373
let traceState;
74+
let parentSpanId;
7475
if (!parentContext || !api.trace.isSpanContextValid(parentContext)) {
7576
// New root span.
7677
traceId = this._idGenerator.generateTraceId();
7778
} else {
7879
// New child span.
7980
traceId = parentContext.traceId;
8081
traceState = parentContext.traceState;
82+
parentSpanId = parentContext.spanId;
8183
}
8284

8385
const spanKind = options.kind ?? api.SpanKind.INTERNAL;
@@ -113,7 +115,7 @@ export class Tracer implements api.Tracer {
113115
name,
114116
spanContext,
115117
spanKind,
116-
parentContext ? parentContext.spanId : undefined,
118+
parentSpanId,
117119
links,
118120
options.startTime
119121
);

packages/opentelemetry-tracing/test/Tracer.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import {
2121
TraceFlags,
2222
ROOT_CONTEXT,
2323
suppressInstrumentation,
24+
SpanContext,
25+
INVALID_TRACEID,
26+
setSpanContext,
2427
} from '@opentelemetry/api';
2528
import { BasicTracerProvider, Tracer, Span } from '../src';
2629
import {
@@ -132,6 +135,45 @@ describe('Tracer', () => {
132135
});
133136
});
134137

138+
it('should use traceId and spanId from parent', () => {
139+
const parent: SpanContext = {
140+
traceId: '00112233445566778899001122334455',
141+
spanId: '0011223344556677',
142+
traceFlags: TraceFlags.SAMPLED,
143+
};
144+
const tracer = new Tracer(
145+
{ name: 'default', version: '0.0.1' },
146+
{},
147+
tracerProvider
148+
);
149+
const span = tracer.startSpan(
150+
'aSpan',
151+
undefined,
152+
setSpanContext(ROOT_CONTEXT, parent)
153+
);
154+
assert.strictEqual((span as Span).parentSpanId, parent.spanId);
155+
assert.strictEqual(span.context().traceId, parent.traceId);
156+
});
157+
158+
it('should not use spanId from invalid parent', () => {
159+
const parent: SpanContext = {
160+
traceId: INVALID_TRACEID,
161+
spanId: '0011223344556677',
162+
traceFlags: TraceFlags.SAMPLED,
163+
};
164+
const tracer = new Tracer(
165+
{ name: 'default', version: '0.0.1' },
166+
{},
167+
tracerProvider
168+
);
169+
const span = tracer.startSpan(
170+
'aSpan',
171+
undefined,
172+
setSpanContext(ROOT_CONTEXT, parent)
173+
);
174+
assert.strictEqual((span as Span).parentSpanId, undefined);
175+
});
176+
135177
if (typeof process !== 'undefined' && process.release.name === 'node') {
136178
it('should sample a trace when OTEL_SAMPLING_PROBABILITY is invalid', () => {
137179
process.env.OTEL_SAMPLING_PROBABILITY = 'invalid value';

0 commit comments

Comments
 (0)