-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(cloudflare,vercel-edge): Add support for Anthropic AI instrumentation #17571
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 5 commits into
develop
from
support-anthropic-ai-in-vercel-edge-and-cloudflare
Sep 11, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bf97cc4
feat(cloudflare,vercel-edge): Add support for Anthropic AI instrument…
RulaKhaled 4136062
more tests
RulaKhaled 4ff0b68
Merge branch 'develop' into support-anthropic-ai-in-vercel-edge-and-c…
RulaKhaled 1707b69
revert back to AnthropicAiResponse
RulaKhaled 9d072de
one more change
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
32 changes: 32 additions & 0 deletions
32
dev-packages/cloudflare-integration-tests/suites/tracing/anthropic-ai/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,32 @@ | ||
import * as Sentry from '@sentry/cloudflare'; | ||
import type { AnthropicAiClient } from '@sentry/core'; | ||
import { MockAnthropic } from './mocks'; | ||
|
||
interface Env { | ||
SENTRY_DSN: string; | ||
} | ||
|
||
const mockClient = new MockAnthropic({ | ||
apiKey: 'mock-api-key', | ||
}); | ||
|
||
const client: AnthropicAiClient = Sentry.instrumentAnthropicAiClient(mockClient); | ||
|
||
export default Sentry.withSentry( | ||
(env: Env) => ({ | ||
dsn: env.SENTRY_DSN, | ||
tracesSampleRate: 1.0, | ||
}), | ||
{ | ||
async fetch(_request, _env, _ctx) { | ||
const response = await client.messages?.create({ | ||
model: 'claude-3-haiku-20240307', | ||
messages: [{ role: 'user', content: 'What is the capital of France?' }], | ||
temperature: 0.7, | ||
max_tokens: 100, | ||
}); | ||
|
||
return new Response(JSON.stringify(response)); | ||
}, | ||
}, | ||
); |
68 changes: 68 additions & 0 deletions
68
dev-packages/cloudflare-integration-tests/suites/tracing/anthropic-ai/mocks.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,68 @@ | ||
import type { AnthropicAiClient, AnthropicAiResponse } from '@sentry/core'; | ||
|
||
export class MockAnthropic implements AnthropicAiClient { | ||
public messages: { | ||
create: (...args: unknown[]) => Promise<AnthropicAiResponse>; | ||
countTokens: (...args: unknown[]) => Promise<AnthropicAiResponse>; | ||
}; | ||
public models: { | ||
list: (...args: unknown[]) => Promise<AnthropicAiResponse>; | ||
get: (...args: unknown[]) => Promise<AnthropicAiResponse>; | ||
}; | ||
public completions: { | ||
create: (...args: unknown[]) => Promise<AnthropicAiResponse>; | ||
}; | ||
public apiKey: string; | ||
|
||
public constructor(config: { apiKey: string }) { | ||
this.apiKey = config.apiKey; | ||
|
||
// Main focus: messages.create functionality | ||
this.messages = { | ||
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: 'msg_mock123', | ||
type: 'message', | ||
role: 'assistant', | ||
model: params.model, | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: 'Hello from Anthropic mock!', | ||
}, | ||
], | ||
stop_reason: 'end_turn', | ||
stop_sequence: null, | ||
usage: { | ||
input_tokens: 10, | ||
output_tokens: 15, | ||
cache_creation_input_tokens: 0, | ||
cache_read_input_tokens: 0, | ||
}, | ||
}; | ||
}, | ||
countTokens: async (..._args: unknown[]) => ({ id: 'mock', type: 'model', model: 'mock', input_tokens: 0 }), | ||
}; | ||
|
||
// Minimal implementations for required interface compliance | ||
this.models = { | ||
list: async (..._args: unknown[]) => ({ id: 'mock', type: 'model', model: 'mock' }), | ||
get: async (..._args: unknown[]) => ({ id: 'mock', type: 'model', model: 'mock' }), | ||
}; | ||
|
||
this.completions = { | ||
create: async (..._args: unknown[]) => ({ id: 'mock', type: 'completion', model: 'mock' }), | ||
}; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
dev-packages/cloudflare-integration-tests/suites/tracing/anthropic-ai/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,41 @@ | ||
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 message creation request', async () => { | ||
const runner = createRunner(__dirname) | ||
.ignore('event') | ||
.expect(envelope => { | ||
const transactionEvent = envelope[1]?.[0]?.[1] as any; | ||
|
||
expect(transactionEvent.transaction).toBe('GET /'); | ||
expect(transactionEvent.spans).toEqual( | ||
expect.arrayContaining([ | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
'gen_ai.operation.name': 'messages', | ||
'sentry.op': 'gen_ai.messages', | ||
'sentry.origin': 'auto.ai.anthropic', | ||
'gen_ai.system': 'anthropic', | ||
'gen_ai.request.model': 'claude-3-haiku-20240307', | ||
'gen_ai.request.temperature': 0.7, | ||
'gen_ai.response.model': 'claude-3-haiku-20240307', | ||
'gen_ai.response.id': 'msg_mock123', | ||
'gen_ai.usage.input_tokens': 10, | ||
'gen_ai.usage.output_tokens': 15, | ||
}), | ||
description: 'messages claude-3-haiku-20240307', | ||
op: 'gen_ai.messages', | ||
origin: 'auto.ai.anthropic', | ||
}), | ||
]), | ||
); | ||
}) | ||
.start(); | ||
await runner.makeRequest('get', '/'); | ||
await runner.completed(); | ||
}); |
6 changes: 6 additions & 0 deletions
6
dev-packages/cloudflare-integration-tests/suites/tracing/anthropic-ai/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": "worker-name", | ||
"compatibility_date": "2025-06-17", | ||
"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
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.