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, 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(() => {}); });