Skip to content

Commit 1a9c99e

Browse files
committed
Adjust to breaking changes
1 parent 28ddc15 commit 1a9c99e

File tree

6 files changed

+50
-24
lines changed

6 files changed

+50
-24
lines changed

dev-packages/opentelemetry-v2-tests/test/helpers/initOtel.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { context, diag, DiagLogLevel, propagation, trace } from '@opentelemetry/api';
22
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks';
3-
import { Resource } from '@opentelemetry/resources';
3+
import { resourceFromAttributes, defaultResource } from '@opentelemetry/resources';
44
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base';
55
import {
66
ATTR_SERVICE_NAME,
@@ -54,12 +54,14 @@ export function setupOtel(client: TestClientInterface): BasicTracerProvider {
5454
// Create and configure NodeTracerProvider
5555
const provider = new BasicTracerProvider({
5656
sampler: new SentrySampler(client),
57-
resource: new Resource({
58-
[ATTR_SERVICE_NAME]: 'opentelemetry-test',
59-
// eslint-disable-next-line deprecation/deprecation
60-
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
61-
[ATTR_SERVICE_VERSION]: SDK_VERSION,
62-
}),
57+
resource: defaultResource().merge(
58+
resourceFromAttributes({
59+
[ATTR_SERVICE_NAME]: 'opentelemetry-test',
60+
// eslint-disable-next-line deprecation/deprecation
61+
[SEMRESATTRS_SERVICE_NAMESPACE]: 'sentry',
62+
[ATTR_SERVICE_VERSION]: SDK_VERSION,
63+
}),
64+
),
6365
forceFlushTimeoutMillis: 500,
6466
spanProcessors: [new SentrySpanProcessor()],
6567
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { Span } from '@opentelemetry/api';
2+
import { INVALID_TRACEID, INVALID_SPANID, type SpanContext } from '@opentelemetry/api';
3+
4+
export const isSpan = (value: unknown): value is Span => {
5+
return (
6+
typeof value === 'object' &&
7+
value !== null &&
8+
'spanContext' in value &&
9+
(value.spanContext as () => SpanContext)().traceId !== INVALID_TRACEID &&
10+
(value.spanContext as () => SpanContext)().spanId !== INVALID_SPANID
11+
);
12+
};

dev-packages/opentelemetry-v2-tests/test/sampler.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ describe('SentrySampler', () => {
8181
const links = undefined;
8282

8383
const actual = sampler.shouldSample(ctx, traceId, spanName, spanKind, spanAttributes, links);
84-
expect(actual).toEqual({
85-
decision: SamplingDecision.RECORD_AND_SAMPLED,
86-
attributes: { 'sentry.sample_rate': 1 },
87-
traceState: expect.any(TraceState),
88-
});
84+
expect(actual).toEqual(
85+
expect.objectContaining({
86+
decision: SamplingDecision.RECORD_AND_SAMPLED,
87+
attributes: { 'sentry.sample_rate': 1 },
88+
}),
89+
);
90+
expect(actual.traceState?.constructor.name).toBe('TraceState');
8991
expect(spyOnDroppedEvent).toHaveBeenCalledTimes(0);
9092

9193
spyOnDroppedEvent.mockReset();

dev-packages/opentelemetry-v2-tests/test/trace.test.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import type { Span, TimeInput } from '@opentelemetry/api';
33
import { context, ROOT_CONTEXT, SpanKind, trace, TraceFlags } from '@opentelemetry/api';
44
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
5-
import { Span as SpanClass } from '@opentelemetry/sdk-trace-base';
65
import { SEMATTRS_HTTP_METHOD } from '@opentelemetry/semantic-conventions';
76
import type { Event, Scope } from '@sentry/core';
87
import {
@@ -34,6 +33,8 @@ import { getSpanKind } from '../../../packages/opentelemetry/src/utils/getSpanKi
3433
import { makeTraceState } from '../../../packages/opentelemetry/src/utils/makeTraceState';
3534
import { spanHasAttributes, spanHasName } from '../../../packages/opentelemetry/src/utils/spanTypes';
3635
import { cleanupOtel, mockSdkInit } from './helpers/mockSdkInit';
36+
import { isSpan } from './helpers/isSpan';
37+
import { getParentSpanId } from '../../../packages/opentelemetry/src/utils/getParentSpanId';
3738

3839
describe('trace', () => {
3940
beforeEach(() => {
@@ -539,7 +540,7 @@ describe('trace', () => {
539540
return span;
540541
});
541542

542-
expect(span).not.toBeInstanceOf(SpanClass);
543+
expect(isSpan(span)).toBe(false);
543544
});
544545

545546
it('creates a span if there is a parent', () => {
@@ -551,7 +552,7 @@ describe('trace', () => {
551552
return span;
552553
});
553554

554-
expect(span).toBeInstanceOf(SpanClass);
555+
expect(isSpan(span)).toBe(true);
555556
});
556557
});
557558
});
@@ -831,7 +832,7 @@ describe('trace', () => {
831832
it('does not create a span if there is no parent', () => {
832833
const span = startInactiveSpan({ name: 'test span', onlyIfParent: true });
833834

834-
expect(span).not.toBeInstanceOf(SpanClass);
835+
expect(isSpan(span)).toBe(false);
835836
});
836837

837838
it('creates a span if there is a parent', () => {
@@ -841,7 +842,7 @@ describe('trace', () => {
841842
return span;
842843
});
843844

844-
expect(span).toBeInstanceOf(SpanClass);
845+
expect(isSpan(span)).toBe(true);
845846
});
846847
});
847848

@@ -1201,7 +1202,7 @@ describe('trace', () => {
12011202
return span;
12021203
});
12031204

1204-
expect(span).not.toBeInstanceOf(SpanClass);
1205+
expect(isSpan(span)).toBe(false);
12051206
});
12061207

12071208
it('creates a span if there is a parent', () => {
@@ -1213,7 +1214,7 @@ describe('trace', () => {
12131214
return span;
12141215
});
12151216

1216-
expect(span).toBeInstanceOf(SpanClass);
1217+
expect(isSpan(span)).toBe(true);
12171218
});
12181219
});
12191220
});
@@ -1930,5 +1931,5 @@ function getSpanAttributes(span: AbstractSpan): Record<string, unknown> | undefi
19301931
}
19311932

