Skip to content
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,5 +1,5 @@
import { SEMATTRS_HTTP_TARGET } from '@opentelemetry/semantic-conventions';
import { GLOBAL_OBJ, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, type Span, type SpanAttributes } from '@sentry/core';
import { getClient, GLOBAL_OBJ, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, type Span, type SpanAttributes } from '@sentry/core';
import { isSentryRequestSpan } from '@sentry/opentelemetry';
import { ATTR_NEXT_SPAN_TYPE } from '../nextSpanAttributes';
import { TRANSACTION_ATTR_SHOULD_DROP_TRANSACTION } from '../span-attributes-with-logic-attached';
Expand All @@ -15,6 +15,12 @@ const globalWithInjectedValues = GLOBAL_OBJ as typeof GLOBAL_OBJ & {
* 2. Requests to Sentry ingest (after rewrite)
*/
export function dropMiddlewareTunnelRequests(span: Span, attrs: SpanAttributes | undefined): void {
// When the user brings their own OTel setup (skipOpenTelemetrySetup: true), we should not
// mutate their spans with Sentry-internal attributes as it pollutes their tracing backends.
if ((getClient()?.getOptions() as { skipOpenTelemetrySetup?: boolean } | undefined)?.skipOpenTelemetrySetup) {
return;
}

// Only filter middleware spans or HTTP fetch spans
const isMiddleware = attrs?.[ATTR_NEXT_SPAN_TYPE] === 'Middleware.execute';
// The fetch span could be originating from rewrites re-writing a tunnel request
Expand Down
7 changes: 7 additions & 0 deletions packages/nextjs/src/common/utils/tracingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PropagationContext, Span, SpanAttributes } from '@sentry/core';
import {
debug,
getActiveSpan,
getClient,
getRootSpan,
GLOBAL_OBJ,
Scope,
Expand Down Expand Up @@ -112,6 +113,12 @@ export function escapeNextjsTracing<T>(cb: () => T): T {
* Drops the entire span tree this function was called in, if it was a span tree created by Next.js.
*/
export function dropNextjsRootContext(): void {
// When the user brings their own OTel setup (skipOpenTelemetrySetup: true), we should not
// mutate their spans with Sentry-internal attributes like `sentry.drop_transaction`
if ((getClient()?.getOptions() as { skipOpenTelemetrySetup?: boolean } | undefined)?.skipOpenTelemetrySetup) {
return;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix PR lacks regression tests for the change

Low Severity

This fix PR adds early returns in dropNextjsRootContext() and dropMiddlewareTunnelRequests() when skipOpenTelemetrySetup is enabled, but includes no unit, integration, or e2e tests verifying the regression. Per the review rules, fix PRs need at least one test that would have failed before the fix and passes after it. Adding tests for both functions to confirm spans are not mutated when skipOpenTelemetrySetup is true would help prevent future regressions.

Additional Locations (1)

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot


const nextJsOwnedSpan = getActiveSpan();
if (nextJsOwnedSpan) {
const rootSpan = getRootSpan(nextJsOwnedSpan);
Expand Down
Loading