Skip to content

feat: Ensure getRootSpan() does not rely on transaction #10979

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 5 commits into from
Mar 11, 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
Expand Up @@ -29,7 +29,6 @@ const client = new BrowserClient({
transport: makeFetchTransport,
stackParser: defaultStackParser,
integrations,
debug: true,
});

const hub = new Hub(client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ window.Sentry = Sentry;

Sentry.init({
dsn: 'https://[email protected]/1337',
// We assert on the debug message, so we need to enable debug mode
debug: true,
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { expect } from '@playwright/test';
import type { Event } from '@sentry/types';

import { sentryTest } from '../../../../utils/fixtures';
import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers';
import {
envelopeRequestParser,
shouldSkipTracingTest,
waitForTransactionRequestOnUrl,
} from '../../../../utils/helpers';

sentryTest('should send a transaction in an envelope', async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestPath({ testDir: __dirname });
const transaction = await getFirstSentryEnvelopeRequest<Event>(page, url);
const req = await waitForTransactionRequestOnUrl(page, url);
const transaction = envelopeRequestParser(req);

expect(transaction.transaction).toBe('parent_span');
expect(transaction.spans).toBeDefined();
Expand All @@ -22,7 +26,8 @@ sentryTest('should report finished spans as children of the root transaction', a
}

const url = await getLocalTestPath({ testDir: __dirname });
const transaction = await getFirstSentryEnvelopeRequest<Event>(page, url);
const req = await waitForTransactionRequestOnUrl(page, url);
const transaction = envelopeRequestParser(req);

expect(transaction.spans).toHaveLength(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ Sentry.init({
dsn: 'https://[email protected]/1337',
tracesSampleRate: 1.0,
normalizeDepth: 10,
debug: true,
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Sentry.init({
sampleRate: 0,
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 0.0,
debug: true,

integrations: [window.Replay, Sentry.replayCanvasIntegration({ enableManualSnapshot: true })],
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Sentry.init({
sampleRate: 0,
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 0.0,
debug: true,

integrations: [window.Replay, Sentry.replayCanvasIntegration()],
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Sentry.init({
sampleRate: 0,
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 0.0,
debug: true,

integrations: [Sentry.replayCanvasIntegration(), window.Replay],
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Sentry.init({
sampleRate: 0,
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 0.0,
debug: true,

integrations: [window.Replay, Sentry.replayCanvasIntegration()],
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Sentry.init({
sampleRate: 0,
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 0.0,
debug: true,

integrations: [window.Replay],
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ Sentry.init({
username: 'user1337',
},
},
debug: true,
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Sentry.init({
integrations: [Sentry.browserTracingIntegration()],
environment: 'production',
tracesSampleRate: 1,
debug: true,
});

Sentry.setUser({ id: 'user123' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Sentry.init({
tracePropagationTargets: [/.*/],
environment: 'production',
tracesSampleRate: 1,
debug: true,
});

const scope = Sentry.getCurrentScope();
Expand Down
3 changes: 3 additions & 0 deletions dev-packages/rollup-utils/plugins/bundlePlugins.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ export function makeTerserPlugin() {
// For v7 backwards-compatibility we need to access txn._frozenDynamicSamplingContext
// TODO (v8): Remove this reserved word
'_frozenDynamicSamplingContext',
// These are used to keep span relationships
'_sentryRootSpan',
'_sentryChildSpans',
],
},
},
Expand Down
4 changes: 0 additions & 4 deletions packages/angular/src/tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,6 @@ export class TraceService implements OnDestroy {
}

const rootSpan = getRootSpan(activeSpan);
if (!rootSpan) {
this._pageloadOngoing = false;
return false;
}

this._pageloadOngoing = spanToJSON(rootSpan).op === 'pageload';
return this._pageloadOngoing;
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/index.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export {
linkedErrorsIntegration,
setMeasurement,
getActiveSpan,
getRootSpan,
startSpan,
startInactiveSpan,
startSpanManual,
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/index.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export declare const startSpan: typeof clientSdk.startSpan;
export declare const startInactiveSpan: typeof clientSdk.startInactiveSpan;
export declare const startSpanManual: typeof clientSdk.startSpanManual;
export declare const withActiveSpan: typeof clientSdk.withActiveSpan;
export declare const getRootSpan: typeof clientSdk.getRootSpan;
export declare const Span: clientSdk.Span;

export declare const metrics: typeof clientSdk.metrics & typeof serverSdk.metrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ addTracingExtensions();

export {
getActiveSpan,
getRootSpan,
startSpan,
startInactiveSpan,
startSpanManual,
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.bundle.tracing.replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ addTracingExtensions();

export {
getActiveSpan,
getRootSpan,
startSpan,
startInactiveSpan,
startSpanManual,
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.bundle.tracing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ addTracingExtensions();

export {
getActiveSpan,
getRootSpan,
startSpan,
startInactiveSpan,
startSpanManual,
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type { RequestInstrumentationOptions } from '@sentry-internal/tracing';
export {
addTracingExtensions,
getActiveSpan,
getRootSpan,
startSpan,
startInactiveSpan,
startSpanManual,
Expand Down
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export {
withMonitor,
setMeasurement,
getActiveSpan,
getRootSpan,
startSpan,
startInactiveSpan,
startSpanManual,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ export {
spanToTraceContext,
getSpanDescendants,
getStatusMessage,
getRootSpan,
} from './utils/spanUtils';
export { getRootSpan } from './utils/getRootSpan';
export { applySdkMetadata } from './utils/sdkMetadata';
export { DEFAULT_ENVIRONMENT } from './constants';
/* eslint-disable deprecation/deprecation */
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/server-runtime-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import {
getDynamicSamplingContextFromClient,
getDynamicSamplingContextFromSpan,
} from './tracing';
import { getRootSpan } from './utils/getRootSpan';
import { spanToTraceContext } from './utils/spanUtils';
import { getRootSpan, spanToTraceContext } from './utils/spanUtils';

export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> {
platform?: string;
Expand Down Expand Up @@ -256,8 +255,9 @@ export class ServerRuntimeClient<
// eslint-disable-next-line deprecation/deprecation
const span = scope.getSpan();
if (span) {
const samplingContext = getRootSpan(span) ? getDynamicSamplingContextFromSpan(span) : undefined;
return [samplingContext, spanToTraceContext(span)];
const rootSpan = getRootSpan(span);
const samplingContext = getDynamicSamplingContextFromSpan(rootSpan);
return [samplingContext, spanToTraceContext(rootSpan)];
}

const { traceId, spanId, parentSpanId, dsc } = scope.getPropagationContext();
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/tracing/dynamicSamplingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { dropUndefinedKeys } from '@sentry/utils';
import { DEFAULT_ENVIRONMENT } from '../constants';
import { getClient } from '../currentScopes';
import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '../semanticAttributes';
import { getRootSpan } from '../utils/getRootSpan';
import { spanIsSampled, spanToJSON } from '../utils/spanUtils';
import { getRootSpan, spanIsSampled, spanToJSON } from '../utils/spanUtils';

/**
* Creates a dynamic sampling context from a client.
Expand Down
15 changes: 8 additions & 7 deletions packages/core/src/tracing/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
} from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';
import { getRootSpan } from '../utils/spanUtils';
import { SPAN_STATUS_ERROR } from './spanstatus';
import { getActiveTransaction } from './utils';
import { getActiveSpan } from './utils';

let errorsInstrumented = false;

Expand All @@ -29,15 +30,15 @@ export function registerErrorInstrumentation(): void {
}

/**
* If an error or unhandled promise occurs, we mark the active transaction as failed
* If an error or unhandled promise occurs, we mark the active root span as failed
*/
function errorCallback(): void {
// eslint-disable-next-line deprecation/deprecation
const activeTransaction = getActiveTransaction();
if (activeTransaction) {
const activeSpan = getActiveSpan();
const rootSpan = activeSpan && getRootSpan(activeSpan);
if (rootSpan) {
const message = 'internal_error';
DEBUG_BUILD && logger.log(`[Tracing] Transaction: ${message} -> Global error occured`);
activeTransaction.setStatus({ code: SPAN_STATUS_ERROR, message });
DEBUG_BUILD && logger.log(`[Tracing] Root span: ${message} -> Global error occured`);
rootSpan.setStatus({ code: SPAN_STATUS_ERROR, message });
}
}

Expand Down
19 changes: 13 additions & 6 deletions packages/core/src/tracing/measurement.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import type { MeasurementUnit } from '@sentry/types';
import type { MeasurementUnit, Span, Transaction } from '@sentry/types';
import { getRootSpan } from '../utils/spanUtils';

import { getActiveTransaction } from './utils';
import { getActiveSpan } from './utils';

/**
* Adds a measurement to the current active transaction.
*/
export function setMeasurement(name: string, value: number, unit: MeasurementUnit): void {
// eslint-disable-next-line deprecation/deprecation
const transaction = getActiveTransaction();
if (transaction) {
const activeSpan = getActiveSpan();
const rootSpan = activeSpan && getRootSpan(activeSpan);

if (rootSpan && rootSpanIsTransaction(rootSpan)) {
// eslint-disable-next-line deprecation/deprecation
transaction.setMeasurement(name, value, unit);
rootSpan.setMeasurement(name, value, unit);
}
}

function rootSpanIsTransaction(rootSpan: Span): rootSpan is Transaction {
// eslint-disable-next-line deprecation/deprecation
return typeof (rootSpan as Transaction).setMeasurement === 'function';
}
2 changes: 1 addition & 1 deletion packages/core/src/tracing/sentrySpan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import { getClient } from '../currentScopes';
import { DEBUG_BUILD } from '../debug-build';
import { getMetricSummaryJsonForSpan } from '../metrics/metric-summary';
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../semanticAttributes';
import { getRootSpan } from '../utils/getRootSpan';
import {
TRACE_FLAG_NONE,
TRACE_FLAG_SAMPLED,
addChildSpanToSpan,
getRootSpan,
getStatusMessage,
spanTimeInputToSeconds,
spanToJSON,
Expand Down
29 changes: 16 additions & 13 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ export function startSpan<T>(context: StartSpanOptions, callback: (span: Span) =
return handleCallbackErrors(
() => callback(activeSpan),
() => {
// Only update the span status if it hasn't been changed yet
if (activeSpan) {
const { status } = spanToJSON(activeSpan);
if (!status || status === 'ok') {
activeSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
}
// Only update the span status if it hasn't been changed yet, and the span is not yet finished
const { status } = spanToJSON(activeSpan);
if (activeSpan.isRecording() && (!status || status === 'ok')) {
activeSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
}
},
() => activeSpan && activeSpan.end(),
() => activeSpan.end(),
);
});
}
Expand Down Expand Up @@ -97,18 +95,16 @@ export function startSpanManual<T>(context: StartSpanOptions, callback: (span: S
scope.setSpan(activeSpan);

function finishAndSetSpan(): void {
activeSpan && activeSpan.end();
activeSpan.end();
}

return handleCallbackErrors(
() => callback(activeSpan, finishAndSetSpan),
() => {
// Only update the span status if it hasn't been changed yet, and the span is not yet finished
if (activeSpan && activeSpan.isRecording()) {
const { status } = spanToJSON(activeSpan);
if (!status || status === 'ok') {
activeSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
}
const { status } = spanToJSON(activeSpan);
if (activeSpan.isRecording() && (!status || status === 'ok')) {
activeSpan.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
}
},
);
Expand Down Expand Up @@ -315,6 +311,13 @@ function createChildSpanOrTransaction(
});
}

// TODO v8: Technically `startTransaction` can return undefined, which is not reflected by the types
// This happens if tracing extensions have not been added
// In this case, we just want to return a non-recording span
if (!span) {
return new SentryNonRecordingSpan();
}

setCapturedScopesOnSpan(span, scope, isolationScope);

return span;
Expand Down
Loading