From d6becc675ac2ea30fe989980e7e02c05b018ad50 Mon Sep 17 00:00:00 2001 From: s1gr1d <32902192+s1gr1d@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:15:15 +0100 Subject: [PATCH 1/2] test(node-integration): Collect traces that arrive in several envelopes With span streaming, the SDK flushes 500 ms after any segment ends, so the spans of one trace can arrive in more than one envelope. Four apollo-graphql tests and one Mastra test asserted the whole trace on the first envelope. They now collect the spans of the trace until its segment arrives, and assert on the collected spans. --- .../useOperationNameForRootSpan/test.ts | 320 +++++++++--------- .../suites/tracing/mastra/nesting/test.ts | 57 ++-- 2 files changed, 184 insertions(+), 193 deletions(-) diff --git a/dev-packages/node-integration-tests/suites/tracing/apollo-graphql/useOperationNameForRootSpan/test.ts b/dev-packages/node-integration-tests/suites/tracing/apollo-graphql/useOperationNameForRootSpan/test.ts index 46a14b5b8f2b..5072c43b79e5 100644 --- a/dev-packages/node-integration-tests/suites/tracing/apollo-graphql/useOperationNameForRootSpan/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/apollo-graphql/useOperationNameForRootSpan/test.ts @@ -9,42 +9,41 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => { describe('single query operation', () => { createEsmAndCjsTests(__dirname, 'scenario-query.mjs', 'instrument.mjs', (createTestRunner, test) => { test('useOperationNameForRootSpan works with single query operation', async () => { - await createTestRunner() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'Test Server Start')).toBeDefined(); - const segment = container.items.find(span => span.is_segment && span.name === 'test span name'); - const children = container.items.filter( - span => !span.is_segment && span.attributes['sentry.segment.id']?.value === segment?.span_id, - ); - - expect(segment?.attributes['sentry.graphql.operation']).toEqual({ - value: 'query GetHello', - type: 'string', - }); - const getHelloSpan = children.find( - span => - span.attributes['graphql.processing.type']?.value === 'execute' && - span.attributes['graphql.operation.name']?.value === 'GetHello', - ); - expect(getHelloSpan?.name).toBe('GraphQL query'); - expect(getHelloSpan?.status).toBe('ok'); - expect(getHelloSpan?.attributes['graphql.operation.name']).toEqual({ value: 'GetHello', type: 'string' }); - expect(getHelloSpan?.attributes['graphql.operation.type']).toEqual({ value: 'query', type: 'string' }); - expect(getHelloSpan?.attributes['graphql.document']).toEqual({ - value: 'query GetHello {hello}', - type: 'string', - }); - expect(getHelloSpan?.attributes['sentry.origin']).toEqual({ - value: 'auto.graphql.graphql', - type: 'string', - }); - expect(getHelloSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); - expect(getHelloSpan?.attributes['graphql.processing.type']).toEqual({ value: 'execute', type: 'string' }); - }, - }) - .start() - .completed(); + const runner = createTestRunner(); + const spansPromise = runner.collectStreamedSpansUntilSegment('test span name'); + + await runner.start().completed(); + + const spans = await spansPromise; + expect(spans.find(span => span.is_segment && span.name === 'Test Server Start')).toBeDefined(); + const segment = spans.find(span => span.is_segment && span.name === 'test span name'); + const children = spans.filter( + span => !span.is_segment && span.attributes['sentry.segment.id']?.value === segment?.span_id, + ); + + expect(segment?.attributes['sentry.graphql.operation']).toEqual({ + value: 'query GetHello', + type: 'string', + }); + const getHelloSpan = children.find( + span => + span.attributes['graphql.processing.type']?.value === 'execute' && + span.attributes['graphql.operation.name']?.value === 'GetHello', + ); + expect(getHelloSpan?.name).toBe('GraphQL query'); + expect(getHelloSpan?.status).toBe('ok'); + expect(getHelloSpan?.attributes['graphql.operation.name']).toEqual({ value: 'GetHello', type: 'string' }); + expect(getHelloSpan?.attributes['graphql.operation.type']).toEqual({ value: 'query', type: 'string' }); + expect(getHelloSpan?.attributes['graphql.document']).toEqual({ + value: 'query GetHello {hello}', + type: 'string', + }); + expect(getHelloSpan?.attributes['sentry.origin']).toEqual({ + value: 'auto.graphql.graphql', + type: 'string', + }); + expect(getHelloSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); + expect(getHelloSpan?.attributes['graphql.processing.type']).toEqual({ value: 'execute', type: 'string' }); }); }); }); @@ -52,51 +51,50 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => { describe('single mutation operation', () => { createEsmAndCjsTests(__dirname, 'scenario-mutation.mjs', 'instrument.mjs', (createTestRunner, test) => { test('useOperationNameForRootSpan works with single mutation operation', async () => { - await createTestRunner() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'Test Server Start')).toBeDefined(); - const segment = container.items.find(span => span.is_segment && span.name === 'test span name'); - const children = container.items.filter( - span => !span.is_segment && span.attributes['sentry.segment.id']?.value === segment?.span_id, - ); - - expect(segment?.attributes['sentry.graphql.operation']).toEqual({ - value: 'mutation TestMutation', - type: 'string', - }); - const testMutationSpan = children.find( - span => - span.attributes['graphql.processing.type']?.value === 'execute' && - span.attributes['graphql.operation.name']?.value === 'TestMutation', - ); - expect(testMutationSpan?.name).toBe('GraphQL mutation'); - expect(testMutationSpan?.status).toBe('ok'); - expect(testMutationSpan?.attributes['graphql.operation.name']).toEqual({ - value: 'TestMutation', - type: 'string', - }); - expect(testMutationSpan?.attributes['graphql.operation.type']).toEqual({ - value: 'mutation', - type: 'string', - }); - expect(testMutationSpan?.attributes['graphql.document']).toEqual({ - value: 'mutation TestMutation($email: String) {\n login(email: $email)\n}', - type: 'string', - }); - expect(testMutationSpan?.attributes['sentry.origin']).toEqual({ - value: 'auto.graphql.graphql', - type: 'string', - }); - expect(testMutationSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); - expect(testMutationSpan?.attributes['graphql.processing.type']).toEqual({ - value: 'execute', - type: 'string', - }); - }, - }) - .start() - .completed(); + const runner = createTestRunner(); + const spansPromise = runner.collectStreamedSpansUntilSegment('test span name'); + + await runner.start().completed(); + + const spans = await spansPromise; + expect(spans.find(span => span.is_segment && span.name === 'Test Server Start')).toBeDefined(); + const segment = spans.find(span => span.is_segment && span.name === 'test span name'); + const children = spans.filter( + span => !span.is_segment && span.attributes['sentry.segment.id']?.value === segment?.span_id, + ); + + expect(segment?.attributes['sentry.graphql.operation']).toEqual({ + value: 'mutation TestMutation', + type: 'string', + }); + const testMutationSpan = children.find( + span => + span.attributes['graphql.processing.type']?.value === 'execute' && + span.attributes['graphql.operation.name']?.value === 'TestMutation', + ); + expect(testMutationSpan?.name).toBe('GraphQL mutation'); + expect(testMutationSpan?.status).toBe('ok'); + expect(testMutationSpan?.attributes['graphql.operation.name']).toEqual({ + value: 'TestMutation', + type: 'string', + }); + expect(testMutationSpan?.attributes['graphql.operation.type']).toEqual({ + value: 'mutation', + type: 'string', + }); + expect(testMutationSpan?.attributes['graphql.document']).toEqual({ + value: 'mutation TestMutation($email: String) {\n login(email: $email)\n}', + type: 'string', + }); + expect(testMutationSpan?.attributes['sentry.origin']).toEqual({ + value: 'auto.graphql.graphql', + type: 'string', + }); + expect(testMutationSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); + expect(testMutationSpan?.attributes['graphql.processing.type']).toEqual({ + value: 'execute', + type: 'string', + }); }); }); }); @@ -104,33 +102,30 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => { describe('query without name', () => { createEsmAndCjsTests(__dirname, 'scenario-no-operation-name.mjs', 'instrument.mjs', (createTestRunner, test) => { test('useOperationNameForRootSpan works with single query operation without name', async () => { - await createTestRunner() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'Test Server Start')).toBeDefined(); - const segment = container.items.find(span => span.is_segment && span.name === 'test span name'); - const children = container.items.filter( - span => !span.is_segment && span.attributes['sentry.segment.id']?.value === segment?.span_id, - ); - - expect(segment?.attributes['sentry.graphql.operation']).toEqual({ value: 'query', type: 'string' }); - const executeSpan = children.find( - span => span.attributes['graphql.processing.type']?.value === 'execute', - ); - expect(executeSpan?.name).toBe('GraphQL query'); - expect(executeSpan?.status).toBe('ok'); - expect(executeSpan?.attributes['graphql.operation.type']).toEqual({ value: 'query', type: 'string' }); - expect(executeSpan?.attributes['graphql.document']).toEqual({ value: 'query {hello}', type: 'string' }); - expect(executeSpan?.attributes['sentry.origin']).toEqual({ - value: 'auto.graphql.graphql', - type: 'string', - }); - expect(executeSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); - expect(executeSpan?.attributes['graphql.processing.type']).toEqual({ value: 'execute', type: 'string' }); - }, - }) - .start() - .completed(); + const runner = createTestRunner(); + const spansPromise = runner.collectStreamedSpansUntilSegment('test span name'); + + await runner.start().completed(); + + const spans = await spansPromise; + expect(spans.find(span => span.is_segment && span.name === 'Test Server Start')).toBeDefined(); + const segment = spans.find(span => span.is_segment && span.name === 'test span name'); + const children = spans.filter( + span => !span.is_segment && span.attributes['sentry.segment.id']?.value === segment?.span_id, + ); + + expect(segment?.attributes['sentry.graphql.operation']).toEqual({ value: 'query', type: 'string' }); + const executeSpan = children.find(span => span.attributes['graphql.processing.type']?.value === 'execute'); + expect(executeSpan?.name).toBe('GraphQL query'); + expect(executeSpan?.status).toBe('ok'); + expect(executeSpan?.attributes['graphql.operation.type']).toEqual({ value: 'query', type: 'string' }); + expect(executeSpan?.attributes['graphql.document']).toEqual({ value: 'query {hello}', type: 'string' }); + expect(executeSpan?.attributes['sentry.origin']).toEqual({ + value: 'auto.graphql.graphql', + type: 'string', + }); + expect(executeSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); + expect(executeSpan?.attributes['graphql.processing.type']).toEqual({ value: 'execute', type: 'string' }); }); }); }); @@ -138,61 +133,60 @@ describe('GraphQL/Apollo Tests > useOperationNameForRootSpan', () => { describe('multiple operations', () => { createEsmAndCjsTests(__dirname, 'scenario-multiple-operations.mjs', 'instrument.mjs', (createTestRunner, test) => { test('useOperationNameForRootSpan works with multiple query operations', async () => { - await createTestRunner() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'Test Server Start')).toBeDefined(); - const segment = container.items.find(span => span.is_segment && span.name === 'test span name'); - const children = container.items.filter( - span => !span.is_segment && span.attributes['sentry.segment.id']?.value === segment?.span_id, - ); - - expect(segment?.attributes['sentry.graphql.operation']).toEqual({ - value: ['query GetWorld', 'query GetHello'], - type: 'array', - }); - const getHelloSpan = children.find( - span => - span.attributes['graphql.processing.type']?.value === 'execute' && - span.attributes['graphql.operation.name']?.value === 'GetHello', - ); - expect(getHelloSpan?.name).toBe('GraphQL query'); - expect(getHelloSpan?.status).toBe('ok'); - expect(getHelloSpan?.attributes['graphql.operation.name']).toEqual({ value: 'GetHello', type: 'string' }); - expect(getHelloSpan?.attributes['graphql.operation.type']).toEqual({ value: 'query', type: 'string' }); - expect(getHelloSpan?.attributes['graphql.document']).toEqual({ - value: 'query GetHello {hello}', - type: 'string', - }); - expect(getHelloSpan?.attributes['sentry.origin']).toEqual({ - value: 'auto.graphql.graphql', - type: 'string', - }); - expect(getHelloSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); - expect(getHelloSpan?.attributes['graphql.processing.type']).toEqual({ value: 'execute', type: 'string' }); - const getWorldSpan = children.find( - span => - span.attributes['graphql.processing.type']?.value === 'execute' && - span.attributes['graphql.operation.name']?.value === 'GetWorld', - ); - expect(getWorldSpan?.name).toBe('GraphQL query'); - expect(getWorldSpan?.status).toBe('ok'); - expect(getWorldSpan?.attributes['graphql.operation.name']).toEqual({ value: 'GetWorld', type: 'string' }); - expect(getWorldSpan?.attributes['graphql.operation.type']).toEqual({ value: 'query', type: 'string' }); - expect(getWorldSpan?.attributes['graphql.document']).toEqual({ - value: 'query GetWorld {world}', - type: 'string', - }); - expect(getWorldSpan?.attributes['sentry.origin']).toEqual({ - value: 'auto.graphql.graphql', - type: 'string', - }); - expect(getWorldSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); - expect(getWorldSpan?.attributes['graphql.processing.type']).toEqual({ value: 'execute', type: 'string' }); - }, - }) - .start() - .completed(); + const runner = createTestRunner(); + const spansPromise = runner.collectStreamedSpansUntilSegment('test span name'); + + await runner.start().completed(); + + const spans = await spansPromise; + expect(spans.find(span => span.is_segment && span.name === 'Test Server Start')).toBeDefined(); + const segment = spans.find(span => span.is_segment && span.name === 'test span name'); + const children = spans.filter( + span => !span.is_segment && span.attributes['sentry.segment.id']?.value === segment?.span_id, + ); + + expect(segment?.attributes['sentry.graphql.operation']).toEqual({ + value: ['query GetWorld', 'query GetHello'], + type: 'array', + }); + const getHelloSpan = children.find( + span => + span.attributes['graphql.processing.type']?.value === 'execute' && + span.attributes['graphql.operation.name']?.value === 'GetHello', + ); + expect(getHelloSpan?.name).toBe('GraphQL query'); + expect(getHelloSpan?.status).toBe('ok'); + expect(getHelloSpan?.attributes['graphql.operation.name']).toEqual({ value: 'GetHello', type: 'string' }); + expect(getHelloSpan?.attributes['graphql.operation.type']).toEqual({ value: 'query', type: 'string' }); + expect(getHelloSpan?.attributes['graphql.document']).toEqual({ + value: 'query GetHello {hello}', + type: 'string', + }); + expect(getHelloSpan?.attributes['sentry.origin']).toEqual({ + value: 'auto.graphql.graphql', + type: 'string', + }); + expect(getHelloSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); + expect(getHelloSpan?.attributes['graphql.processing.type']).toEqual({ value: 'execute', type: 'string' }); + const getWorldSpan = children.find( + span => + span.attributes['graphql.processing.type']?.value === 'execute' && + span.attributes['graphql.operation.name']?.value === 'GetWorld', + ); + expect(getWorldSpan?.name).toBe('GraphQL query'); + expect(getWorldSpan?.status).toBe('ok'); + expect(getWorldSpan?.attributes['graphql.operation.name']).toEqual({ value: 'GetWorld', type: 'string' }); + expect(getWorldSpan?.attributes['graphql.operation.type']).toEqual({ value: 'query', type: 'string' }); + expect(getWorldSpan?.attributes['graphql.document']).toEqual({ + value: 'query GetWorld {world}', + type: 'string', + }); + expect(getWorldSpan?.attributes['sentry.origin']).toEqual({ + value: 'auto.graphql.graphql', + type: 'string', + }); + expect(getWorldSpan?.attributes['sentry.op']).toEqual({ value: 'graphql', type: 'string' }); + expect(getWorldSpan?.attributes['graphql.processing.type']).toEqual({ value: 'execute', type: 'string' }); }); }); }); diff --git a/dev-packages/node-integration-tests/suites/tracing/mastra/nesting/test.ts b/dev-packages/node-integration-tests/suites/tracing/mastra/nesting/test.ts index 5e2aee55e7d8..e112e271d68f 100644 --- a/dev-packages/node-integration-tests/suites/tracing/mastra/nesting/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/mastra/nesting/test.ts @@ -125,36 +125,33 @@ describe.runIf(supports({ min: 22 }))('Mastra integration', () => { // that instance's own `execute_tool` span — if the registry cross-talked, one tool span would // have no dataloader child. - await createRunner() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'mastra-multi')).toBeDefined(); - const loadSpans = container.items.filter( - span => - span.attributes[SENTRY_OP]?.value === 'cache.get' && - span.attributes[DB_OPERATION_NAME]?.value === 'load', - ); - const loadParentIds = loadSpans.map(span => span.parent_span_id); - const toolSpanIds: Record = {}; - for (const toolName of ['count_a', 'count_b']) { - const toolSpan = container.items.find(span => span.attributes[GEN_AI_TOOL_NAME]?.value === toolName)!; - expect(toolSpan).toBeDefined(); - toolSpanIds[toolName] = toolSpan.span_id; - } - - // Each instance's tool span parents at least one dataloader load — neither is starved. - for (const toolName of ['count_a', 'count_b']) { - expect(loadParentIds).toContain(toolSpanIds[toolName]); - } - // And every load parents onto one of the two tool spans (no stray reparenting). - const toolIds = new Set(Object.values(toolSpanIds)); - for (const parent of loadParentIds) { - expect(toolIds.has(parent!)).toBe(true); - } - }, - }) - .start() - .completed(); + const runner = createRunner(); + const spansPromise = runner.collectStreamedSpansUntilSegment('mastra-multi'); + + await runner.start().completed(); + + const spans = await spansPromise; + const loadSpans = spans.filter( + span => + span.attributes[SENTRY_OP]?.value === 'cache.get' && span.attributes[DB_OPERATION_NAME]?.value === 'load', + ); + const loadParentIds = loadSpans.map(span => span.parent_span_id); + const toolSpanIds: Record = {}; + for (const toolName of ['count_a', 'count_b']) { + const toolSpan = spans.find(span => span.attributes[GEN_AI_TOOL_NAME]?.value === toolName)!; + expect(toolSpan).toBeDefined(); + toolSpanIds[toolName] = toolSpan.span_id; + } + + // Each instance's tool span parents at least one dataloader load — neither is starved. + for (const toolName of ['count_a', 'count_b']) { + expect(loadParentIds).toContain(toolSpanIds[toolName]); + } + // And every load parents onto one of the two tool spans (no stray reparenting). + const toolIds = new Set(Object.values(toolSpanIds)); + for (const parent of loadParentIds) { + expect(toolIds.has(parent!)).toBe(true); + } }); }, MASTRA_NESTING_DEPENDENCIES, From e7ea8aa8c992d141741eeb214c03e418ccf16b3f Mon Sep 17 00:00:00 2001 From: s1gr1d <32902192+s1gr1d@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:15:15 +0100 Subject: [PATCH 2/2] test(node-integration): Let a scenario exit after its final flush `flush()` sets a timer, so `beforeExit` fired again after each flush and the scenario process never exited on its own. A test with an expectation that is not met then waited for the full test timeout, and now fails when the process exits. --- .../node-integration-tests/utils/runner/auto-flush.cjs | 5 ++++- .../node-integration-tests/utils/runner/auto-flush.mjs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dev-packages/node-integration-tests/utils/runner/auto-flush.cjs b/dev-packages/node-integration-tests/utils/runner/auto-flush.cjs index 5d3bd67f9706..64a7d237eb2b 100644 --- a/dev-packages/node-integration-tests/utils/runner/auto-flush.cjs +++ b/dev-packages/node-integration-tests/utils/runner/auto-flush.cjs @@ -13,7 +13,10 @@ // `auto-flush.mjs` whose `import` resolves to the ESM client. They don't // conflict — only the active module-system's client has events to flush; the // other no-ops because `getClient()` returns undefined there. -process.on('beforeExit', () => { +// +// `flush()` always sets a timer, so `beforeExit` fires again after each flush. `once` lets the +// process exit after the first flush. +process.once('beforeExit', () => { let Sentry; try { Sentry = require('@sentry/node'); diff --git a/dev-packages/node-integration-tests/utils/runner/auto-flush.mjs b/dev-packages/node-integration-tests/utils/runner/auto-flush.mjs index 6446e682c53a..d7b59f4b7ace 100644 --- a/dev-packages/node-integration-tests/utils/runner/auto-flush.mjs +++ b/dev-packages/node-integration-tests/utils/runner/auto-flush.mjs @@ -3,6 +3,6 @@ // the SDK instance we flush is the same one the scenario's `import` resolves. import * as Sentry from '@sentry/node'; -process.on('beforeExit', () => { +process.once('beforeExit', () => { Sentry.flush(2000).catch(() => {}); });