8000 fix(opentelemetry): Use scope span capturing from core (#11475) · basarat/sentry-javascript@a1e4efe · GitHub
[go: up one dir, main page]

Skip to content

Commit a1e4efe

Browse files
authored
fix(opentelemetry): Use scope span capturing from core (getsentry#11475)
To ensure we can use this in an isomorphic way. This means we do not rely on this being a mutable object anymore, but we need to ensure to update it (in http integration) on both the span _and_ for the current context.
1 parent 83f7cce commit a1e4efe

File tree

8 files changed

+33
-46
lines changed

8 files changed

+33
-46
lines changed

packages/core/src/tracing/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { setCapturedScopesOnSpan, getCapturedScopesOnSpan } from './utils';
12
export { addTracingExtensions } from './hubextensions';
23
export { startIdleSpan, TRACING_DEFAULTS } from './idleSpan';
34
export { SentrySpan } from './sentrySpan';

packages/node/src/integrations/http.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ import { SpanKind } from '@opentelemetry/api';
44
import { registerInstrumentations } from '@opentelemetry/instrumentation';
55
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
66

7-
import { addBreadcrumb, defineIntegration, getIsolationScope, isSentryRequestUrl, spanToJSON } from '@sentry/core';
7+
import {
8+
addBreadcrumb,
9+
defineIntegration,
10+
getCapturedScopesOnSpan,
11+
getCurrentScope,
12+
getIsolationScope,
13+
isSentryRequestUrl,
14+
setCapturedScopesOnSpan,
15+
spanToJSON,
16+
} from '@sentry/core';
817
import { _INTERNAL, getClient, getSpanKind } from '@sentry/opentelemetry';
918
import type { IntegrationFn } from '@sentry/types';
1019

@@ -88,15 +97,20 @@ const _httpIntegration = ((options: HttpOptions = {}) => {
8897
return;
8998
}
9099

100+
const scopes = getCapturedScopesOnSpan(span);
101+
91102
// Update the isolation scope, isolate this request
92-
const isolationScope = getIsolationScope().clone();
103+
const isolationScope = (scopes.isolationScope || getIsolationScope()).clone();
104+
const scope = scopes.scope || getCurrentScope();
105+
93106
isolationScope.setSDKProcessingMetadata({ request: req });
94107

95108
const client = getClient<NodeClient>();
96109
if (client && client.getOptions().autoSessionTracking) {
97110
isolationScope.setRequestSession({ status: 'ok' });
98111
}
99112
setIsolationScope(isolationScope);
113+
setCapturedScopesOnSpan(span, scope, isolationScope);
100114

101115
// attempt to update the scope's `transactionName` based on the request URL
102116
// Ideally, framework instrumentations coming after the HttpInstrumentation

packages/node/test/integration/scope.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { getCurrentScope } from '@sentry/core';
2-
import { getClient, getSpanScopes } from '@sentry/opentelemetry';
1+
import { getCapturedScopesOnSpan, getCurrentScope } from '@sentry/core';
2+
import { getClient } from '@sentry/opentelemetry';
33
import { clearGlobalScope } from '../../../core/test/lib/clear-global-scope';
44

55
import * as Sentry from '../../src/';
@@ -42,7 +42,7 @@ describe('Integration | Scope', () => {
4242
scope2.setTag('tag3', 'val3');
4343

4444
Sentry.startSpan({ name: 'outer' }, span => {
45-
expect(getSpanScopes(span)?.scope).toBe(enableTracing ? scope2 : undefined);
45+
expect(getCapturedScopesOnSpan(span)?.scope).toBe(enableTracing ? scope2 : undefined);
4646

4747
spanId = span.spanContext().spanId;
4848
traceId = span.spanContext().traceId;

packages/opentelemetry/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export type { OpenTelemetryClient } from './types';
44
export { wrapClientClass } from './custom/client';
55

66
export { getSpanKind } from './utils/getSpanKind';
7-
export { getSpanScopes } from './utils/spanData';
87

98
export { getScopesFromContext } from './utils/contextData';
109

packages/opentelemetry/src/spanExporter.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import type { Span } from '@opentelemetry/api';
22
import { SpanKind } from '@opentelemetry/api';
33
import type { ReadableSpan } from '@opentelemetry/sdk-trace-base';
44
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
5-
import { captureEvent, getMetricSummaryJsonForSpan, timedEventsToMeasurements } from '@sentry/core';
5+
import {
6+
captureEvent,
7+
getCapturedScopesOnSpan,
8+
getMetricSummaryJsonForSpan,
9+
timedEventsToMeasurements,
10+
} from '@sentry/core';
611
import {
712
SEMANTIC_ATTRIBUTE_SENTRY_OP,
813
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
@@ -23,7 +28,6 @@ import { getLocalParentId } from './utils/groupSpansWithParents';
2328
import { groupSpansWithParents } from './utils/groupSpansWithParents';
2429
import { mapStatus } from './utils/mapStatus';
2530
import { parseSpanDescription } from './utils/parseSpanDescription';
26-
import { getSpanScopes } from './utils/spanData';
2731

2832
type SpanNodeCompleted = SpanNode & { span: ReadableSpan };
2933

@@ -176,7 +180,7 @@ function parseSpan(span: ReadableSpan): { op?: string; origin?: SpanOrigin; sour
176180

177181
function createTransactionForOtelSpan(span: ReadableSpan): TransactionEvent {
178182
const { op, description, data, origin = 'manual', source } = getSpanData(span);
179-
const capturedSpanScopes = getSpanScopes(span);
183+
const capturedSpanScopes = getCapturedScopesOnSpan(span as unknown as Span);
180184

181185
const sampleRate = span.attributes[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE] as number | undefined;
182186

@@ -220,8 +224,8 @@ function createTransactionForOtelSpan(span: ReadableSpan): TransactionEvent {
220224
type: 'transaction',
221225
sdkProcessingMetadata: {
222226
...dropUndefinedKeys({
223-
capturedSpanScope: capturedSpanScopes?.scope,
224-
capturedSpanIsolationScope: capturedSpanScopes?.isolationScope,
227+
capturedSpanScope: capturedSpanScopes.scope,
228+
capturedSpanIsolationScope: capturedSpanScopes.isolationScope,
225229
sampleRate,
226230
dynamicSamplingContext: getDynamicSamplingContextFromSpan(span),
227231
}),

packages/opentelemetry/src/spanProcessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import {
88
getDefaultIsolationScope,
99
logSpanEnd,
1010
logSpanStart,
11+
setCapturedScopesOnSpan,
1112
} from '@sentry/core';
1213
import { SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE } from './semanticAttributes';
1314
import { SentrySpanExporter } from './spanExporter';
1415
import { getScopesFromContext } from './utils/contextData';
1516
import { setIsSetup } from './utils/setupCheck';
16-
import { setSpanScopes } from './utils/spanData';
1717

1818
function onSpanStart(span: Span, parentContext: Context): void {
1919
// This is a reliable way to get the parent span - because this is exactly how the parent is identified in the OTEL SDK
@@ -42,7 +42,7 @@ function onSpanStart(span: Span, parentContext: Context): void {
4242

4343
// We need the scope at time of span creation in order to apply it to the event when the span is finished
4444
if (scopes) {
45-
setSpanScopes(span, scopes);
45+
setCapturedScopesOnSpan(span, scopes.scope, scopes.isolationScope);
4646
}
4747

4848
logSpanStart(span);

packages/opentelemetry/src/utils/spanData.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/opentelemetry/test/integration/scope.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
captureException,
3+
getCapturedScopesOnSpan,
34
getClient,
45
getCurrentScope,
56
getIsolationScope,
@@ -9,7 +10,6 @@ import {
910
} from '@sentry/core';
1011

1112
import { startSpan } from '../../src/trace';
12-
import { getSpanScopes } from '../../src/utils/spanData';
1313
import type { TestClientInterface } from '../helpers/TestClient';
1414
import { cleanupOtel, mockSdkInit } from '../helpers/mockSdkInit';
1515

@@ -49,7 +49,7 @@ describe('Integration | Scope', () => {
4949
scope2.setTag('tag3', 'val3');
5050

5151
startSpan({ name: 'outer' }, span => {
52-
expect(getSpanScopes(span)?.scope).toBe(enableTracing ? scope2 : undefined);
52+
expect(getCapturedScopesOnSpan(span)?.scope).toBe(enableTracing ? scope2 : undefined);
5353

5454
spanId = span.spanContext().spanId;
5555
traceId = span.spanContext().traceId;

0 commit comments

Comments
 (0)
0