Skip to content

Commit 89c1ec0

Browse files
committed
use continueTrace
1 parent 2037de2 commit 89c1ec0

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

packages/astro/src/server/middleware.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { captureException, configureScope, getCurrentHub, runWithAsyncContext, startSpan } from '@sentry/node';
1+
import {
2+
captureException,
3+
configureScope,
4+
continueTrace,
5+
getCurrentHub,
6+
runWithAsyncContext,
7+
startSpan,
8+
} from '@sentry/node';
29
import type { Hub, Span } from '@sentry/types';
310
import {
411
addNonEnumerableProperty,
@@ -87,37 +94,38 @@ async function instrumentRequest(
8794
const method = ctx.request.method;
8895
const headers = ctx.request.headers;
8996

90-
const { dynamicSamplingContext, traceparentData, propagationContext } = tracingContextFromHeaders(
91-
headers.get('sentry-trace') || undefined,
92-
headers.get('baggage'),
93-
);
97+
const traceCtx = continueTrace({
98+
sentryTrace: headers.get('sentry-trace') || undefined,
99+
baggage: headers.get('baggage'),
100+
});
94101

95102
const allHeaders: Record<string, string> = {};
96-
headers.forEach((value, key) => {
97-
allHeaders[key] = value;
98-
});
99103

100-
configureScope(scope => {
101-
scope.setPropagationContext(propagationContext);
104+
if (options.trackHeaders) {
105+
headers.forEach((value, key) => {
106+
allHeaders[key] = value;
107+
});
108+
}
102109

103-
if (options.trackClientIp) {
110+
if (options.trackClientIp) {
111+
configureScope(scope => {
104112
scope.setUser({ ip_address: ctx.clientAddress });
105-
}
106-
});
113+
});
114+
}
107115

108116
try {
109117
// storing res in a variable instead of directly returning is necessary to
110118
// invoke the catch block if next() throws
111119
const res = await startSpan(
112120
{
121+
...traceCtx,
113122
name: `${method} ${interpolateRouteFromUrlAndParams(ctx.url.pathname, ctx.params)}`,
114123
op: 'http.server',
115124
origin: 'auto.http.astro',
116125
status: 'ok',
117-
...traceparentData,
118126
metadata: {
127+
...traceCtx?.metadata,
119128
source: 'route',
120-
dynamicSamplingContext: traceparentData && !dynamicSamplingContext ? {} : dynamicSamplingContext,
121129
},
122130
data: {
123131
method,
@@ -171,7 +179,7 @@ async function instrumentRequest(
171179
sendErrorToSentry(e);
172180
throw e;
173181
}
174-
// TODO: flush if serveless (first extract function)
182+
// TODO: flush if serverless (first extract function)
175183
}
176184

177185
/**

packages/astro/test/server/middleware.test.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ describe('sentryMiddleware', () => {
9898
});
9999

100100
it('attaches tracing headers', async () => {
101-
const scope = { setUser: vi.fn(), setPropagationContext: vi.fn() };
102-
// @ts-expect-error, only passing a partial Scope object
103-
const configureScopeSpy = vi.spyOn(SentryNode, 'configureScope').mockImplementation(cb => cb(scope));
104-
105101
const middleware = handleRequest();
106102
const ctx = {
107103
request: {
@@ -120,17 +116,6 @@ describe('sentryMiddleware', () => {
120116
// @ts-expect-error, a partial ctx object is fine here
121117
await middleware(ctx, next);
122118

123-
expect(configureScopeSpy).toHaveBeenCalledTimes(1);
124-
expect(scope.setPropagationContext).toHaveBeenCalledWith({
125-
dsc: {
126-
release: '1.0.0',
127-
},
128-
parentSpanId: '1234567890123456',
129-
sampled: true,
130-
spanId: expect.any(String),
131-
traceId: '12345678901234567890123456789012',
132-
});
133-
134119
expect(startSpanSpy).toHaveBeenCalledWith(
135120
expect.objectContaining({
136121
metadata: {

packages/core/src/tracing/trace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ export function continueTrace({
211211
sentryTrace: Parameters<typeof tracingContextFromHeaders>[0];
212212
baggage: Parameters<typeof tracingContextFromHeaders>[1];
213213
}): Partial<TransactionContext>;
214+
214215
export function continueTrace<V>(
215216
{
216217
sentryTrace,

0 commit comments

Comments
 (0)