-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): Add scope-level conversation ID API to support linking AI conversations #18909
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RulaKhaled
merged 9 commits into
develop
from
rolaabuhasna/js-1515-implement-a-new-sentrysetconversationid-api
Jan 23, 2026
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5d59cce
wip(ai): Support scope-level conversation ID for AI tracing
RulaKhaled b6fa981
refactor
RulaKhaled 6f202e0
add exports where missing
RulaKhaled a137f69
improve logic, refactor to an integration
RulaKhaled 0ea7fdd
refactor, add changelog unreleased entry
RulaKhaled cdb00a1
Merge branch 'develop' into rolaabuhasna/js-1515-implement-a-new-sent…
RulaKhaled 84351a6
bump the size
RulaKhaled 9d2e776
Merge branch 'develop' into rolaabuhasna/js-1515-implement-a-new-sent…
RulaKhaled 4a00b3a
Merge branch 'develop' into rolaabuhasna/js-1515-implement-a-new-sent…
RulaKhaled File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...packages/node-integration-tests/suites/tracing/openai/scenario-manual-conversation-id.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
| import OpenAI from 'openai'; | ||
|
|
||
| function startMockServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| // Chat completions endpoint | ||
| app.post('/openai/chat/completions', (req, res) => { | ||
| const { model } = req.body; | ||
|
|
||
| res.send({ | ||
| id: 'chatcmpl-mock123', | ||
| object: 'chat.completion', | ||
| created: 1677652288, | ||
| model: model, | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { | ||
| role: 'assistant', | ||
| content: 'Mock response from OpenAI', | ||
| }, | ||
| finish_reason: 'stop', | ||
| }, | ||
| ], | ||
| usage: { | ||
| prompt_tokens: 10, | ||
| completion_tokens: 15, | ||
| total_tokens: 25, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockServer(); | ||
|
|
||
| // Test: Multiple chat completions in the same conversation with manual conversation ID | ||
| await Sentry.startSpan({ op: 'function', name: 'chat-with-manual-conversation-id' }, async () => { | ||
| const client = new OpenAI({ | ||
| baseURL: `http://localhost:${server.address().port}/openai`, | ||
| apiKey: 'mock-api-key', | ||
| }); | ||
|
|
||
| // Set conversation ID manually using Sentry API | ||
| Sentry.setConversationId('user_chat_session_abc123'); | ||
|
|
||
| // First message in the conversation | ||
| await client.chat.completions.create({ | ||
| model: 'gpt-4', | ||
| messages: [{ role: 'user', content: 'What is the capital of France?' }], | ||
| }); | ||
|
|
||
| // Second message in the same conversation | ||
| await client.chat.completions.create({ | ||
| model: 'gpt-4', | ||
| messages: [{ role: 'user', content: 'Tell me more about it' }], | ||
| }); | ||
|
|
||
| // Third message in the same conversation | ||
| await client.chat.completions.create({ | ||
| model: 'gpt-4', | ||
| messages: [{ role: 'user', content: 'What is its population?' }], | ||
| }); | ||
| }); | ||
|
|
||
| server.close(); | ||
| await Sentry.flush(2000); | ||
| } | ||
|
|
||
| run(); |
74 changes: 74 additions & 0 deletions
74
dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-1.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
| import OpenAI from 'openai'; | ||
|
|
||
| function startMockServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| // Chat completions endpoint | ||
| app.post('/openai/chat/completions', (req, res) => { | ||
| const { model } = req.body; | ||
|
|
||
| res.send({ | ||
| id: 'chatcmpl-mock123', | ||
| object: 'chat.completion', | ||
| created: 1677652288, | ||
| model: model, | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { | ||
| role: 'assistant', | ||
| content: 'Mock response from OpenAI', | ||
| }, | ||
| finish_reason: 'stop', | ||
| }, | ||
| ], | ||
| usage: { | ||
| prompt_tokens: 10, | ||
| completion_tokens: 15, | ||
| total_tokens: 25, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockServer(); | ||
| const client = new OpenAI({ | ||
| baseURL: `http://localhost:${server.address().port}/openai`, | ||
| apiKey: 'mock-api-key', | ||
| }); | ||
|
|
||
| // First request/conversation scope | ||
| await Sentry.withScope(async scope => { | ||
| // Set conversation ID for this request scope BEFORE starting the span | ||
| scope.setConversationId('conv_user1_session_abc'); | ||
|
|
||
| await Sentry.startSpan({ op: 'http.server', name: 'GET /chat/conversation-1' }, async () => { | ||
| // First message in conversation 1 | ||
| await client.chat.completions.create({ | ||
| model: 'gpt-4', | ||
| messages: [{ role: 'user', content: 'Hello from conversation 1' }], | ||
| }); | ||
|
|
||
| // Second message in conversation 1 | ||
| await client.chat.completions.create({ | ||
| model: 'gpt-4', | ||
| messages: [{ role: 'user', content: 'Follow-up in conversation 1' }], | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| server.close(); | ||
| await Sentry.flush(2000); | ||
| } | ||
|
|
||
| run(); |
74 changes: 74 additions & 0 deletions
74
dev-packages/node-integration-tests/suites/tracing/openai/scenario-separate-scope-2.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
| import OpenAI from 'openai'; | ||
|
|
||
| function startMockServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| // Chat completions endpoint | ||
| app.post('/openai/chat/completions', (req, res) => { | ||
| const { model } = req.body; | ||
|
|
||
| res.send({ | ||
| id: 'chatcmpl-mock123', | ||
| object: 'chat.completion', | ||
| created: 1677652288, | ||
| model: model, | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { | ||
| role: 'assistant', | ||
| content: 'Mock response from OpenAI', | ||
| }, | ||
| finish_reason: 'stop', | ||
| }, | ||
| ], | ||
| usage: { | ||
| prompt_tokens: 10, | ||
| completion_tokens: 15, | ||
| total_tokens: 25, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockServer(); | ||
| const client = new OpenAI({ | ||
| baseURL: `http://localhost:${server.address().port}/openai`, | ||
| apiKey: 'mock-api-key', | ||
| }); | ||
|
|
||
| // Second request/conversation scope (completely separate) | ||
| await Sentry.withScope(async scope => { | ||
| // Set different conversation ID for this request scope BEFORE starting the span | ||
| scope.setConversationId('conv_user2_session_xyz'); | ||
|
|
||
| await Sentry.startSpan({ op: 'http.server', name: 'GET /chat/conversation-2' }, async () => { | ||
| // First message in conversation 2 | ||
| await client.chat.completions.create({ | ||
| model: 'gpt-4', | ||
| messages: [{ role: 'user', content: 'Hello from conversation 2' }], | ||
| }); | ||
|
|
||
| // Second message in conversation 2 | ||
| await client.chat.completions.create({ | ||
| model: 'gpt-4', | ||
| messages: [{ role: 'user', content: 'Follow-up in conversation 2' }], | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| server.close(); | ||
| await Sentry.flush(2000); | ||
| } | ||
|
|
||
| run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ export { | |
| Scope, | ||
| SDK_VERSION, | ||
| setContext, | ||
| setConversationId, | ||
| setExtra, | ||
| setExtras, | ||
| setTag, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ export { | |
| Scope, | ||
| SDK_VERSION, | ||
| setContext, | ||
| setConversationId, | ||
| setExtra, | ||
| setExtras, | ||
| setTag, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.