Skip to content

Commit e0133a9

Browse files
committed
fix: remove uneeded isolation scope calls
1 parent 5117797 commit e0133a9

File tree

2 files changed

+25
-80
lines changed

2 files changed

+25
-80
lines changed

packages/nuxt/src/runtime/hooks/wrapMiddlewareHandler.ts

Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import {
2-
type RequestEventData,
32
type SpanAttributes,
43
captureException,
54
debug,
65
flushIfServerless,
76
getClient,
8-
getDefaultIsolationScope,
9-
getIsolationScope,
107
httpHeadersToSpanAttributes,
11-
httpRequestToRequestData,
128
SEMANTIC_ATTRIBUTE_SENTRY_OP,
139
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
1410
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
15-
SPAN_STATUS_ERROR,
1611
SPAN_STATUS_OK,
1712
startSpan,
18-
withIsolationScope,
1913
} from '@sentry/core';
2014
import type { EventHandler, EventHandlerRequest, H3Event } from 'h3';
2115

@@ -29,63 +23,37 @@ export function wrapMiddlewareHandler(handler: EventHandler, fileName: string) {
2923
return async (event: H3Event<EventHandlerRequest>) => {
3024
debug.log(`Sentry middleware: ${fileName} handling ${event.path}`);
3125

32-
const isolationScope = getIsolationScope();
33-
const newIsolationScope = isolationScope === getDefaultIsolationScope() ? isolationScope.clone() : isolationScope;
34-
const normalizedRequest = createNormalizedRequestData(event);
35-
newIsolationScope.setSDKProcessingMetadata({
36-
normalizedRequest,
37-
});
38-
3926
const attributes = getSpanAttributes(event, fileName);
4027

41-
return withIsolationScope(newIsolationScope, async () => {
42-
return startSpan(
43-
{
44-
name: fileName,
45-
attributes,
46-
},
47-
async span => {
48-
try {
49-
const result = await handler(event);
50-
span.setStatus({ code: SPAN_STATUS_OK });
28+
return startSpan(
29+
{
30+
name: fileName,
31+
attributes,
32+
},
33+
async span => {
34+
try {
35+
const result = await handler(event);
36+
span.setStatus({ code: SPAN_STATUS_OK });
5137

52-
return result;
53-
} catch (error) {
54-
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
55-
span.recordException(error);
56-
captureException(error, {
57-
mechanism: {
58-
handled: false,
59-
type: attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN],
60-
},
61-
});
62-
span.end();
38+
return result;
39+
} catch (error) {
40+
captureException(error, {
41+
mechanism: {
42+
handled: false,
43+
type: attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN],
44+
},
45+
});
6346

64-
// Re-throw the error to be handled by the caller
65-
throw error;
66-
} finally {
67-
await flushIfServerless();
68-
}
69-
},
70-
);
71-
});
47+
// Re-throw the error to be handled by the caller
48+
throw error;
49+
} finally {
50+
await flushIfServerless();
51+
}
52+
},
53+
);
7254
};
7355
}
7456

75-
/**
76-
* Creates the normalized request data for the middleware handler based on the event.
77-
*/
78-
function createNormalizedRequestData(event: H3Event<EventHandlerRequest>): RequestEventData {
79-
// Extract headers from the Node.js request object
80-
const headers = event.node?.req?.headers || {};
81-
82-
return httpRequestToRequestData({
83-
method: event.method,
84-
url: event.path || event.node?.req?.url,
85-
headers,
86-
});
87-
}
88-
8957
/**
9058
* Gets the span attributes for the middleware handler based on the event.
9159
*/

packages/nuxt/test/runtime/hooks/wrapMiddlewareHandler.test.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ vi.mock('@sentry/core', async importOriginal => {
1010
...(mod as any),
1111
debug: { log: vi.fn() },
1212
startSpan: vi.fn(),
13-
withIsolationScope: vi.fn(),
14-
getIsolationScope: vi.fn(),
15-
getDefaultIsolationScope: vi.fn(),
1613
getClient: vi.fn(),
1714
httpHeadersToSpanAttributes: vi.fn(),
18-
httpRequestToRequestData: vi.fn(),
1915
captureException: vi.fn(),
2016
flushIfServerless: vi.fn(),
2117
};
@@ -39,22 +35,13 @@ describe('wrapMiddlewareHandler', () => {
3935
end: vi.fn(),
4036
};
4137

42-
const mockIsolationScope = {
43-
clone: vi.fn().mockReturnValue('cloned-scope'),
44-
setSDKProcessingMetadata: vi.fn(),
45-
};
46-
4738
beforeEach(() => {
4839
vi.clearAllMocks();
4940

5041
// Setup minimal required mocks
51-
(SentryCore.getIsolationScope as any).mockReturnValue(mockIsolationScope);
52-
(SentryCore.getDefaultIsolationScope as any).mockReturnValue('default-scope');
53-
(SentryCore.withIsolationScope as any).mockImplementation((_scope: any, callback: any) => callback());
5442
(SentryCore.startSpan as any).mockImplementation((_config: any, callback: any) => callback(mockSpan));
5543
(SentryCore.getClient as any).mockReturnValue({ getOptions: () => ({ sendDefaultPii: false }) });
5644
(SentryCore.httpHeadersToSpanAttributes as any).mockReturnValue({ 'http.request.header.user_agent': 'test-agent' });
57-
(SentryCore.httpRequestToRequestData as any).mockReturnValue({ url: '/test-path', method: 'GET' });
5845
(SentryCore.flushIfServerless as any).mockResolvedValue(undefined);
5946
});
6047

@@ -109,7 +96,6 @@ describe('wrapMiddlewareHandler', () => {
10996

11097
// Verify Sentry APIs were called but error was not masked
11198
expect(SentryCore.captureException).toHaveBeenCalledWith(originalError, expect.any(Object));
112-
expect(mockSpan.recordException).toHaveBeenCalledWith(originalError);
11399
});
114100

115101
it('should propagate sync errors without modification', async () => {
@@ -151,9 +137,6 @@ describe('wrapMiddlewareHandler', () => {
151137

152138
// This should handle the Sentry error gracefully and still call user code
153139
await expect(wrapped(mockEvent)).rejects.toThrow('Sentry API failure');
154-
155-
// But user handler should still have been attempted to be called
156-
// (this tests that we don't fail before reaching user code)
157140
});
158141
});
159142

@@ -169,20 +152,14 @@ describe('wrapMiddlewareHandler', () => {
169152
expect.objectContaining({
170153
name: 'api-middleware',
171154
attributes: expect.objectContaining({
172-
[SentryCore.SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'http.server.middleware',
155+
[SentryCore.SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'middleware.nuxt',
173156
'nuxt.middleware.name': 'api-middleware',
174157
'http.request.method': 'GET',
175158
'http.route': '/test-path',
176159
}),
177160
}),
178161
expect.any(Function),
179162
);
180-
181-
expect(SentryCore.httpRequestToRequestData).toHaveBeenCalledWith({
182-
method: 'GET',
183-
url: '/test-path',
184-
headers: { 'user-agent': 'test-agent' },
185-
});
186163
});
187164

188165
it('should handle missing optional data gracefully', async () => {

0 commit comments

Comments
 (0)