Skip to content

feat(core): Fix span scope handling & transaction setting #10886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Route } from '@playwright/test';
import { expect } from '@playwright/test';
import type { Event, Span, SpanContext, Transaction } from '@sentry/types';
import type { Event, SpanContext, SpanJSON } from '@sentry/types';

import { sentryTest } from '../../../../utils/fixtures';
import {
Expand All @@ -9,8 +9,8 @@ import {
shouldSkipTracingTest,
} from '../../../../utils/helpers';

type TransactionJSON = ReturnType<Transaction['toJSON']> & {
spans: ReturnType<Span['toJSON']>[];
type TransactionJSON = SpanJSON & {
spans: SpanJSON[];
contexts: SpanContext;
platform: string;
type: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { waitForError } from '../event-proxy-server';

test('sends an error', async ({ page }) => {
const errorPromise = waitForError('angular-17', async errorEvent => {
return !errorEvent?.transaction;
return !errorEvent.type;
});

await page.goto(`/`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ test('Should record exceptions and transactions for faulty route handlers', asyn
expect(routehandlerTransaction.contexts?.trace?.op).toBe('http.server');

expect(routehandlerError.exception?.values?.[0].value).toBe('route-handler-error');
expect(routehandlerError.tags?.transaction).toBe('PUT /route-handlers/[param]/error');
expect(routehandlerError.transaction).toBe('PUT /route-handlers/[param]/error');
});

test.describe('Edge runtime', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ test.describe('server-side errors', () => {
}),
);

expect(errorEvent.tags).toMatchObject({
runtime: 'node',
transaction: 'GET /server-route-error',
});
expect(errorEvent.transaction).toEqual('GET /server-route-error');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ test.describe('server-side errors', () => {
}),
);

expect(errorEvent.tags).toMatchObject({
runtime: 'node',
transaction: 'GET /server-route-error',
});
expect(errorEvent.transaction).toEqual('GET /server-route-error');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { waitForError } from '../event-proxy-server';

test('sends an error', async ({ page }) => {
const errorPromise = waitForError('vue-3', async errorEvent => {
return !errorEvent?.transaction;
return !errorEvent.type;
});

await page.goto(`/`);
Expand Down
23 changes: 11 additions & 12 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span |
scope,
});

if (activeSpan) {
// eslint-disable-next-line deprecation/deprecation
scope.setSpan(activeSpan);
}

return handleCallbackErrors(
() => callback(activeSpan),
() => {
Expand Down Expand Up @@ -91,6 +96,11 @@ export function startSpanManual<T>(
scope,
});

if (activeSpan) {
// eslint-disable-next-line deprecation/deprecation
scope.setSpan(activeSpan);
}

function finishAndSetSpan(): void {
activeSpan && activeSpan.end();
}
Expand Down Expand Up @@ -141,16 +151,11 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {

const scope = context.scope || getCurrentScope();

// Even though we don't actually want to make this span active on the current scope,
// we need to make it active on a temporary scope that we use for event processing
// as otherwise, it won't pick the correct span for the event when processing it
const temporaryScope = scope.clone();

return createChildSpanOrTransaction(hub, {
parentSpan,
spanContext,
forceTransaction: context.forceTransaction,
scope: temporaryScope,
scope,
});
}

Expand Down Expand Up @@ -319,12 +324,6 @@ function createChildSpanOrTransaction(
});
}

// We always set this as active span on the scope
// In the case of this being an inactive span, we ensure to pass a detached scope in here in the first place
// But by having this here, we can ensure that the lookup through `getCapturedScopesOnSpan` results in the correct scope & span combo
// eslint-disable-next-line deprecation/deprecation
scope.setSpan(span);

setCapturedScopesOnSpan(span, scope, isolationScope);

return span;
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/utils/applyScopeDataToEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ function applySpanToEvent(event: Event, span: Span): void {
dynamicSamplingContext: getDynamicSamplingContextFromSpan(span),
...event.sdkProcessingMetadata,
};

const transactionName = spanToJSON(rootSpan).description;
if (transactionName) {
event.tags = { transaction: transactionName, ...event.tags };
if (transactionName && !event.transaction) {
event.transaction = transactionName;
}
}
}
Expand Down
30 changes: 12 additions & 18 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,7 @@ describe('startSpan', () => {
},
});
expect(outerTransaction?.spans).toEqual([{ name: 'inner span', id: expect.any(String) }]);
expect(outerTransaction?.tags).toEqual({
transaction: 'outer transaction',
});
expect(outerTransaction?.transaction).toEqual('outer transaction');
expect(outerTransaction?.sdkProcessingMetadata).toEqual({
dynamicSamplingContext: {
environment: 'production',
Expand All @@ -386,9 +384,7 @@ describe('startSpan', () => {
},
});
expect(innerTransaction?.spans).toEqual([{ name: 'inner span 2', id: expect.any(String) }]);
expect(innerTransaction?.tags).toEqual({
transaction: 'inner transaction',
});
expect(innerTransaction?.transaction).toEqual('inner transaction');
expect(innerTransaction?.sdkProcessingMetadata).toEqual({
dynamicSamplingContext: {
environment: 'production',
Expand Down Expand Up @@ -643,9 +639,7 @@ describe('startSpanManual', () => {
},
});
expect(outerTransaction?.spans).toEqual([{ name: 'inner span', id: expect.any(String) }]);
expect(outerTransaction?.tags).toEqual({
transaction: 'outer transaction',
});
expect(outerTransaction?.transaction).toEqual('outer transaction');
expect(outerTransaction?.sdkProcessingMetadata).toEqual({
dynamicSamplingContext: {
environment: 'production',
Expand All @@ -669,9 +663,7 @@ describe('startSpanManual', () => {
},
});
expect(innerTransaction?.spans).toEqual([{ name: 'inner span 2', id: expect.any(String) }]);
expect(innerTransaction?.tags).toEqual({
transaction: 'inner transaction',
});
expect(innerTransaction?.transaction).toEqual('inner transaction');
expect(innerTransaction?.sdkProcessingMetadata).toEqual({
dynamicSamplingContext: {
environment: 'production',
Expand Down Expand Up @@ -854,9 +846,7 @@ describe('startInactiveSpan', () => {
},
});
expect(outerTransaction?.spans).toEqual([{ name: 'inner span', id: expect.any(String) }]);
expect(outerTransaction?.tags).toEqual({
transaction: 'outer transaction',
});
expect(outerTransaction?.transaction).toEqual('outer transaction');
expect(outerTransaction?.sdkProcessingMetadata).toEqual({
dynamicSamplingContext: {
environment: 'production',
Expand All @@ -880,9 +870,7 @@ describe('startInactiveSpan', () => {
},
});
expect(innerTransaction?.spans).toEqual([]);
expect(innerTransaction?.tags).toEqual({
transaction: 'inner transaction',
});
expect(innerTransaction?.transaction).toEqual('inner transaction');
expect(innerTransaction?.sdkProcessingMetadata).toEqual({
dynamicSamplingContext: {
environment: 'production',
Expand Down Expand Up @@ -948,9 +936,13 @@ describe('startInactiveSpan', () => {

let span: Span | undefined;

const scope = getCurrentScope();
scope.setTag('outer', 'foo');

withScope(scope => {
scope.setTag('scope', 1);
span = startInactiveSpan({ name: 'my-span' });
scope.setTag('scope_after_span', 2);
});

withScope(scope => {
Expand All @@ -964,7 +956,9 @@ describe('startInactiveSpan', () => {
expect(beforeSendTransaction).toHaveBeenCalledWith(
expect.objectContaining({
tags: expect.objectContaining({
outer: 'foo',
scope: 1,
scope_after_span: 2,
}),
}),
expect.anything(),
Expand Down
7 changes: 3 additions & 4 deletions packages/node-experimental/test/integration/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ describe('Integration | Scope', () => {
tag2: 'val2',
tag3: 'val3',
tag4: 'val4',
...(enableTracing ? { transaction: 'outer' } : {}),
},
...(enableTracing ? { transaction: 'outer' } : {}),
}),
{
event_id: expect.any(String),
Expand Down Expand Up @@ -118,7 +118,6 @@ describe('Integration | Scope', () => {
tag2: 'val2',
tag3: 'val3',
tag4: 'val4',
transaction: 'outer',
},
timestamp: expect.any(Number),
transaction: 'outer',
Expand Down Expand Up @@ -203,8 +202,8 @@ describe('Integration | Scope', () => {
tag2: 'val2a',
tag3: 'val3a',
tag4: 'val4a',
...(enableTracing ? { transaction: 'outer' } : {}),
},
...(enableTracing ? { transaction: 'outer' } : {}),
}),
{
event_id: expect.any(String),
Expand All @@ -229,8 +228,8 @@ describe('Integration | Scope', () => {
tag2: 'val2b',
tag3: 'val3b',
tag4: 'val4b',
...(enableTracing ? { transaction: 'outer' } : {}),
},
...(enableTracing ? { transaction: 'outer' } : {}),
}),
{
event_id: expect.any(String),
Expand Down
17 changes: 12 additions & 5 deletions packages/node-experimental/test/integration/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ describe('Integration | Transactions', () => {
tags: {
'outer.tag': 'test value',
'test.tag': 'test value',
transaction: 'test name',
},
timestamp: expect.any(Number),
transaction: 'test name',
Expand Down Expand Up @@ -267,7 +266,9 @@ describe('Integration | Transactions', () => {
}),
spans: [expect.any(Object), expect.any(Object)],
start_timestamp: expect.any(Number),
tags: { 'test.tag': 'test value', transaction: 'test name' },
tags: {
'test.tag': 'test value',
},
timestamp: expect.any(Number),
transaction: 'test name',
transaction_info: { source: 'task' },
Expand Down Expand Up @@ -310,7 +311,9 @@ describe('Integration | Transactions', () => {
}),
spans: [expect.any(Object), expect.any(Object)],
start_timestamp: expect.any(Number),
tags: { 'test.tag': 'test value b', transaction: 'test name b' },
tags: {
'test.tag': 'test value b',
},
timestamp: expect.any(Number),
transaction: 'test name b',
transaction_info: { source: 'custom' },
Expand Down Expand Up @@ -417,7 +420,9 @@ describe('Integration | Transactions', () => {
}),
spans: [expect.any(Object), expect.any(Object)],
start_timestamp: expect.any(Number),
tags: { 'test.tag': 'test value', transaction: 'test name' },
tags: {
'test.tag': 'test value',
},
timestamp: expect.any(Number),
transaction: 'test name',
type: 'transaction',
Expand Down Expand Up @@ -456,7 +461,9 @@ describe('Integration | Transactions', () => {
}),
spans: [expect.any(Object), expect.any(Object)],
start_timestamp: expect.any(Number),
tags: { 'test.tag': 'test value b', transaction: 'test name b' },
tags: {
'test.tag': 'test value b',
},
timestamp: expect.any(Number),
transaction: 'test name b',
type: 'transaction',
Expand Down
4 changes: 2 additions & 2 deletions packages/opentelemetry/src/setupEventContextTrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export function setupEventContextTrace(client: Client): void {

const rootSpan = getRootSpan(span);
const transactionName = spanHasName(rootSpan) ? rootSpan.name : undefined;
if (transactionName) {
event.tags = { transaction: transactionName, ...event.tags };
if (transactionName && !event.transaction) {
event.transaction = transactionName;
}

return event;
Expand Down
9 changes: 1 addition & 8 deletions packages/opentelemetry/src/spanExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,7 @@ function createTransactionForOtelSpan(span: ReadableSpan): Transaction {
});

if (capturedSpanScopes) {
// Ensure the `transaction` tag is correctly set on the transaction event
const scope = capturedSpanScopes.scope.clone();
scope.addEventProcessor(event => {
event.tags = { transaction: description, ...event.tags };
return event;
});

setCapturedScopesOnTransaction(transaction, scope, capturedSpanScopes.isolationScope);
setCapturedScopesOnTransaction(transaction, capturedSpanScopes.scope, capturedSpanScopes.isolationScope);
}

return transaction;
Expand Down
Loading