Skip to content

Commit 22d5385

Browse files
committed
feat(opentelemetry): Widen peer dependencies to support Otel v2
1 parent 79480cf commit 22d5385

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

packages/opentelemetry/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
},
4444
"peerDependencies": {
4545
"@opentelemetry/api": "^1.9.0",
46-
"@opentelemetry/context-async-hooks": "^1.30.1",
47-
"@opentelemetry/core": "^1.30.1",
48-
"@opentelemetry/instrumentation": "^0.57.1",
49-
"@opentelemetry/sdk-trace-base": "^1.30.1",
50-
"@opentelemetry/semantic-conventions": "^1.28.0"
46+
"@opentelemetry/context-async-hooks": "^1.30.1 || ^2.0.0",
47+
"@opentelemetry/core": "^1.30.1 || ^2.0.0",
48+
"@opentelemetry/instrumentation": "^0.57.1 || ^0.200.0",
49+
"@opentelemetry/sdk-trace-base": "^1.30.1 || ^2.0.0",
50+
"@opentelemetry/semantic-conventions": "^1.30.0"
5151
},
5252
"devDependencies": {
5353
"@opentelemetry/api": "^1.9.0",

packages/opentelemetry/src/custom/client.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,7 @@ export function wrapClientClass<
4949
*/
5050
public async flush(timeout?: number): Promise<boolean> {
5151
const provider = this.traceProvider;
52-
const spanProcessor = provider?.activeSpanProcessor;
53-
54-
if (spanProcessor) {
55-
await spanProcessor.forceFlush();
56-
}
57-
52+
await provider?.forceFlush();
5853
return super.flush(timeout);
5954
}
6055
}

packages/opentelemetry/src/spanExporter.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from '@sentry/core';
2929
import { DEBUG_BUILD } from './debug-build';
3030
import { SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE } from './semanticAttributes';
31+
import { getParentSpanId } from './utils/getParentSpanId';
3132
import { getRequestSpanData } from './utils/getRequestSpanData';
3233
import type { SpanNode } from './utils/groupSpansWithParents';
3334
import { getLocalParentId, groupSpansWithParents } from './utils/groupSpansWithParents';
@@ -255,7 +256,7 @@ export function createTransactionForOtelSpan(span: ReadableSpan): TransactionEve
255256
// even if `span.parentSpanId` is set
256257
// this is the case when we are starting a new trace, where we have a virtual span based on the propagationContext
257258
// We only want to continue the traceId in this case, but ignore the parent span
258-
const parent_span_id = span.parentSpanId;
259+
const parent_span_id = getParentSpanId(span);
259260

260261
const status = mapStatus(span);
261262

@@ -321,8 +322,9 @@ function createAndFinishSpanForOtelSpan(node: SpanNode, spans: SpanJSON[], sentS
321322

322323
const span_id = span.spanContext().spanId;
323324
const trace_id = span.spanContext().traceId;
325+
const parentSpanId = getParentSpanId(span);
324326

325-
const { attributes, startTime, endTime, parentSpanId, links } = span;
327+
const { attributes, startTime, endTime, links } = span;
326328

327329
const { op, description, data, origin = 'manual' } = getSpanData(span);
328330
const allData = {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
2+
3+
/**
4+
* Get the parent span id from a span.
5+
* In OTel v1, the parent span id is accessed as `parentSpanId`
6+
* In OTel v2, the parent span id is accessed as `spanId` on the `parentSpanContext`
7+
*/
8+
export function getParentSpanId(span: ReadableSpan): string | undefined {
9+
if ('parentSpanId' in span) {
10+
return span.parentSpanId as string | undefined;
11+
} else if ('parentSpanContext' in span) {
12+
return (span.parentSpanContext as { spanId?: string } | undefined)?.spanId;
13+
}
14+
15+
return undefined;
16+
}

packages/opentelemetry/src/utils/groupSpansWithParents.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
22
import { SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE } from '../semanticAttributes';
3+
import { getParentSpanId } from './getParentSpanId';
34

45
export interface SpanNode {
56
id: string;
@@ -33,7 +34,7 @@ export function getLocalParentId(span: ReadableSpan): string | undefined {
3334
const parentIsRemote = span.attributes[SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE] === true;
3435
// If the parentId is the trace parent ID, we pretend it's undefined
3536
// As this means the parent exists somewhere else
36-
return !parentIsRemote ? span.parentSpanId : undefined;
37+
return !parentIsRemote ? getParentSpanId(span) : undefined;
3738
}
3839

3940
function createOrUpdateSpanNodeAndRefs(nodeMap: SpanMap, span: ReadableSpan): void {

packages/opentelemetry/src/utils/spanTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SpanKind, SpanStatus } from '@opentelemetry/api';
22
import type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base';
33
import type { AbstractSpan } from '../types';
4+
import { getParentSpanId } from './getParentSpanId';
45

56
/**
67
* Check if a given span has attributes.
@@ -55,7 +56,7 @@ export function spanHasParentId<SpanType extends AbstractSpan>(
5556
span: SpanType,
5657
): span is SpanType & { parentSpanId: string } {
5758
const castSpan = span as ReadableSpan;
58-
return !!castSpan.parentSpanId;
59+
return !!getParentSpanId(castSpan);
5960
}
6061

6162
/**

0 commit comments

Comments
 (0)