Skip to content

Commit 0072aa9

Browse files
committed
fix(node): Ensure isolation scope is correctly cloned for non-recording spans
1 parent 14e5373 commit 0072aa9

File tree

6 files changed

+74
-6
lines changed

6 files changed

+74
-6
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
2+
import * as Sentry from '@sentry/node';
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
transport: loggingTransport,
8+
debug: true,
9+
tracesSampleRate: 0,
10+
});
11+
12+
import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
13+
import express from 'express';
14+
15+
const app = express();
16+
17+
app.get('/test/express/:id', req => {
18+
throw new Error(`test_error with id ${req.params.id}`);
19+
});
20+
21+
Sentry.setupExpressErrorHandler(app);
22+
23+
startExpressServerAndSendPortToRunner(app);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';
2+
3+
afterAll(() => {
4+
cleanupChildProcesses();
5+
});
6+
7+
test('should capture and send Express controller error.', done => {
8+
const runner = createRunner(__dirname, 'server.ts')
9+
.ignore('session', 'sessions')
10+
.expect({
11+
event: {
12+
exception: {
13+
values: [
14+
{
15+
mechanism: {
16+
type: 'middleware',
17+
handled: false,
18+
},
19+
type: 'Error',
20+
value: 'test_error with id 123',
21+
stacktrace: {
22+
frames: expect.arrayContaining([
23+
expect.objectContaining({
24+
function: expect.any(String),
25+
lineno: expect.any(Number),
26+
colno: expect.any(Number),
27+
}),
28+
]),
29+
},
30+
},
31+
],
32+
},
33+
transaction: 'GET /test/express/:id',
34+
},
35+
})
36+
.start(done);
37+
38+
expect(() => runner.makeRequest('get', '/test/express/123')).rejects.toThrow();
39+
});

dev-packages/node-integration-tests/suites/express/handle-error/server.ts renamed to dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-0/server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ Sentry.init({
55
dsn: 'https://public@dsn.ingest.sentry.io/1337',
66
release: '1.0',
77
transport: loggingTransport,
8+
debug: true,
89
});
910

1011
import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
1112
import express from 'express';
1213

1314
const app = express();
1415

15-
app.get('/test/express', () => {
16-
throw new Error('test_error');
16+
app.get('/test/express/:id', req => {
17+
throw new Error(`test_error with id ${req.params.id}`);
1718
});
1819

1920
Sentry.setupExpressErrorHandler(app);

dev-packages/node-integration-tests/suites/express/handle-error/test.ts renamed to dev-packages/node-integration-tests/suites/express/handle-error-tracesSampleRate-0/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test('should capture and send Express controller error.', done => {
1717
handled: false,
1818
},
1919
type: 'Error',
20-
value: 'test_error',
20+
value: 'test_error with id 123',
2121
stacktrace: {
2222
frames: expect.arrayContaining([
2323
expect.objectContaining({
@@ -34,5 +34,5 @@ test('should capture and send Express controller error.', done => {
3434
})
3535
.start(done);
3636

37-
expect(() => runner.makeRequest('get', '/test/express')).rejects.toThrow();
37+
expect(() => runner.makeRequest('get', '/test/express/123')).rejects.toThrow();
3838
});

packages/node/src/integrations/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ const _httpIntegration = ((options: HttpOptions = {}) => {
9393
requestHook: (span, req) => {
9494
addOriginToSpan(span, 'auto.http.otel.http');
9595

96-
if (getSpanKind(span) !== SpanKind.SERVER) {
96+
if (span.isRecording() && getSpanKind(span) !== SpanKind.SERVER) {
9797
return;
9898
}
9999

packages/node/src/integrations/tracing/express.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type * as http from 'http';
22
import { registerInstrumentations } from '@opentelemetry/instrumentation';
33
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
4-
import { defineIntegration } from '@sentry/core';
4+
import { defineIntegration, getDefaultIsolationScope } from '@sentry/core';
55
import { captureException, getClient, getIsolationScope } from '@sentry/core';
66
import type { IntegrationFn } from '@sentry/types';
77

8+
import { logger } from '@sentry/utils';
89
import type { NodeClient } from '../../sdk/client';
910
import { addOriginToSpan } from '../../utils/addOriginToSpan';
1011

@@ -19,6 +20,10 @@ const _expressIntegration = (() => {
1920
addOriginToSpan(span, 'auto.http.otel.express');
2021
},
2122
spanNameHook(info, defaultName) {
23+
if (getIsolationScope() === getDefaultIsolationScope()) {
24+
logger.warn('Isolation scope is still default isolation scope - skipping setting transactionName');
25+
return defaultName;
26+
}
2227
if (info.layerType === 'request_handler') {
2328
// type cast b/c Otel unfortunately types info.request as any :(
2429
const req = info.request as { method?: string };

0 commit comments

Comments
 (0)