|
| 1 | +import { SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME } from '@sentry/core'; |
| 2 | +import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/node'; |
| 3 | +import { cleanupChildProcesses, createRunner } from '../../../../utils/runner'; |
| 4 | + |
| 5 | +describe('express tracing', () => { |
| 6 | + afterAll(() => { |
| 7 | + cleanupChildProcesses(); |
| 8 | + }); |
| 9 | + |
| 10 | + describe('CJS', () => { |
| 11 | + // This test documents the unfortunate behaviour of using `span.updateName` on the server-side. |
| 12 | + // For http.server root spans (which is the root span on the server 99% of the time), Otel's http instrumentation |
| 13 | + // calls `span.updateName` and overwrites whatever the name was set to before (by us or by users). |
| 14 | + test("calling just `span.updateName` doesn't update the final name in express (missing source)", done => { |
| 15 | + createRunner(__dirname, 'server.js') |
| 16 | + .expect({ |
| 17 | + transaction: { |
| 18 | + transaction: 'GET /test/:id/span-updateName', |
| 19 | + transaction_info: { |
| 20 | + source: 'route', |
| 21 | + }, |
| 22 | + }, |
| 23 | + }) |
| 24 | + .start(done) |
| 25 | + .makeRequest('get', '/test/123/span-updateName'); |
| 26 | + }); |
| 27 | + |
| 28 | + // Also calling `updateName` AND setting a source doesn't change anything - Otel has no concept of source, this is sentry-internal. |
| 29 | + // Therefore, only the source is updated but the name is still overwritten by Otel. |
| 30 | + test("calling `span.updateName` and setting attribute source doesn't update the final name in express but it updates the source", done => { |
| 31 | + createRunner(__dirname, 'server.js') |
| 32 | + .expect({ |
| 33 | + transaction: { |
| 34 | + transaction: 'GET /test/:id/span-updateName-source', |
| 35 | + transaction_info: { |
| 36 | + source: 'custom', |
| 37 | + }, |
| 38 | + }, |
| 39 | + }) |
| 40 | + .start(done) |
| 41 | + .makeRequest('get', '/test/123/span-updateName-source'); |
| 42 | + }); |
| 43 | + |
| 44 | + // This test documents the correct way to update the span name (and implicitly the source) in Node: |
| 45 | + test('calling `Sentry.updateSpanName` updates the final name and source in express', done => { |
| 46 | + createRunner(__dirname, 'server.js') |
| 47 | + .expect({ |
| 48 | + transaction: txnEvent => { |
| 49 | + expect(txnEvent).toMatchObject({ |
| 50 | + transaction: 'new-name', |
| 51 | + transaction_info: { |
| 52 | + source: 'custom', |
| 53 | + }, |
| 54 | + contexts: { |
| 55 | + trace: { |
| 56 | + op: 'http.server', |
| 57 | + data: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'custom' }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }); |
| 61 | + // ensure we delete the internal attribute once we're done with it |
| 62 | + expect(txnEvent.contexts?.trace?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME]).toBeUndefined(); |
| 63 | + }, |
| 64 | + }) |
| 65 | + .start(done) |
| 66 | + .makeRequest('get', '/test/123/updateSpanName'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + // This test documents the correct way to update the span name (and implicitly the source) in Node: |
| 71 | + test('calling `Sentry.updateSpanName` and setting source subsequently updates the final name and sets correct source', done => { |
| 72 | + createRunner(__dirname, 'server.js') |
| 73 | + .expect({ |
| 74 | + transaction: txnEvent => { |
| 75 | + expect(txnEvent).toMatchObject({ |
| 76 | + transaction: 'new-name', |
| 77 | + transaction_info: { |
| 78 | + source: 'component', |
| 79 | + }, |
| 80 | + contexts: { |
| 81 | + trace: { |
| 82 | + op: 'http.server', |
| 83 | + data: { [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'component' }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }); |
| 87 | + // ensure we delete the internal attribute once we're done with it |
| 88 | + expect(txnEvent.contexts?.trace?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_CUSTOM_SPAN_NAME]).toBeUndefined(); |
| 89 | + }, |
| 90 | + }) |
| 91 | + .start(done) |
| 92 | + .makeRequest('get', '/test/123/updateSpanNameAndSource'); |
| 93 | + }); |
| 94 | +}); |
0 commit comments