Skip to content

Commit 97c18d3

Browse files
committed
feat(node): Implement Sentry-specific http instrumentation
1 parent a9750be commit 97c18d3

File tree

9 files changed

+543
-325
lines changed

9 files changed

+543
-325
lines changed

dev-packages/e2e-tests/test-applications/node-otel-without-tracing/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@opentelemetry/sdk-trace-node": "1.26.0",
1515
"@opentelemetry/exporter-trace-otlp-http": "0.53.0",
1616
"@opentelemetry/instrumentation-undici": "0.6.0",
17+
"@opentelemetry/instrumentation-http": "0.53.0",
1718
"@opentelemetry/instrumentation": "0.53.0",
1819
"@sentry/core": "latest || *",
1920
"@sentry/node": "latest || *",

dev-packages/e2e-tests/test-applications/node-otel-without-tracing/src/app.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import express from 'express';
77
const app = express();
88
const port = 3030;
99

10+
Sentry.setTag('root-level-tag', 'yes');
11+
1012
app.get('/test-success', function (req, res) {
1113
res.send({ version: 'v1' });
1214
});
@@ -23,8 +25,6 @@ app.get('/test-transaction', function (req, res) {
2325

2426
await fetch('http://localhost:3030/test-success');
2527

26-
await Sentry.flush();
27-
2828
res.send({});
2929
});
3030
});
@@ -38,7 +38,10 @@ app.get('/test-error', async function (req, res) {
3838
});
3939

4040
app.get('/test-exception/:id', function (req, _res) {
41-
throw new Error(`This is an exception with id ${req.params.id}`);
41+
const id = req.params.id;
42+
Sentry.setTag(`param-${id}`, id);
43+
44+
throw new Error(`This is an exception with id ${id}`);
4245
});
4346

4447
Sentry.setupExpressErrorHandler(app);

dev-packages/e2e-tests/test-applications/node-otel-without-tracing/src/instrument.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
12
const { NodeTracerProvider, BatchSpanProcessor } = require('@opentelemetry/sdk-trace-node');
23
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
34
const Sentry = require('@sentry/node');
4-
const { SentrySpanProcessor, SentryPropagator } = require('@sentry/opentelemetry');
5+
const { SentryPropagator } = require('@sentry/opentelemetry');
56
const { UndiciInstrumentation } = require('@opentelemetry/instrumentation-undici');
67
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
78

@@ -15,6 +16,8 @@ Sentry.init({
1516
tunnel: `http://localhost:3031/`, // proxy server
1617
// Tracing is completely disabled
1718

19+
integrations: [Sentry.httpIntegration({ spans: true })],
20+
1821
// Custom OTEL setup
1922
skipOpenTelemetrySetup: true,
2023
});
@@ -37,5 +40,5 @@ provider.register({
3740
});
3841

3942
registerInstrumentations({
40-
instrumentations: [new UndiciInstrumentation()],
43+
instrumentations: [new UndiciInstrumentation(), new HttpInstrumentation()],
4144
});

dev-packages/e2e-tests/test-applications/node-otel-without-tracing/tests/errors.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,24 @@ test('Sends correct error event', async ({ baseURL }) => {
2828
span_id: expect.any(String),
2929
});
3030
});
31+
32+
test('Isolates requests correctly', async ({ baseURL }) => {
33+
const errorEventPromise1 = waitForError('node-otel-without-tracing', event => {
34+
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 555-a';
35+
});
36+
const errorEventPromise2 = waitForError('node-otel-without-tracing', event => {
37+
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 555-b';
38+
});
39+
40+
fetch(`${baseURL}/test-exception/555-a`);
41+
fetch(`${baseURL}/test-exception/555-b`);
42+
43+
const errorEvent1 = await errorEventPromise1;
44+
const errorEvent2 = await errorEventPromise2;
45+
46+
expect(errorEvent1.transaction).toEqual('GET /test-exception/555-a');
47+
expect(errorEvent1.tags).toEqual({ 'root-level-tag': 'yes', 'param-555-a': '555-a' });
48+
49+
expect(errorEvent2.transaction).toEqual('GET /test-exception/555-b');
50+
expect(errorEvent2.tags).toEqual({ 'root-level-tag': 'yes', 'param-555-b': '555-b' });
51+
});

dev-packages/e2e-tests/test-applications/node-otel-without-tracing/tests/transactions.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ test('Sends an API route transaction to OTLP', async ({ baseURL }) => {
1212

1313
const scopeSpans = json.resourceSpans?.[0]?.scopeSpans;
1414

15-
const httpScope = scopeSpans?.find(
16-
scopeSpan => scopeSpan.scope.name === '@opentelemetry_sentry-patched/instrumentation-http',
17-
);
15+
const httpScope = scopeSpans?.find(scopeSpan => scopeSpan.scope.name === '@opentelemetry/instrumentation-http');
1816

1917
return (
2018
httpScope &&
@@ -40,9 +38,7 @@ test('Sends an API route transaction to OTLP', async ({ baseURL }) => {
4038
// But our default node-fetch spans are not emitted
4139
expect(scopeSpans.length).toEqual(2);
4240

43-
const httpScopes = scopeSpans?.filter(
44-
scopeSpan => scopeSpan.scope.name === '@opentelemetry_sentry-patched/instrumentation-http',
45-
);
41+
const httpScopes = scopeSpans?.filter(scopeSpan => scopeSpan.scope.name === '@opentelemetry/instrumentation-http');
4642
const undiciScopes = scopeSpans?.filter(
4743
scopeSpan => scopeSpan.scope.name === '@opentelemetry/instrumentation-undici',
4844
);
@@ -114,7 +110,6 @@ test('Sends an API route transaction to OTLP', async ({ baseURL }) => {
114110
{ key: 'net.peer.port', value: { intValue: expect.any(Number) } },
115111
{ key: 'http.status_code', value: { intValue: 200 } },
116112
{ key: 'http.status_text', value: { stringValue: 'OK' } },
117-
{ key: 'sentry.origin', value: { stringValue: 'auto.http.otel.http' } },
118113
]),
119114
droppedAttributesCount: 0,
120115
events: [],

0 commit comments

Comments
 (0)