Skip to content

Commit 1afa8ba

Browse files
feat(sdk-trace-base)!: Add parentSpanContext and remove parentSpanId (#5450)
Co-authored-by: Marc Pichler <[email protected]>
1 parent 14d55d8 commit 1afa8ba

File tree

28 files changed

+168
-106
lines changed

28 files changed

+168
-106
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se
1111

1212
### :boom: Breaking Change
1313

14+
* feat(sdk-trace-base)!: Add `parentSpanContext` and remove `parentSpanId` from `Span` and `ReadableSpan` [#5450](https://github.com/open-telemetry/opentelemetry-js/pull/5450) @JacksonWeber
15+
* (user-facing): the SDK's `Span`s `parentSpanId` was replaced by `parentSpanContext`, to migrate to the new property, please replace `span.parentSpanId` -> `span.parentSpanContext?.spanId`
1416
* feat(sdk-metrics)!: drop deprecated `type` field on `MetricDescriptor` [#5291](https://github.com/open-telemetry/opentelemetry-js/pull/5291) @chancancode
1517
* feat(sdk-metrics)!: drop deprecated `InstrumentDescriptor` type; use `MetricDescriptor` instead [#5277](https://github.com/open-telemetry/opentelemetry-js/pull/5266) @chancancode
1618
* feat(sdk-metrics)!: bump minimum version of `@opentelemetry/api` peer dependency to 1.9.0 [#5254](https://github.com/open-telemetry/opentelemetry-js/pull/5254) @chancancode

experimental/packages/opentelemetry-instrumentation-fetch/test/fetch.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ describe('fetch', () => {
400400
const span: tracing.ReadableSpan = exportedSpans[0];
401401

402402
assert.strictEqual(
403-
span.parentSpanId,
403+
span.parentSpanContext?.spanId,
404404
rootSpan!.spanContext().spanId,
405405
'parent span is not root span'
406406
);
@@ -775,7 +775,7 @@ describe('fetch', () => {
775775
const span: tracing.ReadableSpan = exportedSpans[0];
776776

777777
assert.strictEqual(
778-
span.parentSpanId,
778+
span.parentSpanContext?.spanId,
779779
rootSpan!.spanContext().spanId,
780780
'parent span is not root span'
781781
);
@@ -1636,7 +1636,7 @@ describe('fetch', () => {
16361636
const span: tracing.ReadableSpan = exportedSpans[0];
16371637

16381638
assert.strictEqual(
1639-
span.parentSpanId,
1639+
span.parentSpanContext?.spanId,
16401640
rootSpan!.spanContext().spanId,
16411641
'parent span is not root span'
16421642
);
@@ -1686,7 +1686,7 @@ describe('fetch', () => {
16861686
const span: tracing.ReadableSpan = exportedSpans[0];
16871687

16881688
assert.strictEqual(
1689-
span.parentSpanId,
1689+
span.parentSpanContext?.spanId,
16901690
rootSpan!.spanContext().spanId,
16911691
'parent span is not root span'
16921692
);
@@ -1768,7 +1768,7 @@ describe('fetch', () => {
17681768
const span: tracing.ReadableSpan = exportedSpans[0];
17691769

17701770
assert.strictEqual(
1771-
span.parentSpanId,
1771+
span.parentSpanContext?.spanId,
17721772
rootSpan!.spanContext().spanId,
17731773
'parent span is not root span'
17741774
);
@@ -1867,7 +1867,7 @@ describe('fetch', () => {
18671867
const span: tracing.ReadableSpan = exportedSpans[0];
18681868

18691869
assert.strictEqual(
1870-
span.parentSpanId,
1870+
span.parentSpanContext?.spanId,
18711871
rootSpan!.spanContext().spanId,
18721872
'parent span is not root span'
18731873
);
@@ -1925,7 +1925,7 @@ describe('fetch', () => {
19251925
const span: tracing.ReadableSpan = exportedSpans[0];
19261926

19271927
assert.strictEqual(
1928-
span.parentSpanId,
1928+
span.parentSpanContext?.spanId,
19291929
rootSpan!.spanContext().spanId,
19301930
'parent span is not root span'
19311931
);

experimental/packages/opentelemetry-instrumentation-grpc/test/helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ export const runTests = (
622622
);
623623
assert.strictEqual(
624624
rootSpan.spanContext().spanId,
625-
clientSpan.parentSpanId
625+
clientSpan.parentSpanContext?.spanId
626626
);
627627
}
628628
})
@@ -740,7 +740,7 @@ export const runTests = (
740740
);
741741
assert.strictEqual(
742742
rootSpan.spanContext().spanId,
743-
clientSpan.parentSpanId
743+
clientSpan.parentSpanContext?.spanId
744744
);
745745
});
746746
});

experimental/packages/opentelemetry-instrumentation-grpc/test/protobuf-ts-utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ export function assertExportedSpans(
103103
rootSpan?.spanContext().traceId,
104104
serverSpan.spanContext().traceId
105105
);
106-
assert.strictEqual(rootSpan?.spanContext().spanId, clientSpan.parentSpanId);
106+
assert.strictEqual(
107+
rootSpan?.spanContext().spanId,
108+
clientSpan.parentSpanContext?.spanId
109+
);
107110
}
108111
}

experimental/packages/opentelemetry-instrumentation-grpc/test/utils/assertionUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ export const assertPropagation = (
9999
const targetSpanContext = incomingSpan.spanContext();
100100
const sourceSpanContext = outgoingSpan.spanContext();
101101
assert.strictEqual(targetSpanContext.traceId, sourceSpanContext.traceId);
102-
assert.strictEqual(incomingSpan.parentSpanId, sourceSpanContext.spanId);
102+
assert.strictEqual(
103+
incomingSpan.parentSpanContext?.spanId,
104+
sourceSpanContext.spanId
105+
);
103106
assert.strictEqual(
104107
targetSpanContext.traceFlags,
105108
sourceSpanContext.traceFlags

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ export const assertSpan = (
185185
validations.component,
186186
' must have http.scheme attribute'
187187
);
188-
assert.ok(typeof span.parentSpanId === 'string');
189-
assert.ok(isValidSpanId(span.parentSpanId));
188+
assert.ok(typeof span.parentSpanContext?.spanId === 'string');
189+
assert.ok(isValidSpanId(span.parentSpanContext.spanId));
190190
} else if (validations.reqHeaders) {
191191
assert.ok(validations.reqHeaders[DummyPropagation.TRACE_CONTEXT_KEY]);
192192
assert.ok(validations.reqHeaders[DummyPropagation.SPAN_CONTEXT_KEY]);

experimental/packages/opentelemetry-instrumentation-xml-http-request/test/xhr.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ describe('xhr', () => {
388388
it('should create a span with correct root span', () => {
389389
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
390390
assert.strictEqual(
391-
span.parentSpanId,
391+
span.parentSpanContext?.spanId,
392392
rootSpan.spanContext().spanId,
393393
'parent span is not root span'
394394
);
@@ -492,7 +492,7 @@ describe('xhr', () => {
492492
const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
493493
const parentSpan: tracing.ReadableSpan = exportSpy.args[1][0][0];
494494
assert.strictEqual(
495-
span.parentSpanId,
495+
span.parentSpanContext?.spanId,
496496
parentSpan.spanContext().spanId,
497497
'parent span is not root span'
498498
);
@@ -1586,7 +1586,7 @@ describe('xhr', () => {
15861586
it('should create a span with correct root span', () => {
15871587
const span: tracing.ReadableSpan = exportSpy.args[1][0][0];
15881588
assert.strictEqual(
1589-
span.parentSpanId,
1589+
span.parentSpanContext?.spanId,
15901590
rootSpan.spanContext().spanId,
15911591
'parent span is not root span'
15921592
);
@@ -1690,7 +1690,7 @@ describe('xhr', () => {
16901690
const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
16911691
const parentSpan: tracing.ReadableSpan = exportSpy.args[1][0][0];
16921692
assert.strictEqual(
1693-
span.parentSpanId,
1693+
span.parentSpanContext?.spanId,
16941694
parentSpan.spanContext().spanId,
16951695
'parent span is not root span'
16961696
);

experimental/packages/otlp-transformer/src/trace/internal.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ import { getOtlpEncoder } from '../common/utils';
3737
export function sdkSpanToOtlpSpan(span: ReadableSpan, encoder: Encoder): ISpan {
3838
const ctx = span.spanContext();
3939
const status = span.status;
40+
const parentSpanId = span.parentSpanContext?.spanId
41+
? encoder.encodeSpanContext(span.parentSpanContext?.spanId)
42+
: undefined;
4043
return {
4144
traceId: encoder.encodeSpanContext(ctx.traceId),
4245
spanId: encoder.encodeSpanContext(ctx.spanId),
43-
parentSpanId: encoder.encodeOptionalSpanContext(span.parentSpanId),
46+
parentSpanId: parentSpanId,
4447
traceState: ctx.traceState?.serialize(),
4548
name: span.name,
4649
// Span kind is offset by 1 because the API does not define a value for unset

experimental/packages/otlp-transformer/test/trace.test.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { createExportTraceServiceRequest } from '../src/trace/internal';
2626
import { ProtobufTraceSerializer } from '../src/trace/protobuf';
2727
import { JsonTraceSerializer } from '../src/trace/json';
2828
import { hexToBinary } from '../src/common/hex-to-binary';
29+
import { ISpan } from '../src/trace/internal-types';
2930

3031
function createExpectedSpanJson(options: OtlpEncodingOptions) {
3132
const useHex = options.useHex ?? false;
@@ -243,7 +244,11 @@ describe('Trace', () => {
243244
isRemote: false,
244245
traceState: new TraceState('span=bar'),
245246
}),
246-
parentSpanId: '0000000000000001',
247+
parentSpanContext: {
248+
spanId: '0000000000000001',
249+
traceId: '00000000000000000000000000000001',
250+
traceFlags: TraceFlags.SAMPLED,
251+
},
247252
attributes: { 'string-attribute': 'some attribute value' },
248253
duration: [1, 300000000],
249254
endTime: [1640715558, 642725388],
@@ -329,25 +334,27 @@ describe('Trace', () => {
329334
});
330335

331336
it('serializes a span without a parent with useHex = true', () => {
332-
(span as any).parentSpanId = undefined;
337+
(span as any).parentSpanContext.spanId = undefined;
333338
const exportRequest = createExportTraceServiceRequest([span], {
334339
useHex: true,
335340
});
336341
assert.ok(exportRequest);
337342
assert.strictEqual(
338-
exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].parentSpanId,
343+
(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0] as ISpan)
344+
.parentSpanId,
339345
undefined
340346
);
341347
});
342348

343349
it('serializes a span without a parent with useHex = false', () => {
344-
(span as any).parentSpanId = undefined;
350+
(span as any).parentSpanContext.spanId = undefined;
345351
const exportRequest = createExportTraceServiceRequest([span], {
346352
useHex: false,
347353
});
348354
assert.ok(exportRequest);
349355
assert.strictEqual(
350-
exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].parentSpanId,
356+
(exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0] as ISpan)
357+
.parentSpanId,
351358
undefined
352359
);
353360
});
@@ -446,7 +453,6 @@ describe('Trace', () => {
446453
root.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest.decode(
447454
serialized
448455
);
449-
450456
const expected = createExpectedSpanProtobuf();
451457
const decodedObj =
452458
root.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest.toObject(
@@ -461,7 +467,6 @@ describe('Trace', () => {
461467
bytes: String,
462468
}
463469
);
464-
465470
assert.deepStrictEqual(decodedObj, expected);
466471
});
467472

experimental/packages/shim-opencensus/test/ShimSpan.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe('ShimSpan', () => {
107107

108108
assert.strictEqual(childSpan.name, 'span');
109109
assert.deepStrictEqual(
110-
childSpan.parentSpanId,
110+
childSpan.parentSpanContext?.spanId,
111111
parentSpan.spanContext().spanId
112112
);
113113
});
@@ -120,7 +120,7 @@ describe('ShimSpan', () => {
120120

121121
assert.strictEqual(childSpan.name, 'child');
122122
assert.deepStrictEqual(
123-
childSpan.parentSpanId,
123+
childSpan.parentSpanContext?.spanId,
124124
parentSpan.spanContext().spanId
125125
);
126126
});

0 commit comments

Comments
 (0)