Skip to content

Commit cb8ee5f

Browse files
committed
ref(core): Remove OTel span handling from spanToJSON
1 parent 0e100ef commit cb8ee5f

2 files changed

Lines changed: 10 additions & 362 deletions

File tree

‎packages/core/src/utils/spanUtils.ts‎

Lines changed: 8 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import { getCurrentScope } from '../currentScopes';
77
import type { Scope } from '../scope';
88
import {
99
SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME,
10-
SEMANTIC_ATTRIBUTE_SENTRY_OP,
11-
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
1210
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1311
SEMANTIC_ATTRIBUTE_SENTRY_STATUS_MESSAGE,
1412
} from '../semanticAttributes';
@@ -22,7 +20,6 @@ import type {
2220
Span,
2321
SpanAttributes,
2422
SpanJSON,
25-
SpanOrigin,
2623
SpanTimeInput,
2724
StreamedSpanJSON,
2825
} from '../types/span';
@@ -167,40 +164,20 @@ function ensureTimestampInSeconds(timestamp: number): number {
167164
* Convert a span to a JSON representation.
168165
*/
169166
// Note: Because of this, we currently have a circular type dependency (which we opted out of in package.json).
170-
// This is not avoidable as we need `spanToJSON` in `spanUtils.ts`, which in turn is needed by `span.ts` for backwards compatibility.
167+
// This is not avoidable as we need `spanToJSON` in `spanUtils.ts`, which in turn is needed by `sentrySpan.ts` for backwards compatibility.
171168
// And `spanToJSON` needs the Span class from `span.ts` to check here.
172169
export function spanToStaticSpanJSON(span: Span): SpanJSON {
173170
if (spanIsSentrySpan(span)) {
174171
return span.getSpanJSON();
175172
}
176173

177-
const { spanId: span_id, traceId: trace_id } = span.spanContext();
178-
179-
// Handle a span from @opentelemetry/sdk-base-trace's `Span` class
180-
if (spanIsOpenTelemetrySdkTraceBaseSpan(span)) {
181-
const { attributes, startTime, name, endTime, status, links } = span;
182-
183-
return {
184-
span_id,
185-
trace_id,
186-
data: attributes,
187-
description: name,
188-
parent_span_id: getOtelParentSpanId(span),
189-
start_timestamp: spanTimeInputToSeconds(startTime),
190-
// This is [0,0] by default in OTEL, in which case we want to interpret this as no end time
191-
timestamp: spanTimeInputToSeconds(endTime) || undefined,
192-
status: getStatusMessage(status),
193-
op: attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP],
194-
origin: attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] as SpanOrigin | undefined,
195-
links: convertSpanLinksForEnvelope(links),
196-
};
197-
}
198-
199-
// Finally, at least we have `spanContext()`....
174+
// because `spanToJSON` accepts a `Span` interface rather than a `SentrySpan` instance,
175+
// we need to handle the case where the span is not a Sentry span.
200176
// This should not actually happen in reality, but we need to handle it for type safety.
177+
const ctx = span.spanContext();
201178
return {
202-
span_id,
203-
trace_id,
179+
span_id: ctx.spanId,
180+
trace_id: ctx.traceId,
204181
start_timestamp: 0,
205182
status: 'ok',
206183
data: {},
@@ -217,26 +194,8 @@ export function spanToJSON(span: Span): StreamedSpanJSON {
217194

218195
const { spanId: span_id, traceId: trace_id } = span.spanContext();
219196

220-
// Handle a span from @opentelemetry/sdk-base-trace's `Span` class
221-
if (spanIsOpenTelemetrySdkTraceBaseSpan(span)) {
222-
const { attributes, startTime, name, endTime, status, links } = span;
223-
224-
return {
225-
name,
226-
span_id,
227-
trace_id,
228-
parent_span_id: getOtelParentSpanId(span),
229-
start_timestamp: spanTimeInputToSeconds(startTime),
230-
// This is [0,0] by default in OTEL, in which case we want to interpret this as no end time
231-
end_timestamp: spanTimeInputToSeconds(endTime) || undefined,
232-
is_segment: span === INTERNAL_getSegmentSpan(span),
233-
status: getSimpleStatus(status),
234-
attributes: addStatusMessageAttribute(attributes, status),
235-
links: getStreamedSpanLinks(links),
236-
};
237-
}
238-
239-
// Finally, as a fallback, at least we have `spanContext()`....
197+
// Because `spanToJSON` accepts a `Span` interface rather than a `SentrySpan` instance,
198+
// we need to handle the case where the span is not a Sentry span.
240199
// This should not actually happen in reality, but we need to handle it for type safety.
241200
return {
242201
span_id,
@@ -249,20 +208,6 @@ export function spanToJSON(span: Span): StreamedSpanJSON {
249208
};
250209
}
251210

252-
/**
253-
* In preparation for the next major of OpenTelemetry, we want to support
254-
* looking up the parent span id according to the new API
255-
* In OTel v1, the parent span id is accessed as `parentSpanId`
256-
* In OTel v2, the parent span id is accessed as `spanId` on the `parentSpanContext`
257-
*/
258-
function getOtelParentSpanId(span: OpenTelemetrySdkTraceBaseSpan): string | undefined {
259-
return 'parentSpanId' in span
260-
? span.parentSpanId
261-
: 'parentSpanContext' in span
262-
? (span.parentSpanContext as { spanId?: string } | undefined)?.spanId
263-
: undefined;
264-
}
265-
266211
/**
267212
* Converts a {@link StreamedSpanJSON} to a {@link SerializedSpan}.
268213
* This is the final serialized span format that is sent to Sentry.
@@ -282,22 +227,6 @@ export function streamedSpanJsonToSerializedSpan(spanJson: StreamedSpanJSON): Se
282227
};
283228
}
284229

285-
function spanIsOpenTelemetrySdkTraceBaseSpan(span: Span): span is OpenTelemetrySdkTraceBaseSpan {
286-
const castSpan = span as Partial<OpenTelemetrySdkTraceBaseSpan>;
287-
return !!castSpan.attributes && !!castSpan.startTime && !!castSpan.name && !!castSpan.endTime && !!castSpan.status;
288-
}
289-
290-
/** Exported only for tests. */
291-
export interface OpenTelemetrySdkTraceBaseSpan extends Span {
292-
attributes: SpanAttributes;
293-
startTime: SpanTimeInput;
294-
name: string;
295-
status: SpanStatus;
296-
endTime: SpanTimeInput;
297-
parentSpanId?: string;
298-
links?: SpanLink[];
299-
}
300-
301230
/**
302231
* Sadly, due to circular dependency checks we cannot actually import the Span class here and check for instanceof.
303232
* :( So instead we approximate this by checking if it has the `getSpanJSON` method.

0 commit comments

Comments
 (0)