Skip to content

feat(cloudflare,vercel-edge): Add support for OpenAI instrumentation #17338

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
merged 7 commits into from
Aug 6, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ Sentry.init({

Spans matching the filter criteria will not be recorded. Potential child spans of filtered spans will be re-parented, if possible.

- **feat(cloudflare,vercel-edge): Add support for OpenAI instrumentation ([#17338](https://github.com/getsentry/sentry-javascript/pull/17338))**

Adds support for OpenAI manual instrumentation in `@sentry/cloudflare` and `@sentry/vercel-edge`.

To instrument the OpenAI client, wrap it with `Sentry.instrumentOpenAiClient` and set recording settings.

```js
import * as Sentry from '@sentry/cloudflare';
import OpenAI from 'openai';

const openai = new OpenAI();
const client = Sentry.instrumentOpenAiClient(openai, { recordInputs: true, recordOutputs: true });

// use the wrapped client
```

## 10.1.0

- feat(nuxt): Align build-time options to follow bundler plugins structure ([#17255](https://github.com/getsentry/sentry-javascript/pull/17255))
Expand Down
1 change: 1 addition & 0 deletions dev-packages/cloudflare-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@sentry/cloudflare": "10.1.0"
},
"devDependencies": {
"@sentry/core": "10.0.0",
"@cloudflare/workers-types": "^4.20250708.0",
"@sentry-internal/test-utils": "link:../test-utils",
"vitest": "^3.2.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as Sentry from '@sentry/cloudflare';
import { MockOpenAi } from './mocks';

interface Env {
SENTRY_DSN: string;
}

const mockClient = new MockOpenAi({
apiKey: 'mock-api-key',
});

const client = Sentry.instrumentOpenAiClient(mockClient);

export default Sentry.withSentry(
(env: Env) => ({
dsn: env.SENTRY_DSN,
tracesSampleRate: 1.0,
}),
{
async fetch(_request, _env, _ctx) {
const response = await client.chat?.completions?.create({
model: 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is the capital of France?' },
],
temperature: 0.7,
max_tokens: 100,
});

return new Response(JSON.stringify(response));
},
},
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
import type { OpenAiClient } from '@sentry/core';

export class MockOpenAi implements OpenAiClient {
public chat: Record<string, unknown>;
public responses: {
create: (...args: unknown[]) => Promise<unknown>;
};

public apiKey: string;

public constructor(config: { apiKey: string }) {
this.apiKey = config.apiKey;

this.chat = {
completions: {
create: async (...args: unknown[]) => {
const params = args[0] as { model: string; stream?: boolean };
// Simulate processing time
await new Promise(resolve => setTimeout(resolve, 10));

if (params.model === 'error-model') {
const error = new Error('Model not found');
(error as unknown as { status: number }).status = 404;
(error as unknown as { headers: Record<string, string> }).headers = { 'x-request-id': 'mock-request-123' };
throw error;
}

// If stream is requested, return an async generator
if (params.stream) {
return this.createChatCompletionStream(params);
}

return {
id: 'chatcmpl-mock123',
object: 'chat.completion',
created: 1677652288,
model: params.model,
system_fingerprint: 'fp_44709d6fcb',
choices: [
{
index: 0,
message: {
role: 'assistant',
content: 'Hello from OpenAI mock!',
},
finish_reason: 'stop',
},
],
usage: {
prompt_tokens: 10,
completion_tokens: 15,
total_tokens: 25,
},
};
},
},
};

this.responses = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you're only testing client.chat?.completions?.create you can get rid of the rest of this file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, removed!

create: async (...args: unknown[]) => {
const params = args[0] as { model: string; input: string; instructions: string; stream?: boolean };
await new Promise(resolve => setTimeout(resolve, 10));

// If stream is requested, return an async generator
if (params.stream) {
return this.createResponsesApiStream(params);
}

return {
id: 'resp_mock456',
object: 'response',
created_at: 1677652290,
model: params.model,
input_text: params.input,
output_text: `Response to: ${params.input}`,
status: 'completed',
usage: {
input_tokens: 5,
output_tokens: 8,
total_tokens: 13,
},
};
},
};
}

// Create a mock streaming response for chat completions
public async *createChatCompletionStream(params: { model: string }): AsyncGenerator<unknown> {
// First chunk with basic info
yield {
id: 'chatcmpl-stream-123',
object: 'chat.completion.chunk',
created: 1677652300,
model: params.model,
system_fingerprint: 'fp_stream_123',
choices: [
{
index: 0,
delta: {
role: 'assistant',
content: 'Hello',
},
finish_reason: null,
},
],
};

// Second chunk with more content
yield {
id: 'chatcmpl-stream-123',
object: 'chat.completion.chunk',
created: 1677652300,
model: params.model,
system_fingerprint: 'fp_stream_123',
choices: [
{
index: 0,
delta: {
content: ' from OpenAI streaming!',
},
finish_reason: 'stop',
},
],
usage: {
prompt_tokens: 12,
completion_tokens: 18,
total_tokens: 30,
completion_tokens_details: {
accepted_prediction_tokens: 0,
audio_tokens: 0,
reasoning_tokens: 0,
rejected_prediction_tokens: 0,
},
prompt_tokens_details: {
audio_tokens: 0,
cached_tokens: 0,
},
},
};
}

// Create a mock streaming response for responses API
public async *createResponsesApiStream(params: {
model: string;
input: string;
instructions: string;
}): AsyncGenerator<unknown> {
// Response created event
yield {
type: 'response.created',
response: {
id: 'resp_stream_456',
object: 'response',
created_at: 1677652310,
model: params.model,
status: 'in_progress',
error: null,
incomplete_details: null,
instructions: params.instructions,
max_output_tokens: 1000,
parallel_tool_calls: false,
previous_response_id: null,
reasoning: {
effort: null,
summary: null,
},
store: false,
temperature: 0.7,
text: {
format: {
type: 'text',
},
},
tool_choice: 'auto',
top_p: 1.0,
truncation: 'disabled',
user: null,
metadata: {},
output: [],
output_text: '',
usage: {
input_tokens: 0,
output_tokens: 0,
total_tokens: 0,
},
},
sequence_number: 1,
};

// Response in progress with output text delta
yield {
type: 'response.output_text.delta',
delta: 'Streaming response to: ',
sequence_number: 2,
};

yield {
type: 'response.output_text.delta',
delta: params.input,
sequence_number: 3,
};

// Response completed event
yield {
type: 'response.completed',
response: {
id: 'resp_stream_456',
object: 'response',
created_at: 1677652310,
model: params.model,
status: 'completed',
error: null,
incomplete_details: null,
instructions: params.instructions,
max_output_tokens: 1000,
parallel_tool_calls: false,
previous_response_id: null,
reasoning: {
effort: null,
summary: null,
},
store: false,
temperature: 0.7,
text: {
format: {
type: 'text',
},
},
tool_choice: 'auto',
top_p: 1.0,
truncation: 'disabled',
user: null,
metadata: {},
output: [],
output_text: params.input,
usage: {
input_tokens: 6,
output_tokens: 10,
total_tokens: 16,
},
},
sequence_number: 4,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect, it } from 'vitest';
import { createRunner } from '../../../runner';

// These tests are not exhaustive because the instrumentation is
// already tested in the node integration tests and we merely
// want to test that the instrumentation does not break in our
// cloudflare SDK.

it('traces a basic chat completion request', async () => {
const runner = createRunner(__dirname)
.ignore('event')
.expect(envelope => {
const transactionEvent = envelope[1]?.[0]?.[1];

expect(transactionEvent.transaction).toBe('GET /');
expect(transactionEvent.spans).toEqual(
expect.arrayContaining([
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'chat',
'sentry.op': 'gen_ai.chat',
'gen_ai.system': 'openai',
'gen_ai.request.model': 'gpt-3.5-turbo',
'gen_ai.request.temperature': 0.7,
'gen_ai.response.model': 'gpt-3.5-turbo',
'gen_ai.response.id': 'chatcmpl-mock123',
'gen_ai.usage.input_tokens': 10,
'gen_ai.usage.output_tokens': 15,
'gen_ai.usage.total_tokens': 25,
'gen_ai.response.finish_reasons': '["stop"]',
}),
description: 'chat gpt-3.5-turbo',
op: 'gen_ai.chat',
origin: 'manual',
}),
]),
);
})
.start();
await runner.makeRequest('get', '/');
await runner.completed();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "worker-name",
"compatibility_date": "2025-06-17",
"main": "index.ts",
"compatibility_flags": ["nodejs_compat"]
}
6 changes: 6 additions & 0 deletions dev-packages/cloudflare-integration-tests/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export default defineConfig({
// already run in their own processes. We use threads instead because the
// overhead is significantly less.
pool: 'threads',
// Run tests sequentially to avoid port conflicts with wrangler dev processes
poolOptions: {
threads: {
singleThread: true,
},
},
reporters: process.env.DEBUG
? ['default', { summary: false }]
: process.env.GITHUB_ACTIONS
Expand Down
1 change: 1 addition & 0 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export {
functionToStringIntegration,
// eslint-disable-next-line deprecation/deprecation
inboundFiltersIntegration,
instrumentOpenAiClient,
eventFiltersIntegration,
linkedErrorsIntegration,
requestDataIntegration,
Expand Down
Loading
Loading