From b1f4ccf60bf23dec77e885e89418ef423bfd5809 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 14 Aug 2024 16:50:54 +0200 Subject: [PATCH 01/13] fix(opentelemetry): Always use active span in `getInjectionData` --- packages/opentelemetry/src/propagator.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/opentelemetry/src/propagator.ts b/packages/opentelemetry/src/propagator.ts index 387943cf9cf0..0bf251913bf6 100644 --- a/packages/opentelemetry/src/propagator.ts +++ b/packages/opentelemetry/src/propagator.ts @@ -93,6 +93,8 @@ export class SentryPropagator extends W3CBaggagePropagator { const activeSpan = trace.getSpan(context); const url = activeSpan && getCurrentURL(activeSpan); + console.log('1', { activeSpan, url }); + const tracePropagationTargets = getClient()?.getOptions()?.tracePropagationTargets; if ( typeof url === 'string' && @@ -104,6 +106,7 @@ export class SentryPropagator extends W3CBaggagePropagator { '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:', url, ); + console.log('2', { tracePropagationTargets }); return; } @@ -112,6 +115,7 @@ export class SentryPropagator extends W3CBaggagePropagator { const { dynamicSamplingContext, traceId, spanId, sampled } = getInjectionData(context); + console.log('3', { existingBaggageHeader, baggage, dynamicSamplingContext, traceId, spanId, sampled }); if (existingBaggageHeader) { const baggageEntries = parseBaggageHeader(existingBaggageHeader); @@ -120,6 +124,7 @@ export class SentryPropagator extends W3CBaggagePropagator { baggage = baggage.setEntry(key, { value }); }); } + console.log('4', { baggageEntries, baggage }); } if (dynamicSamplingContext) { @@ -129,14 +134,17 @@ export class SentryPropagator extends W3CBaggagePropagator { } return b; }, baggage); + console.log('5', { dynamicSamplingContext, baggage }); } // We also want to avoid setting the default OTEL trace ID, if we get that for whatever reason if (traceId && traceId !== INVALID_TRACEID) { setter.set(carrier, SENTRY_TRACE_HEADER, generateSentryTraceHeader(traceId, spanId, sampled)); + console.log('6', { carrier, traceId, spanId, sampled }); } super.inject(propagation.setBaggage(context, baggage), carrier, setter); + console.log('7', { carrier }); } /** @@ -198,7 +206,7 @@ function getInjectionData(context: Context): { spanId: string | undefined; sampled: boolean | undefined; } { - const span = hasTracingEnabled() ? trace.getSpan(context) : undefined; + const span = trace.getSpan(context); const spanIsRemote = span?.spanContext().isRemote; // If we have a local span, we can just pick everything from it From d558c1b555a68d74073c80fd4b598fcb9ccd2195 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 14 Aug 2024 17:48:08 +0200 Subject: [PATCH 02/13] formatting, linting --- packages/opentelemetry/src/propagator.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/packages/opentelemetry/src/propagator.ts b/packages/opentelemetry/src/propagator.ts index 0bf251913bf6..f4fcb1fa91e9 100644 --- a/packages/opentelemetry/src/propagator.ts +++ b/packages/opentelemetry/src/propagator.ts @@ -5,7 +5,6 @@ import { propagation, trace } from '@opentelemetry/api'; import { W3CBaggagePropagator, isTracingSuppressed } from '@opentelemetry/core'; import { ATTR_URL_FULL, SEMATTRS_HTTP_URL } from '@opentelemetry/semantic-conventions'; import type { continueTrace } from '@sentry/core'; -import { hasTracingEnabled } from '@sentry/core'; import { getRootSpan } from '@sentry/core'; import { spanToJSON } from '@sentry/core'; import { @@ -93,8 +92,6 @@ export class SentryPropagator extends W3CBaggagePropagator { const activeSpan = trace.getSpan(context); const url = activeSpan && getCurrentURL(activeSpan); - console.log('1', { activeSpan, url }); - const tracePropagationTargets = getClient()?.getOptions()?.tracePropagationTargets; if ( typeof url === 'string' && @@ -106,7 +103,6 @@ export class SentryPropagator extends W3CBaggagePropagator { '[Tracing] Not injecting trace data for url because it does not match tracePropagationTargets:', url, ); - console.log('2', { tracePropagationTargets }); return; } @@ -115,7 +111,6 @@ export class SentryPropagator extends W3CBaggagePropagator { const { dynamicSamplingContext, traceId, spanId, sampled } = getInjectionData(context); - console.log('3', { existingBaggageHeader, baggage, dynamicSamplingContext, traceId, spanId, sampled }); if (existingBaggageHeader) { const baggageEntries = parseBaggageHeader(existingBaggageHeader); @@ -124,7 +119,6 @@ export class SentryPropagator extends W3CBaggagePropagator { baggage = baggage.setEntry(key, { value }); }); } - console.log('4', { baggageEntries, baggage }); } if (dynamicSamplingContext) { @@ -134,17 +128,14 @@ export class SentryPropagator extends W3CBaggagePropagator { } return b; }, baggage); - console.log('5', { dynamicSamplingContext, baggage }); } // We also want to avoid setting the default OTEL trace ID, if we get that for whatever reason if (traceId && traceId !== INVALID_TRACEID) { setter.set(carrier, SENTRY_TRACE_HEADER, generateSentryTraceHeader(traceId, spanId, sampled)); - console.log('6', { carrier, traceId, spanId, sampled }); } super.inject(propagation.setBaggage(context, baggage), carrier, setter); - console.log('7', { carrier }); } /** From 921d7f0c5ed0921369e17785d94c484b2a47cd6e Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 14 Aug 2024 18:19:03 +0200 Subject: [PATCH 03/13] add test --- .../tracing/meta-tags-twp-errors/server.js | 31 +++++++++++++ .../tracing/meta-tags-twp-errors/test.ts | 43 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js create mode 100644 dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js new file mode 100644 index 000000000000..af879aa5787d --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js @@ -0,0 +1,31 @@ +const { loggingTransport } = require('@sentry-internal/node-integration-tests'); +const Sentry = require('@sentry/node'); + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + transport: loggingTransport, +}); + +// express must be required after Sentry is initialized +const express = require('express'); +const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests'); + +const app = express(); + +app.get('/test', (_req, res) => { + Sentry.captureException(new Error('This is a test error')); + Sentry.getClient().on('beforeEnvelope', envelope => { + const event = envelope[1][0][1]; + if (event.exception.values[0].value === 'This is a test error') { + res.send({ + traceData: Sentry.getTraceData(), + traceMetaTags: Sentry.getTraceMetaTags(), + errorTraceContext: event.contexts.trace, + }); + } + }); +}); + +Sentry.setupExpressErrorHandler(app); + +startExpressServerAndSendPortToRunner(app); diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts new file mode 100644 index 000000000000..aed6914ecaaf --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts @@ -0,0 +1,43 @@ +import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; + +describe('errors in Tracing without Performance mode', () => { + afterAll(() => { + cleanupChildProcesses(); + }); + + test('error has the same traceId as obtained via getTraceData()/getTraceMetaTags()', async () => { + const runner = createRunner(__dirname, 'server.js').start(); + + const response = await runner.makeRequest('get', '/test'); + + console.log('response: ', response); + + const { traceData, traceMetaTags, errorTraceContext } = response as { + traceData: Record; + traceMetaTags: string; + errorTraceContext: { + trace_id: string; + span_id: string; + }; + }; + + const traceId = errorTraceContext?.trace_id; + const spanId = errorTraceContext?.span_id; + + expect(traceId).toMatch(/^[a-f0-9]{32}$/); + expect(spanId).toMatch(/^[a-f0-9]{16}$/); + + expect(errorTraceContext).toEqual({ + trace_id: traceId, + span_id: spanId, + }); + + expect(traceData).toEqual({ + 'sentry-trace': `${traceId}-${spanId}`, + baggage: expect.stringContaining(`sentry-trace_id=${traceId}`), + }); + + expect(traceMetaTags).toContain(`content="${traceId}-${spanId}"/>\n`); + expect(traceMetaTags).toContain(`sentry-trace_id=${traceId}`); + }); +}); From 79921fff7e24063fddf680b7cfecc8f2985cd669 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 14 Aug 2024 18:36:30 +0200 Subject: [PATCH 04/13] cleanup --- .../suites/tracing/meta-tags-twp-errors/test.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts index aed6914ecaaf..e33d3983f691 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts @@ -10,8 +10,6 @@ describe('errors in Tracing without Performance mode', () => { const response = await runner.makeRequest('get', '/test'); - console.log('response: ', response); - const { traceData, traceMetaTags, errorTraceContext } = response as { traceData: Record; traceMetaTags: string; From d35b8f884b9bd154ed74ec6ac3fcaa4b0a605231 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 10:34:40 +0200 Subject: [PATCH 05/13] fix test? --- .../suites/tracing/meta-tags-twp-errors/server.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js index af879aa5787d..a8190376daba 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js @@ -17,10 +17,14 @@ app.get('/test', (_req, res) => { Sentry.getClient().on('beforeEnvelope', envelope => { const event = envelope[1][0][1]; if (event.exception.values[0].value === 'This is a test error') { + const { trace_id, span_id } = event.contexts.trace; res.send({ traceData: Sentry.getTraceData(), traceMetaTags: Sentry.getTraceMetaTags(), - errorTraceContext: event.contexts.trace, + errorTraceContext: { + trace_id, + span_id, + }, }); } }); From 68ca4e2c43fa77c7e3da8b95c12ff45ac1d2c69e Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 10:53:31 +0200 Subject: [PATCH 06/13] tmp debug logs --- .../suites/tracing/meta-tags-twp-errors/server.js | 11 +++++++++++ .../suites/tracing/meta-tags-twp-errors/test.ts | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js index a8190376daba..6bb7539f9d0f 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js @@ -1,22 +1,31 @@ const { loggingTransport } = require('@sentry-internal/node-integration-tests'); const Sentry = require('@sentry/node'); +console.log('X-4'); Sentry.init({ dsn: 'https://public@dsn.ingest.sentry.io/1337', transport: loggingTransport, }); +console.log('X-3'); // express must be required after Sentry is initialized const express = require('express'); +console.log('X-2'); const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests'); +console.log('X-1'); const app = express(); +console.log('X0'); app.get('/test', (_req, res) => { + console.log('X1'); Sentry.captureException(new Error('This is a test error')); + console.log('X2'); Sentry.getClient().on('beforeEnvelope', envelope => { + console.log('X3'); const event = envelope[1][0][1]; if (event.exception.values[0].value === 'This is a test error') { + console.log('X4'); const { trace_id, span_id } = event.contexts.trace; res.send({ traceData: Sentry.getTraceData(), @@ -31,5 +40,7 @@ app.get('/test', (_req, res) => { }); Sentry.setupExpressErrorHandler(app); +console.log('X5'); startExpressServerAndSendPortToRunner(app); +console.log('X6'); diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts index e33d3983f691..1b7d52fee00d 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts @@ -10,6 +10,8 @@ describe('errors in Tracing without Performance mode', () => { const response = await runner.makeRequest('get', '/test'); + console.log('yy res', response); + const { traceData, traceMetaTags, errorTraceContext } = response as { traceData: Record; traceMetaTags: string; @@ -37,5 +39,7 @@ describe('errors in Tracing without Performance mode', () => { expect(traceMetaTags).toContain(`content="${traceId}-${spanId}"/>\n`); expect(traceMetaTags).toContain(`sentry-trace_id=${traceId}`); + + console.log(runner.getLogs()); }); }); From e394beb4546aad3baa57aa9a7d89a5a13db73e3e Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 11:24:44 +0200 Subject: [PATCH 07/13] more debug logs --- .../suites/tracing/meta-tags-twp-errors/test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts index 1b7d52fee00d..cd9ea22582dc 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts @@ -8,8 +8,12 @@ describe('errors in Tracing without Performance mode', () => { test('error has the same traceId as obtained via getTraceData()/getTraceMetaTags()', async () => { const runner = createRunner(__dirname, 'server.js').start(); + console.log('1', runner.getLogs()); + const response = await runner.makeRequest('get', '/test'); + console.log('2', runner.getLogs()); + console.log('yy res', response); const { traceData, traceMetaTags, errorTraceContext } = response as { @@ -21,6 +25,8 @@ describe('errors in Tracing without Performance mode', () => { }; }; + console.log('3', runner.getLogs()); + const traceId = errorTraceContext?.trace_id; const spanId = errorTraceContext?.span_id; From 265865a363c2f73247f7970929f70b4e773c5ea4 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 11:37:10 +0200 Subject: [PATCH 08/13] more logs pls? --- .../suites/tracing/meta-tags-twp-errors/test.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts index cd9ea22582dc..3fb6f0c1f59a 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts @@ -1,11 +1,14 @@ import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; describe('errors in Tracing without Performance mode', () => { + console.log('-1'); + afterAll(() => { cleanupChildProcesses(); }); test('error has the same traceId as obtained via getTraceData()/getTraceMetaTags()', async () => { + console.log('0'); const runner = createRunner(__dirname, 'server.js').start(); console.log('1', runner.getLogs()); From c4be89b3466bb1674a6781ff013da123059e07fb Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 12:49:30 +0200 Subject: [PATCH 09/13] does this trigger a different error now? --- .../tracing/meta-tags-twp-errors/server.js | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js index 6bb7539f9d0f..aacf027161ec 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js @@ -21,22 +21,22 @@ app.get('/test', (_req, res) => { console.log('X1'); Sentry.captureException(new Error('This is a test error')); console.log('X2'); - Sentry.getClient().on('beforeEnvelope', envelope => { - console.log('X3'); - const event = envelope[1][0][1]; - if (event.exception.values[0].value === 'This is a test error') { - console.log('X4'); - const { trace_id, span_id } = event.contexts.trace; - res.send({ - traceData: Sentry.getTraceData(), - traceMetaTags: Sentry.getTraceMetaTags(), - errorTraceContext: { - trace_id, - span_id, - }, - }); - } - }); + // Sentry.getClient().on('beforeEnvelope', envelope => { + // console.log('X3'); + // const event = envelope[1][0][1]; + // if (event.exception.values[0].value === 'This is a test error') { + // console.log('X4'); + // const { trace_id, span_id } = event.contexts.trace; + // res.send({ + // traceData: Sentry.getTraceData(), + // traceMetaTags: Sentry.getTraceMetaTags(), + // errorTraceContext: { + // trace_id, + // span_id, + // }, + // }); + // } + // }); }); Sentry.setupExpressErrorHandler(app); From f97a740e8de8b5bf90086acc87217b271033c887 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 13:41:11 +0200 Subject: [PATCH 10/13] , --- .../suites/tracing/meta-tags-twp-errors/server.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js index aacf027161ec..e801c3613c6d 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js @@ -1,4 +1,4 @@ -const { loggingTransport } = require('@sentry-internal/node-integration-tests'); +const { loggingTransport, startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests'); const Sentry = require('@sentry/node'); console.log('X-4'); @@ -11,15 +11,13 @@ console.log('X-3'); // express must be required after Sentry is initialized const express = require('express'); console.log('X-2'); -const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests'); -console.log('X-1'); const app = express(); console.log('X0'); app.get('/test', (_req, res) => { console.log('X1'); - Sentry.captureException(new Error('This is a test error')); + // Sentry.captureException(new Error('This is a test error')); console.log('X2'); // Sentry.getClient().on('beforeEnvelope', envelope => { // console.log('X3'); From 2192d6f76383c7372ecdd8f5775baff29d54fe61 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 14:02:37 +0200 Subject: [PATCH 11/13] .. --- .../suites/tracing/meta-tags-twp-errors/server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js index e801c3613c6d..fc1704a229a6 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js @@ -17,7 +17,7 @@ console.log('X0'); app.get('/test', (_req, res) => { console.log('X1'); - // Sentry.captureException(new Error('This is a test error')); + Sentry.captureException('This is a test error'); console.log('X2'); // Sentry.getClient().on('beforeEnvelope', envelope => { // console.log('X3'); From ef9aa486baa367892d09d2277d09dda4610df1da Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 14:49:55 +0200 Subject: [PATCH 12/13] rewrite test, add new test --- .../tracing/meta-tags-twp-errors/no-server.js | 20 +++++ .../tracing/meta-tags-twp-errors/server.js | 36 +++----- .../tracing/meta-tags-twp-errors/test.ts | 88 +++++++++---------- 3 files changed, 74 insertions(+), 70 deletions(-) create mode 100644 dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/no-server.js diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/no-server.js b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/no-server.js new file mode 100644 index 000000000000..ac0122b48380 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/no-server.js @@ -0,0 +1,20 @@ +const { loggingTransport } = require('@sentry-internal/node-integration-tests'); +const Sentry = require('@sentry/node'); + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + release: '1.0', + transport: loggingTransport, + beforeSend(event) { + event.contexts = { + ...event.contexts, + traceData: { + ...Sentry.getTraceData(), + metaTags: Sentry.getTraceMetaTags(), + }, + }; + return event; + }, +}); + +Sentry.captureException(new Error('test error')); diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js index fc1704a229a6..95ecb620c3ed 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js @@ -1,44 +1,30 @@ const { loggingTransport, startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests'); const Sentry = require('@sentry/node'); -console.log('X-4'); Sentry.init({ dsn: 'https://public@dsn.ingest.sentry.io/1337', transport: loggingTransport, + beforeSend(event) { + event.contexts = { + ...event.contexts, + traceData: { + ...Sentry.getTraceData(), + metaTags: Sentry.getTraceMetaTags(), + }, + }; + return event; + }, }); -console.log('X-3'); // express must be required after Sentry is initialized const express = require('express'); -console.log('X-2'); const app = express(); -console.log('X0'); app.get('/test', (_req, res) => { - console.log('X1'); - Sentry.captureException('This is a test error'); - console.log('X2'); - // Sentry.getClient().on('beforeEnvelope', envelope => { - // console.log('X3'); - // const event = envelope[1][0][1]; - // if (event.exception.values[0].value === 'This is a test error') { - // console.log('X4'); - // const { trace_id, span_id } = event.contexts.trace; - // res.send({ - // traceData: Sentry.getTraceData(), - // traceMetaTags: Sentry.getTraceMetaTags(), - // errorTraceContext: { - // trace_id, - // span_id, - // }, - // }); - // } - // }); + throw new Error('test error'); }); Sentry.setupExpressErrorHandler(app); -console.log('X5'); startExpressServerAndSendPortToRunner(app); -console.log('X6'); diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts index 3fb6f0c1f59a..793aada386f3 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts @@ -1,54 +1,52 @@ import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; -describe('errors in Tracing without Performance mode', () => { - console.log('-1'); - +describe('errors in TwP mode have same trace in trace context and getTraceData()', () => { afterAll(() => { cleanupChildProcesses(); }); - test('error has the same traceId as obtained via getTraceData()/getTraceMetaTags()', async () => { - console.log('0'); - const runner = createRunner(__dirname, 'server.js').start(); - - console.log('1', runner.getLogs()); - - const response = await runner.makeRequest('get', '/test'); - - console.log('2', runner.getLogs()); - - console.log('yy res', response); - - const { traceData, traceMetaTags, errorTraceContext } = response as { - traceData: Record; - traceMetaTags: string; - errorTraceContext: { - trace_id: string; - span_id: string; - }; - }; - - console.log('3', runner.getLogs()); - - const traceId = errorTraceContext?.trace_id; - const spanId = errorTraceContext?.span_id; - - expect(traceId).toMatch(/^[a-f0-9]{32}$/); - expect(spanId).toMatch(/^[a-f0-9]{16}$/); - - expect(errorTraceContext).toEqual({ - trace_id: traceId, - span_id: spanId, - }); - - expect(traceData).toEqual({ - 'sentry-trace': `${traceId}-${spanId}`, - baggage: expect.stringContaining(`sentry-trace_id=${traceId}`), - }); - - expect(traceMetaTags).toContain(`content="${traceId}-${spanId}"/>\n`); - expect(traceMetaTags).toContain(`sentry-trace_id=${traceId}`); + test('in incoming request', async () => { + const runner = createRunner(__dirname, 'server.js') + .expect({ + event: event => { + const { contexts } = event; + const { trace_id, span_id } = contexts?.trace || {}; + expect(trace_id).toMatch(/^[a-f0-9]{32}$/); + expect(span_id).toMatch(/^[a-f0-9]{16}$/); + + const traceData = contexts?.traceData || {}; + + expect(traceData['sentry-trace']).toEqual(`${trace_id}-${span_id}`); + expect(traceData.baggage).toContain(`sentry-trace_id=${trace_id}`); + + expect(traceData.metaTags).toContain(``); + expect(traceData.metaTags).toContain(`sentr y-trace_id=${trace_id}`); + expect(traceData.metaTags).not.toContain('sentry-sampled='); + }, + }) + .start() + .makeRequest('get', '/test'); + }); - console.log(runner.getLogs()); + test('outside of a request handler', done => { + createRunner(__dirname, 'no-server.js') + .expect({ + event: event => { + const { contexts } = event; + const { trace_id, span_id } = contexts?.trace || {}; + expect(trace_id).toMatch(/^[a-f0-9]{32}$/); + expect(span_id).toMatch(/^[a-f0-9]{16}$/); + + const traceData = contexts?.traceData || {}; + + expect(traceData['sentry-trace']).toEqual(`${trace_id}-${span_id}`); + expect(traceData.baggage).toContain(`sentry-trace_id=${trace_id}`); + + expect(traceData.metaTags).toContain(``); + expect(traceData.metaTags).toContain(`sentry-trace_id=${trace_id}`); + expect(traceData.metaTags).not.toContain('sentry-sampled='); + }, + }) + .start(done); }); }); From d3aa88c537a3a4f915a7a0c1f084409e3a64bf05 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 18 Sep 2024 15:16:42 +0200 Subject: [PATCH 13/13] lint --- .../suites/tracing/meta-tags-twp-errors/server.js | 2 +- .../suites/tracing/meta-tags-twp-errors/test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js index 95ecb620c3ed..19877ffe3613 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/server.js @@ -21,7 +21,7 @@ const express = require('express'); const app = express(); -app.get('/test', (_req, res) => { +app.get('/test', () => { throw new Error('test error'); }); diff --git a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts index 793aada386f3..e6c0bfff822d 100644 --- a/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/meta-tags-twp-errors/test.ts @@ -6,7 +6,7 @@ describe('errors in TwP mode have same trace in trace context and getTraceData() }); test('in incoming request', async () => { - const runner = createRunner(__dirname, 'server.js') + createRunner(__dirname, 'server.js') .expect({ event: event => { const { contexts } = event;