From 1f56a451a0fcb7adaa5dd7dc53325a90b3437493 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 20 Nov 2023 18:14:17 -0500 Subject: [PATCH] ref(bun): Use Sentry.continueTrace in Bun --- packages/bun/src/integrations/bunserver.ts | 90 +++++++++++----------- 1 file changed, 43 insertions(+), 47 deletions(-) diff --git a/packages/bun/src/integrations/bunserver.ts b/packages/bun/src/integrations/bunserver.ts index 915856f473db..d20953628544 100644 --- a/packages/bun/src/integrations/bunserver.ts +++ b/packages/bun/src/integrations/bunserver.ts @@ -1,6 +1,6 @@ -import { captureException, getCurrentHub, runWithAsyncContext, startSpan, Transaction } from '@sentry/core'; +import { captureException, continueTrace, runWithAsyncContext, startSpan, Transaction } from '@sentry/core'; import type { Integration } from '@sentry/types'; -import { addExceptionMechanism, getSanitizedUrlString, parseUrl, tracingContextFromHeaders } from '@sentry/utils'; +import { addExceptionMechanism, getSanitizedUrlString, parseUrl } from '@sentry/utils'; function sendErrorToSentry(e: unknown): unknown { captureException(e, scope => { @@ -62,22 +62,12 @@ function instrumentBunServeOptions(serveOptions: Parameters[0] serveOptions.fetch = new Proxy(serveOptions.fetch, { apply(fetchTarget, fetchThisArg, fetchArgs: Parameters) { return runWithAsyncContext(() => { - const hub = getCurrentHub(); - const request = fetchArgs[0]; const upperCaseMethod = request.method.toUpperCase(); if (upperCaseMethod === 'OPTIONS' || upperCaseMethod === 'HEAD') { return fetchTarget.apply(fetchThisArg, fetchArgs); } - const sentryTrace = request.headers.get('sentry-trace') || ''; - const baggage = request.headers.get('baggage'); - const { traceparentData, dynamicSamplingContext, propagationContext } = tracingContextFromHeaders( - sentryTrace, - baggage, - ); - hub.getScope().setPropagationContext(propagationContext); - const parsedUrl = parseUrl(request.url); const data: Record = { 'http.request.method': request.method || 'GET', @@ -87,43 +77,49 @@ function instrumentBunServeOptions(serveOptions: Parameters[0] } const url = getSanitizedUrlString(parsedUrl); - return startSpan( - { - op: 'http.server', - name: `${request.method} ${parsedUrl.path || '/'}`, - origin: 'auto.http.bun.serve', - ...traceparentData, - data, - metadata: { - source: 'url', - dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext, - request: { - url, - method: request.method, - headers: request.headers.toJSON(), + + return continueTrace( + { sentryTrace: request.headers.get('sentry-trace') || '', baggage: request.headers.get('baggage') }, + ctx => { + return startSpan( + { + op: 'http.server', + name: `${request.method} ${parsedUrl.path || '/'}`, + origin: 'auto.http.bun.serve', + ...ctx, + data, + metadata: { + ...ctx.metadata, + source: 'url', + request: { + url, + method: request.method, + headers: request.headers.toJSON(), + }, + }, }, - }, - }, - async span => { - try { - const response = await (fetchTarget.apply(fetchThisArg, fetchArgs) as ReturnType< - typeof serveOptions.fetch - >); - if (response && response.status) { - span?.setHttpStatus(response.status); - span?.setData('http.response.status_code', response.status); - if (span instanceof Transaction) { - span.setContext('response', { - headers: response.headers.toJSON(), - status_code: response.status, - }); + async span => { + try { + const response = await (fetchTarget.apply(fetchThisArg, fetchArgs) as ReturnType< + typeof serveOptions.fetch + >); + if (response && response.status) { + span?.setHttpStatus(response.status); + span?.setData('http.response.status_code', response.status); + if (span instanceof Transaction) { + span.setContext('response', { + headers: response.headers.toJSON(), + status_code: response.status, + }); + } + } + return response; + } catch (e) { + sendErrorToSentry(e); + throw e; } - } - return response; - } catch (e) { - sendErrorToSentry(e); - throw e; - } + }, + ); }, ); });