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 5 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,50 @@
import type { OpenAiClient } from '@sentry/core';

export class MockOpenAi implements OpenAiClient {
public chat?: Record<string, 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;
}

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,
},
};
},
},
};
}
}
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
7 changes: 3 additions & 4 deletions packages/core/src/utils/openai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import type {
ChatCompletionChunk,
InstrumentedMethod,
OpenAiChatCompletionObject,
OpenAiClient,
OpenAiIntegration,
OpenAiOptions,
OpenAiResponse,
Expand Down Expand Up @@ -294,7 +293,7 @@ function instrumentMethod<T extends unknown[], R>(
/**
* Create a deep proxy for OpenAI client instrumentation
*/
function createDeepProxy(target: object, currentPath = '', options?: OpenAiOptions): OpenAiClient {
function createDeepProxy<T extends object>(target: T, currentPath = '', options?: OpenAiOptions): T {
return new Proxy(target, {
get(obj: object, prop: string): unknown {
const value = (obj as Record<string, unknown>)[prop];
Expand All @@ -316,13 +315,13 @@ function createDeepProxy(target: object, currentPath = '', options?: OpenAiOptio

return value;
},
});
}) as T;
}

/**
* Instrument an OpenAI client with Sentry tracing
* Can be used across Node.js, Cloudflare Workers, and Vercel Edge
*/
export function instrumentOpenAiClient(client: OpenAiClient, options?: OpenAiOptions): OpenAiClient {
export function instrumentOpenAiClient<T extends object>(client: T, options?: OpenAiOptions): T {
Copy link
Member

Choose a reason for hiding this comment

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

🎉

return createDeepProxy(client, '', options);
}
4 changes: 2 additions & 2 deletions packages/core/src/utils/openai/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export type AttributeValue =

export interface OpenAiOptions {
/**
* Enable or disable input recording. Enabled if `sendDefaultPii` is `true`
* Enable or disable input recording.
*/
recordInputs?: boolean;
/**
* Enable or disable output recording. Enabled if `sendDefaultPii` is `true`
* Enable or disable output recording.
*/
recordOutputs?: boolean;
}
Expand Down
1 change: 1 addition & 0 deletions packages/vercel-edge/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