19321933
function getSpanParentSpanId(span: AbstractSpan): string | undefined {
1933-
return (span as ReadableSpan).parentSpanId;
1934+
return getParentSpanId(span as ReadableSpan);
19341935
}

dev-packages/opentelemetry-v2-tests/test/utils/setupCheck.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ describe('openTelemetrySetupCheck', () => {
3535
const client = new TestClient(getDefaultTestClientOptions());
3636
provider = new BasicTracerProvider({
3737
sampler: new SentrySampler(client),
38+
spanProcessors: [new SentrySpanProcessor()],
3839
});
39-
// We want to test this deprecated case also works
40-
// eslint-disable-next-line deprecation/deprecation
41-
provider.addSpanProcessor(new SentrySpanProcessor());
4240

4341
const setup = openTelemetrySetupCheck();
4442
expect(setup).toEqual(['SentrySampler', 'SentrySpanProcessor']);

packages/core/src/utils/spanUtils.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,18 @@ export function spanToJSON(span: Span): SpanJSON {
139139

140140
// Handle a span from @opentelemetry/sdk-base-trace's `Span` class
141141
if (spanIsOpenTelemetrySdkTraceBaseSpan(span)) {
142-
const { attributes, startTime, name, endTime, parentSpanId, status, links } = span;
142+
const { attributes, startTime, name, endTime, status, links } = span;
143+
144+
// In preparation for the next major of OpenTelemetry, we want to support
145+
// looking up the parent span id according to the new API
146+
// In OTel v1, the parent span id is accessed as `parentSpanId`
147+
// In OTel v2, the parent span id is accessed as `spanId` on the `parentSpanContext`
148+
const parentSpanId =
149+
'parentSpanId' in span
150+
? span.parentSpanId
151+
: 'parentSpanContext' in span
152+
? (span.parentSpanContext as { spanId?: string } | undefined)?.spanId
153+
: undefined;
143154

144155
return {
145156
span_id,

0 commit comments

Comments
 (0)