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); }); });