Repository navigation
feat(cloudflare): Export the channel integrations #25584
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
Open
isaacs
wants to merge
1
commit into
isaacschlueter/js-3972-exports-parity-across-cloudflare-deno-bun-node
from
isaacschlueter/js-3973-cloudflare-export-channel-integrations
Open
Changes from all commits
Commits
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
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
46 changes: 46 additions & 0 deletions
46
...ages/cloudflare-integration-tests/suites/vite/diagnostics-channel/openai-options/index.ts
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,46 @@ | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
| import OpenAI from 'openai'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| } | ||
|
|
||
| // Return a canned response so the `openai` SDK runs on workerd without hitting the network. | ||
| const mockFetch: typeof fetch = async () => | ||
| new Response( | ||
| JSON.stringify({ | ||
| id: 'chatcmpl-mock123', | ||
| object: 'chat.completion', | ||
| created: 1677652288, | ||
| model: 'gpt-3.5-turbo', | ||
| choices: [ | ||
| { | ||
| index: 0, | ||
| message: { role: 'assistant', content: 'Hello from OpenAI!' }, | ||
| finish_reason: 'stop', | ||
| }, | ||
| ], | ||
| usage: { prompt_tokens: 10, completion_tokens: 15, total_tokens: 25 }, | ||
| }), | ||
| { status: 200, headers: { 'content-type': 'application/json' } }, | ||
| ); | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1, | ||
| // `dataCollection` records inputs by default. The integration options must win over it. | ||
| integrations: [Sentry.openAIIntegration({ recordInputs: false })], | ||
| }), | ||
| { | ||
| async fetch(_request, _env, _ctx) { | ||
| // Not wrapped with `instrumentOpenAiClient`: the vite plugin instruments the `openai` package. | ||
| const client = new OpenAI({ apiKey: 'mock-api-key', fetch: mockFetch }); | ||
| await client.chat.completions.create({ | ||
| model: 'gpt-3.5-turbo', | ||
| messages: [{ role: 'user', content: 'What is the capital of France?' }], | ||
| }); | ||
| return new Response('ok'); | ||
| }, | ||
| }, | ||
| ); |
25 changes: 25 additions & 0 deletions
25
...kages/cloudflare-integration-tests/suites/vite/diagnostics-channel/openai-options/test.ts
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,25 @@ | ||
| import { GEN_AI_INPUT_MESSAGES, GEN_AI_RESPONSE_TEXT, SENTRY_OP, SENTRY_ORIGIN } from '@sentry/conventions/attributes'; | ||
| import { GEN_AI_CHAT } from '@sentry/conventions/op'; | ||
| import { expect, it } from 'vitest'; | ||
| import { createRunner } from '../../../../runner'; | ||
|
|
||
| it('applies the options of an openAIIntegration the user configured', async ({ signal }) => { | ||
| const runner = createRunner(__dirname).ignore('event').start(signal); | ||
|
|
||
| // The segment span and the gen_ai child. The segment can arrive before the child's envelope. | ||
| const spansPromise = runner.collectStreamedSpans(spansOfTrace => spansOfTrace.length === 2); | ||
|
|
||
| await runner.makeRequest('get', '/'); | ||
|
|
||
| const genAiSpans = (await spansPromise).filter(span => !span.is_segment); | ||
| expect(genAiSpans).toHaveLength(1); | ||
| const attributes = genAiSpans[0]?.attributes; | ||
|
|
||
| // The vite plugin instrumented `openai`, so the channel integration created the span. | ||
| expect(attributes?.[SENTRY_OP]).toEqual({ type: 'string', value: GEN_AI_CHAT }); | ||
| expect(attributes?.[SENTRY_ORIGIN]).toEqual({ type: 'string', value: 'auto.ai.openai' }); | ||
| // `recordInputs: false` from the configured integration wins over `dataCollection`, which records | ||
| // inputs by default. Outputs keep the default. | ||
| expect(attributes?.[GEN_AI_INPUT_MESSAGES]).toBeUndefined(); | ||
| expect(attributes?.[GEN_AI_RESPONSE_TEXT]).toEqual({ type: 'string', value: '["Hello from OpenAI!"]' }); | ||
| }); | ||
7 changes: 7 additions & 0 deletions
7
...oudflare-integration-tests/suites/vite/diagnostics-channel/openai-options/vite.config.mts
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,7 @@ | ||
| import { cloudflare } from '@cloudflare/vite-plugin'; | ||
| import { sentryCloudflareVitePlugin } from '@sentry/cloudflare/vite'; | ||
| import { defineConfig } from 'vite'; | ||
|
|
||
| export default defineConfig({ | ||
| plugins: [cloudflare(), sentryCloudflareVitePlugin({ autoInstrumentation: false })], | ||
| }); |
6 changes: 6 additions & 0 deletions
6
...loudflare-integration-tests/suites/vite/diagnostics-channel/openai-options/wrangler.jsonc
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,6 @@ | ||
| { | ||
| "name": "openai-options-worker", | ||
| "compatibility_date": "2026-04-26", | ||
| "main": "index.ts", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| } |
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
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
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.