diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-azure-openai.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-azure-openai.mjs index fa34b15c27e7..866f0557baa0 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-azure-openai.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-azure-openai.mjs @@ -12,7 +12,7 @@ function startMockOpenAiServer() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockOpenAiServer(); const client = new AzureOpenAI({ @@ -36,5 +36,3 @@ async function run() { server.close(); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-chat.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-chat.mjs index 9119499bb87c..f33e7fcfe9d8 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-chat.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-chat.mjs @@ -129,7 +129,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { @@ -236,5 +236,3 @@ async function run() { server.close(); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-conversation.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-conversation.mjs index 6edf85bdf46f..79631131b3bd 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-conversation.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-conversation.mjs @@ -33,7 +33,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); await Sentry.startSpan({ op: 'function', name: 'conversation-test' }, async () => { @@ -70,5 +70,3 @@ async function run() { server.close(); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-embeddings.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-embeddings.mjs index ea6995e4331f..29673df26d7b 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-embeddings.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-embeddings.mjs @@ -24,7 +24,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { @@ -68,5 +68,3 @@ async function run() { server.close(); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-manual-conversation-id.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-manual-conversation-id.mjs index 265ad8de6cd1..95196e3b774d 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-manual-conversation-id.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-manual-conversation-id.mjs @@ -17,7 +17,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); // Test: Multiple chat completions in the same conversation with manual conversation ID @@ -52,5 +52,3 @@ async function run() { server.close(); await Sentry.flush(2000); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-root-span.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-root-span.mjs index 998adadb8929..94af0f3fadcf 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-root-span.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-root-span.mjs @@ -12,7 +12,7 @@ function startMockOpenAiServer() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockOpenAiServer(); const client = new OpenAI({ @@ -35,5 +35,3 @@ async function run() { server.close(); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-1.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-1.mjs index ef3fb880805f..f36a13e35d21 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-1.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-1.mjs @@ -17,7 +17,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); const client = new OpenAI({ baseURL: `${server.url}/openai`, @@ -47,5 +47,3 @@ async function run() { server.close(); await Sentry.flush(2000); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-2.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-2.mjs index cb3c2e4c8331..feeb6338608f 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-2.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-2.mjs @@ -17,7 +17,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); const client = new OpenAI({ baseURL: `${server.url}/openai`, @@ -47,5 +47,3 @@ async function run() { server.close(); await Sentry.flush(2000); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-system-instructions.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-system-instructions.mjs index f51bee1b9442..04500d7aa876 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-system-instructions.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-system-instructions.mjs @@ -22,7 +22,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { @@ -42,5 +42,3 @@ async function run() { server.close(); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-with-response.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-with-response.mjs index 10c50f106d37..c3ee1ba95695 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/scenario-with-response.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/scenario-with-response.mjs @@ -29,7 +29,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { @@ -132,5 +132,3 @@ async function run() { server.close(); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/v5/chat/test.ts b/dev-packages/node-integration-tests/suites/tracing/openai/v5/chat/test.ts index 01d7174e522f..dd16f8aea80b 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/v5/chat/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/openai/v5/chat/test.ts @@ -29,12 +29,48 @@ describe('OpenAI integration', () => { afterAll(() => { cleanupChildProcesses(); }); - createEsmAndCjsTests(join(__dirname, '../..'), 'scenario-chat.mjs', 'instrument.mjs', (createRunner, test) => { - test('creates openai related spans with genAI recording disabled', async () => { + const defaultScenarios = [ + 'scenario-chat.mjs', + 'scenario-embeddings.mjs', + 'scenario-azure-openai.mjs', + 'scenario-with-response.mjs', + 'scenario-root-span.mjs', + 'scenario-conversation.mjs', + 'scenario-manual-conversation-id.mjs', + 'scenario-separate-scope-1.mjs', + 'scenario-separate-scope-2.mjs', + ]; + const recordingScenarios = ['scenario-chat.mjs', 'scenario-embeddings.mjs', 'scenario-system-instructions.mjs']; + createEsmAndCjsTests(join(__dirname, '../..'), defaultScenarios, 'instrument.mjs', (createRunner, test) => { + let scenarios: ReturnType | undefined; + async function runScenarios() { const runner = createRunner(); - const spansPromise = runner.collectStreamedSpansUntilSegment('main'); + const segmentNames: Record = { + 'scenario-conversation.mjs': 'conversation-test', + 'scenario-manual-conversation-id.mjs': 'chat-with-manual-conversation-id', + 'scenario-separate-scope-1.mjs': 'GET /chat/conversation-1', + 'scenario-separate-scope-2.mjs': 'GET /chat/conversation-2', + }; + + const collectors = defaultScenarios.map(name => + runner.collectStreamedSpansUntilSegment( + span => + span.attributes['test.scenario']?.value === name && + (name === 'scenario-azure-openai.mjs' || name === 'scenario-root-span.mjs' + ? span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai' + : span.name === (segmentNames[name] ?? 'main')), + ), + ); await runner.start().completed(); + return { spans: await Promise.all(collectors) }; + } + const getScenarios = () => (scenarios ??= runScenarios()); + + test('creates openai related spans with genAI recording disabled', async () => { + const spansPromise = getScenarios().then(result => result.spans[0]!); + + await getScenarios(); const allSpans = await spansPromise; const spans = allSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); @@ -442,17 +478,92 @@ describe('OpenAI integration', () => { expect(span.attributes[GEN_AI_EMBEDDINGS_INPUT]).toBeUndefined(); } }); + + test('it works with Azure OpenAI', async () => { + let receivedSpans: SerializedStreamedSpanContainer['items'] = []; + + const container = { items: (await getScenarios()).spans[2]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'chat gpt-3.5-turbo')).toBeDefined(); + receivedSpans = container.items; + + const segment = receivedSpans.find(span => span.is_segment && span.name === 'chat gpt-3.5-turbo'); + expect(segment?.span_id).toEqual(expect.any(String)); + expect(segment?.trace_id).toEqual(expect.any(String)); + expect(segment?.attributes[GEN_AI_OPERATION_NAME]).toEqual({ value: 'chat', type: 'string' }); + expect(segment?.attributes[SENTRY_OP]).toEqual({ value: 'gen_ai.chat', type: 'string' }); + expect(segment?.attributes[SENTRY_ORIGIN]).toEqual({ value: 'auto.ai.openai', type: 'string' }); + expect(segment?.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ value: 'openai', type: 'string' }); + expect(segment?.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ value: 'gpt-3.5-turbo', type: 'string' }); + expect(segment?.attributes[GEN_AI_REQUEST_TEMPERATURE]).toEqual({ value: 0.7, type: 'double' }); + expect(segment?.attributes[GEN_AI_RESPONSE_MODEL]).toEqual({ value: 'gpt-3.5-turbo', type: 'string' }); + expect(segment?.attributes[GEN_AI_RESPONSE_ID]).toEqual({ value: 'chatcmpl-mock123', type: 'string' }); + expect(segment?.attributes[GEN_AI_RESPONSE_FINISH_REASONS]).toEqual({ value: '["stop"]', type: 'string' }); + expect(segment?.attributes[GEN_AI_USAGE_INPUT_TOKENS]).toEqual({ value: 10, type: 'integer' }); + expect(segment?.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]).toEqual({ value: 15, type: 'integer' }); + expect(segment?.attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toEqual({ value: 25, type: 'integer' }); + expect(segment?.status).toEqual('ok'); + }); + + test('preserves .withResponse() method and works correctly', async () => { + let receivedSpans: SerializedStreamedSpanContainer['items'] = []; + + const container = { items: (await getScenarios()).spans[3]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); + receivedSpans = container.items; + + const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); + expect(spans).toHaveLength(2); + + for (const span of spans) { + expect(span!.name).toBe('chat gpt-4'); + expect(span!.status).toBe('ok'); + expect(span!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ type: 'string', value: 'chat' }); + expect(span!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ type: 'string', value: 'gpt-4' }); + } + + const parsedSpans = spans.filter(span => span.attributes[GEN_AI_RESPONSE_ID] !== undefined); + expect(parsedSpans).toHaveLength(1); + expect(parsedSpans[0]!.attributes[GEN_AI_RESPONSE_ID]).toEqual({ + type: 'string', + value: 'chatcmpl-withresponse', + }); + + const rawSpans = spans.filter(span => span.attributes[GEN_AI_RESPONSE_ID] === undefined); + expect(rawSpans).toHaveLength(1); + expect(rawSpans[0]!.attributes[GEN_AI_RESPONSE_TEXT]).toBeUndefined(); + expect(rawSpans[0]!.attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toBeUndefined(); + }); }); + createEsmAndCjsTests( join(__dirname, '../..'), - 'scenario-chat.mjs', + recordingScenarios, 'instrument-with-pii.mjs', (createRunner, test) => { - test('creates openai related spans with genAI recording enabled', async () => { + let scenarios: ReturnType | undefined; + async function runScenarios() { const runner = createRunner(); - const spansPromise = runner.collectStreamedSpansUntilSegment('main'); + const collectors = recordingScenarios.map(name => + runner.collectStreamedSpansUntilSegment( + span => + span.attributes['test.scenario']?.value === name && + (name === 'scenario-azure-openai.mjs' + ? span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai' + : span.name === 'main'), + ), + ); await runner.start().completed(); + return { spans: await Promise.all(collectors) }; + } + const getScenarios = () => (scenarios ??= runScenarios()); + + test('creates openai related spans with genAI recording enabled', async () => { + const spansPromise = getScenarios().then(result => result.spans[0]!); + + await getScenarios(); const allSpans = await spansPromise; const spans = allSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); @@ -917,9 +1028,10 @@ describe('OpenAI integration', () => { }); }, ); + createEsmAndCjsTests( join(__dirname, '../..'), - 'scenario-chat.mjs', + ['scenario-chat.mjs'], 'instrument-with-options.mjs', (createRunner, test) => { test('creates openai related spans with custom options', async () => { @@ -1006,87 +1118,7 @@ describe('OpenAI integration', () => { }); }, ); - createEsmAndCjsTests( - join(__dirname, '../..'), - 'scenario-azure-openai.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('it works with Azure OpenAI', async () => { - let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - - await createRunner() - .unordered() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'chat gpt-3.5-turbo')).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); - - const segment = receivedSpans.find(span => span.is_segment && span.name === 'chat gpt-3.5-turbo'); - expect(segment?.span_id).toEqual(expect.any(String)); - expect(segment?.trace_id).toEqual(expect.any(String)); - expect(segment?.attributes[GEN_AI_OPERATION_NAME]).toEqual({ value: 'chat', type: 'string' }); - expect(segment?.attributes[SENTRY_OP]).toEqual({ value: 'gen_ai.chat', type: 'string' }); - expect(segment?.attributes[SENTRY_ORIGIN]).toEqual({ value: 'auto.ai.openai', type: 'string' }); - expect(segment?.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ value: 'openai', type: 'string' }); - expect(segment?.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ value: 'gpt-3.5-turbo', type: 'string' }); - expect(segment?.attributes[GEN_AI_REQUEST_TEMPERATURE]).toEqual({ value: 0.7, type: 'double' }); - expect(segment?.attributes[GEN_AI_RESPONSE_MODEL]).toEqual({ value: 'gpt-3.5-turbo', type: 'string' }); - expect(segment?.attributes[GEN_AI_RESPONSE_ID]).toEqual({ value: 'chatcmpl-mock123', type: 'string' }); - expect(segment?.attributes[GEN_AI_RESPONSE_FINISH_REASONS]).toEqual({ value: '["stop"]', type: 'string' }); - expect(segment?.attributes[GEN_AI_USAGE_INPUT_TOKENS]).toEqual({ value: 10, type: 'integer' }); - expect(segment?.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]).toEqual({ value: 15, type: 'integer' }); - expect(segment?.attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toEqual({ value: 25, type: 'integer' }); - expect(segment?.status).toEqual('ok'); - }); - }, - ); - createEsmAndCjsTests( - join(__dirname, '../..'), - 'scenario-with-response.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('preserves .withResponse() method and works correctly', async () => { - let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - - await createRunner() - .unordered() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); - const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); - expect(spans).toHaveLength(2); - - for (const span of spans) { - expect(span!.name).toBe('chat gpt-4'); - expect(span!.status).toBe('ok'); - expect(span!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ type: 'string', value: 'chat' }); - expect(span!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ type: 'string', value: 'gpt-4' }); - } - - const parsedSpans = spans.filter(span => span.attributes[GEN_AI_RESPONSE_ID] !== undefined); - expect(parsedSpans).toHaveLength(1); - expect(parsedSpans[0]!.attributes[GEN_AI_RESPONSE_ID]).toEqual({ - type: 'string', - value: 'chatcmpl-withresponse', - }); - - const rawSpans = spans.filter(span => span.attributes[GEN_AI_RESPONSE_ID] === undefined); - expect(rawSpans).toHaveLength(1); - expect(rawSpans[0]!.attributes[GEN_AI_RESPONSE_TEXT]).toBeUndefined(); - expect(rawSpans[0]!.attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toBeUndefined(); - }); - }, - ); createEsmAndCjsTests( join(__dirname, '../..'), 'scenario-stream-midway-error.mjs', @@ -1116,6 +1148,7 @@ describe('OpenAI integration', () => { }); }, ); + createEsmAndCjsTests(join(__dirname, '../..'), 'scenario-abort.mjs', 'instrument.mjs', (createRunner, test) => { test('ends a call the caller aborts as cancelled, but an SDK timeout as an error', async () => { await createRunner() diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/v5/other-apis/test.ts b/dev-packages/node-integration-tests/suites/tracing/openai/v5/other-apis/test.ts index 3c7e15bf9c59..ee84fc6532c2 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/v5/other-apis/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/openai/v5/other-apis/test.ts @@ -30,20 +30,51 @@ describe('OpenAI integration', () => { afterAll(() => { cleanupChildProcesses(); }); - createEsmAndCjsTests(join(__dirname, '../..'), 'scenario-embeddings.mjs', 'instrument.mjs', (createRunner, test) => { + const defaultScenarios = [ + 'scenario-chat.mjs', + 'scenario-embeddings.mjs', + 'scenario-azure-openai.mjs', + 'scenario-with-response.mjs', + 'scenario-root-span.mjs', + 'scenario-conversation.mjs', + 'scenario-manual-conversation-id.mjs', + 'scenario-separate-scope-1.mjs', + 'scenario-separate-scope-2.mjs', + ]; + const recordingScenarios = ['scenario-chat.mjs', 'scenario-embeddings.mjs', 'scenario-system-instructions.mjs']; + createEsmAndCjsTests(join(__dirname, '../..'), defaultScenarios, 'instrument.mjs', (createRunner, test) => { + let scenarios: ReturnType | undefined; + async function runScenarios() { + const runner = createRunner(); + + const segmentNames: Record = { + 'scenario-conversation.mjs': 'conversation-test', + 'scenario-manual-conversation-id.mjs': 'chat-with-manual-conversation-id', + 'scenario-separate-scope-1.mjs': 'GET /chat/conversation-1', + 'scenario-separate-scope-2.mjs': 'GET /chat/conversation-2', + }; + + const collectors = defaultScenarios.map(name => + runner.collectStreamedSpansUntilSegment( + span => + span.attributes['test.scenario']?.value === name && + (name === 'scenario-azure-openai.mjs' || name === 'scenario-root-span.mjs' + ? span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai' + : span.name === (segmentNames[name] ?? 'main')), + ), + ); + await runner.start().completed(); + return { spans: await Promise.all(collectors) }; + } + const getScenarios = () => (scenarios ??= runScenarios()); + test('creates openai related spans with genAI recording disabled', async () => { let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - await createRunner() - .unordered() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); + const container = { items: (await getScenarios()).spans[1]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); + receivedSpans = container.items; const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); expect(spans).toHaveLength(4); @@ -192,25 +223,246 @@ describe('OpenAI integration', () => { expect(rawEmbeddingSpan!.attributes[GEN_AI_USAGE_INPUT_TOKENS]).toBeUndefined(); expect(rawEmbeddingSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toBeUndefined(); }); + + test('it works without a wrapping span', async () => { + let receivedSpans: SerializedStreamedSpanContainer['items'] = []; + + const container = { items: (await getScenarios()).spans[4]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'chat gpt-3.5-turbo')).toBeDefined(); + receivedSpans = container.items; + + const segment = receivedSpans.find(span => span.is_segment && span.name === 'chat gpt-3.5-turbo'); + expect(segment?.span_id).toEqual(expect.any(String)); + expect(segment?.trace_id).toEqual(expect.any(String)); + expect(segment?.attributes[GEN_AI_OPERATION_NAME]).toEqual({ value: 'chat', type: 'string' }); + expect(segment?.attributes[SENTRY_OP]).toEqual({ value: 'gen_ai.chat', type: 'string' }); + expect(segment?.attributes[SENTRY_ORIGIN]).toEqual({ value: 'auto.ai.openai', type: 'string' }); + expect(segment?.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ value: 'openai', type: 'string' }); + expect(segment?.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ value: 'gpt-3.5-turbo', type: 'string' }); + expect(segment?.attributes[GEN_AI_REQUEST_TEMPERATURE]).toEqual({ value: 0.7, type: 'double' }); + expect(segment?.attributes[GEN_AI_RESPONSE_MODEL]).toEqual({ value: 'gpt-3.5-turbo', type: 'string' }); + expect(segment?.attributes[GEN_AI_RESPONSE_ID]).toEqual({ value: 'chatcmpl-mock123', type: 'string' }); + expect(segment?.attributes[GEN_AI_RESPONSE_FINISH_REASONS]).toEqual({ value: '["stop"]', type: 'string' }); + expect(segment?.attributes[GEN_AI_USAGE_INPUT_TOKENS]).toEqual({ value: 10, type: 'integer' }); + expect(segment?.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]).toEqual({ value: 15, type: 'integer' }); + expect(segment?.attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toEqual({ value: 25, type: 'integer' }); + expect(segment?.status).toEqual('ok'); + }); + + test('captures the conversation ID from the Conversations API and ignores previous_response_id', async () => { + let receivedSpans: SerializedStreamedSpanContainer['items'] = []; + + const container = { items: (await getScenarios()).spans[5]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'conversation-test')).toBeDefined(); + receivedSpans = container.items; + + const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); + expect(spans).toHaveLength(5); + const conversationCreateSpan = spans.find( + span => span.name === 'chat' && span.attributes[GEN_AI_RESPONSE_ID] !== undefined, + ); + expect(conversationCreateSpan!.status).toBe('ok'); + expect(conversationCreateSpan!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ + type: 'string', + value: 'chat', + }); + expect(conversationCreateSpan!.attributes[SENTRY_OP]).toEqual({ + type: 'string', + value: 'gen_ai.chat', + }); + expect(conversationCreateSpan!.attributes[SENTRY_ORIGIN]).toEqual({ + type: 'string', + value: 'auto.ai.openai', + }); + expect(conversationCreateSpan!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ + type: 'string', + value: 'openai', + }); + expect(conversationCreateSpan!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ + type: 'string', + value: 'conv_689667905b048191b4740501625afd940c7533ace33a2dab', + }); + + const conversationResponseSpan = spans.find( + span => + span.attributes[GEN_AI_CONVERSATION_ID]?.value === 'conv_689667905b048191b4740501625afd940c7533ace33a2dab' && + span.attributes[GEN_AI_REQUEST_MODEL]?.value === 'gpt-4', + ); + expect(conversationResponseSpan!.status).toBe('ok'); + expect(conversationResponseSpan!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ + type: 'string', + value: 'chat', + }); + expect(conversationResponseSpan!.attributes[SENTRY_OP]).toEqual({ + type: 'string', + value: 'gen_ai.chat', + }); + expect(conversationResponseSpan!.attributes[SENTRY_ORIGIN]).toEqual({ + type: 'string', + value: 'auto.ai.openai', + }); + expect(conversationResponseSpan!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ + type: 'string', + value: 'openai', + }); + expect(conversationResponseSpan!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ + type: 'string', + value: 'gpt-4', + }); + expect(conversationResponseSpan!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ + type: 'string', + value: 'conv_689667905b048191b4740501625afd940c7533ace33a2dab', + }); + + // The unlinked call and the one chained with `previous_response_id` both carry no conversation id: + // a response id names one response, not the thread, so chaining does not group spans. + const unlinkedResponseSpans = spans.filter( + span => + span.attributes[SENTRY_OP]?.value === 'gen_ai.chat' && + span.attributes[GEN_AI_REQUEST_MODEL]?.value === 'gpt-4' && + span.attributes[GEN_AI_CONVERSATION_ID] === undefined, + ); + expect(unlinkedResponseSpans).toHaveLength(2); + for (const span of unlinkedResponseSpans) { + expect(span.status).toBe('ok'); + expect(span.attributes[GEN_AI_RESPONSE_ID]).toEqual({ type: 'string', value: 'resp_mock_conv_123' }); + } + expect(spans.some(span => span.attributes[GEN_AI_CONVERSATION_ID]?.value === 'resp_mock_conv_123')).toBe(false); + + const rawConversationSpan = spans.find( + span => span.name === 'chat' && span.attributes[GEN_AI_CONVERSATION_ID] === undefined, + ); + expect(rawConversationSpan!.status).toBe('ok'); + expect(rawConversationSpan!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ + type: 'string', + value: 'chat', + }); + expect(rawConversationSpan!.attributes[SENTRY_OP]).toEqual({ + type: 'string', + value: 'gen_ai.chat', + }); + expect(rawConversationSpan!.attributes[SENTRY_ORIGIN]).toEqual({ + type: 'string', + value: 'auto.ai.openai', + }); + expect(rawConversationSpan!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ + type: 'string', + value: 'openai', + }); + expect(rawConversationSpan!.attributes[GEN_AI_CONVERSATION_ID]).toBeUndefined(); + }); + + test('attaches manual conversation ID set via setConversationId() to all chat spans', async () => { + let receivedSpans: SerializedStreamedSpanContainer['items'] = []; + + const container = { items: (await getScenarios()).spans[6]! }; + + expect( + container.items.find(span => span.is_segment && span.name === 'chat-with-manual-conversation-id'), + ).toBeDefined(); + receivedSpans = container.items; + + const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); + expect(spans).toHaveLength(3); + + // All three chat completion spans should have the same manually-set conversation ID + for (const span of spans) { + expect(span!.name).toBe('chat gpt-4'); + expect(span!.status).toBe('ok'); + expect(span!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ + type: 'string', + value: 'user_chat_session_abc123', + }); + expect(span!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ type: 'string', value: 'openai' }); + expect(span!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ type: 'string', value: 'gpt-4' }); + expect(span!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ type: 'string', value: 'chat' }); + expect(span!.attributes[SENTRY_OP]).toEqual({ type: 'string', value: 'gen_ai.chat' }); + } + }); + + test('isolates conversation IDs across separate scopes - conversation 1', async () => { + let receivedSpans: SerializedStreamedSpanContainer['items'] = []; + + const container = { items: (await getScenarios()).spans[7]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'GET /chat/conversation-1')).toBeDefined(); + receivedSpans = container.items; + + const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); + expect(spans).toHaveLength(2); + + // Both chat completion spans should have the expected conversation ID + for (const span of spans) { + expect(span!.name).toBe('chat gpt-4'); + expect(span!.status).toBe('ok'); + expect(span!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ + type: 'string', + value: 'conv_user1_session_abc', + }); + expect(span!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ type: 'string', value: 'openai' }); + expect(span!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ type: 'string', value: 'gpt-4' }); + expect(span!.attributes[SENTRY_OP]).toEqual({ type: 'string', value: 'gen_ai.chat' }); + } + }); + + test('isolates conversation IDs across separate scopes - conversation 2', async () => { + let receivedSpans: SerializedStreamedSpanContainer['items'] = []; + + const container = { items: (await getScenarios()).spans[8]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'GET /chat/conversation-2')).toBeDefined(); + receivedSpans = container.items; + + const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); + expect(spans).toHaveLength(2); + + // Both chat completion spans should have the expected conversation ID + for (const span of spans) { + expect(span!.name).toBe('chat gpt-4'); + expect(span!.status).toBe('ok'); + expect(span!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ + type: 'string', + value: 'conv_user2_session_xyz', + }); + expect(span!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ type: 'string', value: 'openai' }); + expect(span!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ type: 'string', value: 'gpt-4' }); + expect(span!.attributes[SENTRY_OP]).toEqual({ type: 'string', value: 'gen_ai.chat' }); + } + }); }); + createEsmAndCjsTests( join(__dirname, '../..'), - 'scenario-embeddings.mjs', + recordingScenarios, 'instrument-with-pii.mjs', (createRunner, test) => { + let scenarios: ReturnType | undefined; + async function runScenarios() { + const runner = createRunner(); + + const collectors = recordingScenarios.map(name => + runner.collectStreamedSpansUntilSegment( + span => + span.attributes['test.scenario']?.value === name && + (name === 'scenario-azure-openai.mjs' + ? span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai' + : span.name === 'main'), + ), + ); + await runner.start().completed(); + return { spans: await Promise.all(collectors) }; + } + const getScenarios = () => (scenarios ??= runScenarios()); + test('creates openai related spans with genAI recording enabled', async () => { let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - await createRunner() - .unordered() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); + const container = { items: (await getScenarios()).spans[1]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); + receivedSpans = container.items; const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); expect(spans).toHaveLength(4); @@ -368,294 +620,14 @@ describe('OpenAI integration', () => { value: 'Raw embedding test!', }); }); - }, - ); - createEsmAndCjsTests(join(__dirname, '../..'), 'scenario-root-span.mjs', 'instrument.mjs', (createRunner, test) => { - test('it works without a wrapping span', async () => { - let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - - await createRunner() - .unordered() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'chat gpt-3.5-turbo')).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); - - const segment = receivedSpans.find(span => span.is_segment && span.name === 'chat gpt-3.5-turbo'); - expect(segment?.span_id).toEqual(expect.any(String)); - expect(segment?.trace_id).toEqual(expect.any(String)); - expect(segment?.attributes[GEN_AI_OPERATION_NAME]).toEqual({ value: 'chat', type: 'string' }); - expect(segment?.attributes[SENTRY_OP]).toEqual({ value: 'gen_ai.chat', type: 'string' }); - expect(segment?.attributes[SENTRY_ORIGIN]).toEqual({ value: 'auto.ai.openai', type: 'string' }); - expect(segment?.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ value: 'openai', type: 'string' }); - expect(segment?.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ value: 'gpt-3.5-turbo', type: 'string' }); - expect(segment?.attributes[GEN_AI_REQUEST_TEMPERATURE]).toEqual({ value: 0.7, type: 'double' }); - expect(segment?.attributes[GEN_AI_RESPONSE_MODEL]).toEqual({ value: 'gpt-3.5-turbo', type: 'string' }); - expect(segment?.attributes[GEN_AI_RESPONSE_ID]).toEqual({ value: 'chatcmpl-mock123', type: 'string' }); - expect(segment?.attributes[GEN_AI_RESPONSE_FINISH_REASONS]).toEqual({ value: '["stop"]', type: 'string' }); - expect(segment?.attributes[GEN_AI_USAGE_INPUT_TOKENS]).toEqual({ value: 10, type: 'integer' }); - expect(segment?.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]).toEqual({ value: 15, type: 'integer' }); - expect(segment?.attributes[GEN_AI_USAGE_TOTAL_TOKENS]).toEqual({ value: 25, type: 'integer' }); - expect(segment?.status).toEqual('ok'); - }); - }); - createEsmAndCjsTests( - join(__dirname, '../..'), - 'scenario-conversation.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('captures the conversation ID from the Conversations API and ignores previous_response_id', async () => { - let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - - await createRunner() - .unordered() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'conversation-test')).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); - const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); - expect(spans).toHaveLength(5); - const conversationCreateSpan = spans.find( - span => span.name === 'chat' && span.attributes[GEN_AI_RESPONSE_ID] !== undefined, - ); - expect(conversationCreateSpan!.status).toBe('ok'); - expect(conversationCreateSpan!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ - type: 'string', - value: 'chat', - }); - expect(conversationCreateSpan!.attributes[SENTRY_OP]).toEqual({ - type: 'string', - value: 'gen_ai.chat', - }); - expect(conversationCreateSpan!.attributes[SENTRY_ORIGIN]).toEqual({ - type: 'string', - value: 'auto.ai.openai', - }); - expect(conversationCreateSpan!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ - type: 'string', - value: 'openai', - }); - expect(conversationCreateSpan!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ - type: 'string', - value: 'conv_689667905b048191b4740501625afd940c7533ace33a2dab', - }); - - const conversationResponseSpan = spans.find( - span => - span.attributes[GEN_AI_CONVERSATION_ID]?.value === - 'conv_689667905b048191b4740501625afd940c7533ace33a2dab' && - span.attributes[GEN_AI_REQUEST_MODEL]?.value === 'gpt-4', - ); - expect(conversationResponseSpan!.status).toBe('ok'); - expect(conversationResponseSpan!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ - type: 'string', - value: 'chat', - }); - expect(conversationResponseSpan!.attributes[SENTRY_OP]).toEqual({ - type: 'string', - value: 'gen_ai.chat', - }); - expect(conversationResponseSpan!.attributes[SENTRY_ORIGIN]).toEqual({ - type: 'string', - value: 'auto.ai.openai', - }); - expect(conversationResponseSpan!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ - type: 'string', - value: 'openai', - }); - expect(conversationResponseSpan!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ - type: 'string', - value: 'gpt-4', - }); - expect(conversationResponseSpan!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ - type: 'string', - value: 'conv_689667905b048191b4740501625afd940c7533ace33a2dab', - }); - - // The unlinked call and the one chained with `previous_response_id` both carry no conversation id: - // a response id names one response, not the thread, so chaining does not group spans. - const unlinkedResponseSpans = spans.filter( - span => - span.attributes[SENTRY_OP]?.value === 'gen_ai.chat' && - span.attributes[GEN_AI_REQUEST_MODEL]?.value === 'gpt-4' && - span.attributes[GEN_AI_CONVERSATION_ID] === undefined, - ); - expect(unlinkedResponseSpans).toHaveLength(2); - for (const span of unlinkedResponseSpans) { - expect(span.status).toBe('ok'); - expect(span.attributes[GEN_AI_RESPONSE_ID]).toEqual({ type: 'string', value: 'resp_mock_conv_123' }); - } - expect(spans.some(span => span.attributes[GEN_AI_CONVERSATION_ID]?.value === 'resp_mock_conv_123')).toBe(false); - - const rawConversationSpan = spans.find( - span => span.name === 'chat' && span.attributes[GEN_AI_CONVERSATION_ID] === undefined, - ); - expect(rawConversationSpan!.status).toBe('ok'); - expect(rawConversationSpan!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ - type: 'string', - value: 'chat', - }); - expect(rawConversationSpan!.attributes[SENTRY_OP]).toEqual({ - type: 'string', - value: 'gen_ai.chat', - }); - expect(rawConversationSpan!.attributes[SENTRY_ORIGIN]).toEqual({ - type: 'string', - value: 'auto.ai.openai', - }); - expect(rawConversationSpan!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ - type: 'string', - value: 'openai', - }); - expect(rawConversationSpan!.attributes[GEN_AI_CONVERSATION_ID]).toBeUndefined(); - }); - }, - ); - createEsmAndCjsTests( - join(__dirname, '../..'), - 'scenario-manual-conversation-id.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('attaches manual conversation ID set via setConversationId() to all chat spans', async () => { - let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - - await createRunner() - .unordered() - .expect({ - span: container => { - expect( - container.items.find(span => span.is_segment && span.name === 'chat-with-manual-conversation-id'), - ).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); - - const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); - expect(spans).toHaveLength(3); - - // All three chat completion spans should have the same manually-set conversation ID - for (const span of spans) { - expect(span!.name).toBe('chat gpt-4'); - expect(span!.status).toBe('ok'); - expect(span!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ - type: 'string', - value: 'user_chat_session_abc123', - }); - expect(span!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ type: 'string', value: 'openai' }); - expect(span!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ type: 'string', value: 'gpt-4' }); - expect(span!.attributes[GEN_AI_OPERATION_NAME]).toEqual({ type: 'string', value: 'chat' }); - expect(span!.attributes[SENTRY_OP]).toEqual({ type: 'string', value: 'gen_ai.chat' }); - } - }); - }, - ); - createEsmAndCjsTests( - join(__dirname, '../..'), - 'scenario-separate-scope-1.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('isolates conversation IDs across separate scopes - conversation 1', async () => { - let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - - await createRunner() - .unordered() - .expect({ - span: container => { - expect( - container.items.find(span => span.is_segment && span.name === 'GET /chat/conversation-1'), - ).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); - - const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); - expect(spans).toHaveLength(2); - - // Both chat completion spans should have the expected conversation ID - for (const span of spans) { - expect(span!.name).toBe('chat gpt-4'); - expect(span!.status).toBe('ok'); - expect(span!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ - type: 'string', - value: 'conv_user1_session_abc', - }); - expect(span!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ type: 'string', value: 'openai' }); - expect(span!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ type: 'string', value: 'gpt-4' }); - expect(span!.attributes[SENTRY_OP]).toEqual({ type: 'string', value: 'gen_ai.chat' }); - } - }); - }, - ); - createEsmAndCjsTests( - join(__dirname, '../..'), - 'scenario-separate-scope-2.mjs', - 'instrument.mjs', - (createRunner, test) => { - test('isolates conversation IDs across separate scopes - conversation 2', async () => { - let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - - await createRunner() - .unordered() - .expect({ - span: container => { - expect( - container.items.find(span => span.is_segment && span.name === 'GET /chat/conversation-2'), - ).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); - - const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); - expect(spans).toHaveLength(2); - - // Both chat completion spans should have the expected conversation ID - for (const span of spans) { - expect(span!.name).toBe('chat gpt-4'); - expect(span!.status).toBe('ok'); - expect(span!.attributes[GEN_AI_CONVERSATION_ID]).toEqual({ - type: 'string', - value: 'conv_user2_session_xyz', - }); - expect(span!.attributes[GEN_AI_PROVIDER_NAME]).toEqual({ type: 'string', value: 'openai' }); - expect(span!.attributes[GEN_AI_REQUEST_MODEL]).toEqual({ type: 'string', value: 'gpt-4' }); - expect(span!.attributes[SENTRY_OP]).toEqual({ type: 'string', value: 'gen_ai.chat' }); - } - }); - }, - ); - createEsmAndCjsTests( - join(__dirname, '../..'), - 'scenario-system-instructions.mjs', - 'instrument-with-pii.mjs', - (createRunner, test) => { test('extracts system instructions from messages', async () => { let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - await createRunner() - .unordered() - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); + const container = { items: (await getScenarios()).spans[2]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); + receivedSpans = container.items; const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); expect(spans).toHaveLength(1); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/v6/scenario-chat.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/v6/scenario-chat.mjs index 5779613b2b4b..11db7fc8b8a4 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/v6/scenario-chat.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/v6/scenario-chat.mjs @@ -91,7 +91,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { @@ -176,5 +176,3 @@ async function run() { server.close(); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/v6/scenario-embeddings.mjs b/dev-packages/node-integration-tests/suites/tracing/openai/v6/scenario-embeddings.mjs index a8d3c5cda6d0..714ac24635a8 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/v6/scenario-embeddings.mjs +++ b/dev-packages/node-integration-tests/suites/tracing/openai/v6/scenario-embeddings.mjs @@ -24,7 +24,7 @@ function startMockApi() { return startMockServer(app); } -async function run() { +export async function run() { const server = await startMockApi(); await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { @@ -60,5 +60,3 @@ async function run() { server.close(); } - -run(); diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/v7/test.ts b/dev-packages/node-integration-tests/suites/tracing/openai/v7/test.ts index 588896b31d49..890536e27e7b 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/v7/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/openai/v7/test.ts @@ -35,14 +35,27 @@ describe.runIf(supports({ min: 22 }))('OpenAI integration (V7)', () => { // all, so these assert that each instrumented `create` produces a span with the right shape. createEsmAndCjsTests( __dirname, - '../v6/scenario-chat.mjs', + ['../v6/scenario-chat.mjs', '../v6/scenario-embeddings.mjs'], 'instrument.mjs', (createRunner, test) => { - test('instruments chat completions, the responses API and streaming on openai v7', async () => { - const runner = createRunner().ignore('event'); - const spansPromise = runner.collectStreamedSpansUntilSegment('main'); + let scenarios: ReturnType | undefined; + async function runScenarios() { + const runner = createRunner(); + const collectors = ['../v6/scenario-chat.mjs', '../v6/scenario-embeddings.mjs'].map(name => + runner.collectStreamedSpansUntilSegment( + span => span.name === 'main' && span.attributes['test.scenario']?.value === name, + ), + ); await runner.start().completed(); + return { spans: await Promise.all(collectors) }; + } + const getScenarios = () => (scenarios ??= runScenarios()); + + test('instruments chat completions, the responses API and streaming on openai v7', async () => { + const spansPromise = getScenarios().then(result => result.spans[0]!); + + await getScenarios(); const allSpans = await spansPromise; const spans = allSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); @@ -103,35 +116,16 @@ describe.runIf(supports({ min: 22 }))('OpenAI integration (V7)', () => { value: 'auto.ai.openai', }); }); - }, - { - additionalDependencies: { - openai: '7.5.0', - }, - }, - ); - // Embeddings publish to a different channel than chat, and match a resource file at the package - // root rather than under a nested directory, so they need their own coverage. - createEsmAndCjsTests( - __dirname, - '../v6/scenario-embeddings.mjs', - 'instrument.mjs', - (createRunner, test) => { test('instruments the embeddings API on openai v7', async () => { let receivedSpans: SerializedStreamedSpanContainer['items'] = []; - await createRunner() - .unordered() - .ignore('event') - .expect({ - span: container => { - expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); - receivedSpans = container.items; - }, - }) - .start() - .completed(); + { + const container = { items: (await getScenarios()).spans[1]! }; + + expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined(); + receivedSpans = container.items; + } const spans = receivedSpans.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.openai'); const embeddingSpans = spans.filter(span => span.attributes[GEN_AI_OPERATION_NAME]?.value === 'embeddings'); @@ -165,6 +159,9 @@ describe.runIf(supports({ min: 22 }))('OpenAI integration (V7)', () => { }, ); + // Embeddings publish to a different channel than chat, and match a resource file at the package + // root rather than under a nested directory, so they need their own coverage. + // The decisions API (`openai` >= 7.30.0) returns typed answers, so it is recorded like a Jev call. createEsmAndCjsTests( __dirname,