@@ -181,23 +181,12 @@ export function spanToJSON(span: Span): SpanJSON {
181181 if ( spanIsOpenTelemetrySdkTraceBaseSpan ( span ) ) {
182182 const { attributes, startTime, name, endTime, status, links } = span ;
183183
184- // In preparation for the next major of OpenTelemetry, we want to support
185- // looking up the parent span id according to the new API
186- // In OTel v1, the parent span id is accessed as `parentSpanId`
187- // In OTel v2, the parent span id is accessed as `spanId` on the `parentSpanContext`
188- const parentSpanId =
189- 'parentSpanId' in span
190- ? span . parentSpanId
191- : 'parentSpanContext' in span
192- ? ( span . parentSpanContext as { spanId ?: string } | undefined ) ?. spanId
193- : undefined ;
194-
195184 return {
196185 span_id,
197186 trace_id,
198187 data : attributes ,
199188 description : name ,
200- parent_span_id : parentSpanId ,
189+ parent_span_id : getOtelParentSpanId ( span ) ,
201190 start_timestamp : spanTimeInputToSeconds ( startTime ) ,
202191 // This is [0,0] by default in OTEL, in which case we want to interpret this as no end time
203192 timestamp : spanTimeInputToSeconds ( endTime ) || undefined ,
@@ -232,22 +221,11 @@ export function spanToStreamedSpanJSON(span: Span): StreamedSpanJSON {
232221 if ( spanIsOpenTelemetrySdkTraceBaseSpan ( span ) ) {
233222 const { attributes, startTime, name, endTime, status, links } = span ;
234223
235- // In preparation for the next major of OpenTelemetry, we want to support
236- // looking up the parent span id according to the new API
237- // In OTel v1, the parent span id is accessed as `parentSpanId`
238- // In OTel v2, the parent span id is accessed as `spanId` on the `parentSpanContext`
239- const parentSpanId =
240- 'parentSpanId' in span
241- ? span . parentSpanId
242- : 'parentSpanContext' in span
243- ? ( span . parentSpanContext as { spanId ?: string } | undefined ) ?. spanId
244- : undefined ;
245-
246224 return {
247225 name,
248226 span_id,
249227 trace_id,
250- parent_span_id : parentSpanId ,
228+ parent_span_id : getOtelParentSpanId ( span ) ,
251229 start_timestamp : spanTimeInputToSeconds ( startTime ) ,
252230 end_timestamp : spanTimeInputToSeconds ( endTime ) ,
253231 is_segment : span === INTERNAL_getSegmentSpan ( span ) ,
@@ -270,6 +248,20 @@ export function spanToStreamedSpanJSON(span: Span): StreamedSpanJSON {
270248 } ;
271249}
272250
251+ /**
252+ * In preparation for the next major of OpenTelemetry, we want to support
253+ * looking up the parent span id according to the new API
254+ * In OTel v1, the parent span id is accessed as `parentSpanId`
255+ * In OTel v2, the parent span id is accessed as `spanId` on the `parentSpanContext`
256+ */
257+ function getOtelParentSpanId ( span : OpenTelemetrySdkTraceBaseSpan ) : string | undefined {
258+ return 'parentSpanId' in span
259+ ? span . parentSpanId
260+ : 'parentSpanContext' in span
261+ ? ( span . parentSpanContext as { spanId ?: string } | undefined ) ?. spanId
262+ : undefined ;
263+ }
264+
273265/**
274266 * Converts a {@link StreamedSpanJSON} to a {@link SerializedSpan}.
275267 * This is the final serialized span format that is sent to Sentry.
0 commit comments