Skip to content

Commit dd500bd

Browse files
committed
Replace SpanClass with isSpan helper
1 parent a592136 commit dd500bd

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed
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+
};

packages/opentelemetry/test/trace.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ROOT_CONTEXT } from '@opentelemetry/api';
44
import { SpanKind } from '@opentelemetry/api';
55
import { TraceFlags, context, trace } from '@opentelemetry/api';
66
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
7-
import { Span as SpanClass } from '@opentelemetry/sdk-trace-base';
87
import {
98
SEMANTIC_ATTRIBUTE_SENTRY_OP,
109
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
@@ -32,6 +31,7 @@ import { getSpanKind } from '../src/utils/getSpanKind';
3231
import { makeTraceState } from '../src/utils/makeTraceState';
3332
import { spanHasAttributes, spanHasName } from '../src/utils/spanTypes';
3433
import { cleanupOtel, mockSdkInit } from './helpers/mockSdkInit';
34+
import { isSpan } from './helpers/isSpan';
3535

3636
describe('trace', () => {
3737
beforeEach(() => {
@@ -537,7 +537,7 @@ describe('trace', () => {
537537
return span;
538538
});
539539

540-
expect(span).not.toBeInstanceOf(SpanClass);
540+
expect(isSpan(span)).toEqual(false);
541541
});
542542

543543
it('creates a span if there is a parent', () => {
@@ -549,7 +549,7 @@ describe('trace', () => {
549549
return span;
550550
});
551551

552-
expect(span).toBeInstanceOf(SpanClass);
552+
expect(isSpan(span)).toEqual(true);
553553
});
554554
});
555555
});
@@ -829,7 +829,7 @@ describe('trace', () => {
829829
it('does not create a span if there is no parent', () => {
830830
const span = startInactiveSpan({ name: 'test span', onlyIfParent: true });
831831

832-
expect(span).not.toBeInstanceOf(SpanClass);
832+
expect(isSpan(span)).toEqual(false);
833833
});
834834

835835
it('creates a span if there is a parent', () => {
@@ -839,7 +839,7 @@ describe('trace', () => {
839839
return span;
840840
});
841841

842-
expect(span).toBeInstanceOf(SpanClass);
842+
expect(isSpan(span)).toEqual(true);
843843
});
844844
});
845845

@@ -1199,7 +1199,7 @@ describe('trace', () => {
11991199
return span;
12001200
});
12011201

1202-
expect(span).not.toBeInstanceOf(SpanClass);
1202+
expect(isSpan(span)).toEqual(false);
12031203
});
12041204

12051205
it('creates a span if there is a parent', () => {
@@ -1211,7 +1211,7 @@ describe('trace', () => {
12111211
return span;
12121212
});
12131213

1214-
expect(span).toBeInstanceOf(SpanClass);
1214+
expect(isSpan(span)).toEqual(true);
12151215
});
12161216
});
12171217
});

0 commit comments

Comments
 (0)