Skip to content

Conversation

kentcdodds
Copy link
Member

Implement request correlation to link frontend and backend errors in Sentry for improved tracing and diagnosis.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: Sentry Scope Update Race Condition

The middleware attempts to set a correlation ID in the Sentry scope using an asynchronous dynamic import and withScope. However, next() is called synchronously without awaiting the completion of this operation. This creates a race condition where the Sentry scope changes are applied after the request has proceeded to downstream handlers, rendering them ineffective for the current request's Sentry scope. Consequently, the correlation ID is not properly associated with the request's Sentry events.

packages/workshop-app/server/index.ts#L198-L215

// Add correlation ID middleware before React Router handler
app.use((req, res, next) => {
const correlationId = req.headers['x-correlation-id'] as string
if (correlationId && ENV.EPICSHOP_IS_PUBLISHED) {
// Add correlation ID to current Sentry scope
import('@sentry/react-router').then(({ withScope }) => {
withScope((scope) => {
scope.setTag('correlationId', correlationId)
scope.setExtra('correlationId', correlationId)
scope.setContext('correlation', {
id: correlationId,
timestamp: new Date().toISOString(),
})
})
})
}
next()
})

Fix in CursorFix in Web


Bug: Sentry Hook Fails to Retrieve Correlation ID

The beforeSend hook incorrectly attempts to retrieve the correlation ID from Sentry.getActiveSpan()?.getAttribute('correlationId'). Correlation IDs are set on the Sentry scope (as tags, extras, or context) elsewhere, not as span attributes, causing this retrieval to always return undefined and preventing the ID from being added to server-side events.

packages/workshop-app/instrument.js#L39-L40

// Add correlation ID to server-side events
const correlationId = Sentry.getActiveSpan()?.getAttribute('correlationId')

Fix in CursorFix in Web


Bug: Concurrent Correlation ID Overwrite Bug

Race condition in correlation ID management: The setupRequestCorrelation function generates a new correlation ID for each fetch request and stores it in a shared global variable. Concurrent requests overwrite this variable, causing incorrect correlation IDs to be associated with requests and Sentry events, thus breaking frontend-backend trace correlation.

packages/workshop-app/app/utils/monitoring.client.ts#L46-L66

// Set up request correlation for React Router requests
export function setupRequestCorrelation() {
if (!ENV.EPICSHOP_IS_PUBLISHED) return
// Intercept React Router requests by patching fetch
const originalFetch = window.fetch
window.fetch = function(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
const correlationId = generateCorrelationId()
setCorrelationId(correlationId)
const headers = new Headers(init?.headers)
headers.set(CORRELATION_ID_HEADER, correlationId)
const newInit = {
...init,
headers,
}
return originalFetch(input, newInit)
}
}

Fix in CursorFix in Web


BugBot free trial expires on July 22, 2025
Learn more in the Cursor dashboard.

Was this report helpful? Give feedback by reacting with 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants