Skip to content

Commit e093eb2

Browse files
authored
Merge 9714653 into a57560f
2 parents a57560f + 9714653 commit e093eb2

17 files changed

Lines changed: 809 additions & 30 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Work in this release was contributed by @lazerg, @chiliec, @61465, @jpdefo, and
3030

3131
Add `Sentry.cronJobIntegration()` to `integrations` to send check-ins for every named `CronJob` of the `cron` package (4.3+), with the job name as the monitor slug. It's off by default. Each monitored job creates a monitor in Sentry, and monitors are billed. Use `excludeJobs` to skip jobs by name. `Sentry.cron.instrumentCron` is deprecated; use `cronJobIntegration` instead.
3232

33+
- **fix(server-utils): Instrument @anthropic-ai/sdk 1.x and turn off its own OTel spans ([#25750](https://github.com/getsentry/sentry-javascript/pull/25750))**
34+
35+
The Anthropic integration now instruments `@anthropic-ai/sdk` 1.x. Since 0.134 the SDK also records its own OpenTelemetry spans, such as `anthropic.messages.create`, which duplicated Sentry's `gen_ai` spans. Once Sentry instruments the SDK, it sets `ANTHROPIC_OPEN_TELEMETRY=false` to turn those spans off. To keep them, set `ANTHROPIC_OPEN_TELEMETRY=true` or pass the `openTelemetry` option to the Anthropic client.
36+
3337
- **feat(nextjs): Mark pageloads of cached pages ([#25318](https://github.com/getsentry/sentry-javascript/pull/25318))**
3438

3539
When Next.js serves a page from its cache, for example an ISR page or a `cacheComponents` shell, the pageload now has the `page.server_rendering.type` attribute. It is `static` if the whole page came from the cache, and `partially_static` if parts of it rendered for this request. Then the pageload stays connected to this request, as before. If the request that rendered the cached page was sampled, the pageload also links to it. So in Sentry, a cached page no longer looks like missing server instrumentation.

‎dev-packages/bun-integration-tests/node-suites/excludes.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export const NO_AUTO_INSTRUMENTATION = [
7575
'suites/pino/test.ts',
7676
'suites/tracing/amqplib/test.ts',
7777
'suites/tracing/anthropic/test.ts',
78+
'suites/tracing/anthropic/v0/test.ts',
7879
'suites/tracing/anthropic/v0.129/test.ts',
80+
'suites/tracing/anthropic/v1/test.ts',
7981
'suites/tracing/apollo-graphql/**',
8082
'suites/tracing/dataloader/test.ts',
8183
'suites/tracing/fastify/test.ts',

‎dev-packages/node-integration-tests/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"test:watch": "pnpm run test --watch"
2626
},
2727
"devDependencies": {
28-
"@anthropic-ai/sdk": "0.133.0",
28+
"@anthropic-ai/sdk": "1.3.0",
2929
"@apollo/server": "^5.5.0",
3030
"@aws-sdk/client-bedrock-runtime": "^3.1161.0",
3131
"@aws-sdk/client-dynamodb": "^3.1161.0",

‎dev-packages/node-integration-tests/suites/tracing/anthropic/scenario-with-response.mjs‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,6 @@ function startMockAnthropicServer() {
66
const app = express();
77
app.use(express.json());
88

9-
app.post('/anthropic/v1/complete', (req, res) => {
10-
res.send({
11-
id: 'compl_withresponse',
12-
type: 'completion',
13-
model: req.body.model,
14-
completion: 'Testing .asResponse() method!',
15-
stop_reason: 'stop_sequence',
16-
});
17-
});
18-
199
app.post('/anthropic/v1/messages', (req, res) => {
2010
const model = req.body.model;
2111

@@ -210,15 +200,6 @@ async function run() {
210200
.asResponse();
211201
await betaResponse.json();
212202

213-
const completionResponse = await client.completions
214-
.create({
215-
model: 'claude-2',
216-
max_tokens_to_sample: 100,
217-
prompt: '\n\nHuman: Test asResponse\n\nAssistant:',
218-
})
219-
.asResponse();
220-
await completionResponse.json();
221-
222203
const rawStreamResponse = await client.messages
223204
.create({
224205
model: 'claude-3-haiku-20240307',

‎dev-packages/node-integration-tests/suites/tracing/anthropic/test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('Anthropic integration', () => {
4040
span: container => {
4141
expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined();
4242
const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.anthropic');
43-
expect(spans).toHaveLength(6);
43+
expect(spans).toHaveLength(5);
4444
const nonStreamingSpans = spans.filter(
4545
span => span.attributes[GEN_AI_RESPONSE_ID]?.value === 'msg_withresponse',
4646
);
@@ -60,7 +60,7 @@ describe('Anthropic integration', () => {
6060
expect(streamingSpan!.attributes[GEN_AI_RESPONSE_STREAMING].value).toBe(true);
6161

6262
const rawResponseSpans = spans.filter(span => span.attributes[GEN_AI_RESPONSE_ID] === undefined);
63-
expect(rawResponseSpans).toHaveLength(4);
63+
expect(rawResponseSpans).toHaveLength(3);
6464
for (const span of rawResponseSpans) {
6565
expect(span.name).toBe(`chat ${span.attributes[GEN_AI_REQUEST_MODEL].value}`);
6666
expect(span.status).toBe('ok');
@@ -70,7 +70,6 @@ describe('Anthropic integration', () => {
7070
expect(rawResponseSpans.map(span => span.attributes[GEN_AI_REQUEST_MODEL].value)).toEqual([
7171
'claude-3-haiku-20240307',
7272
'claude-3-haiku-20240307',
73-
'claude-2',
7473
'claude-3-haiku-20240307',
7574
]);
7675
},
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
dataCollection: { genAI: { inputs: false, outputs: false } },
9+
transport: loggingTransport,
10+
});
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import Anthropic from '@anthropic-ai/sdk';
2+
import * as Sentry from '@sentry/node';
3+
import express from 'express';
4+
5+
function streamEvents(id, model) {
6+
return [
7+
{
8+
type: 'message_start',
9+
message: { id, type: 'message', role: 'assistant', model, content: [], usage: { input_tokens: 10 } },
10+
},
11+
{ type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } },
12+
{ type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'Hello!' } },
13+
{ type: 'content_block_stop', index: 0 },
14+
{ type: 'message_delta', delta: { stop_reason: 'end_turn', stop_sequence: null }, usage: { output_tokens: 15 } },
15+
{ type: 'message_stop' },
16+
];
17+
}
18+
19+
function startMockAnthropicServer() {
20+
const app = express();
21+
app.use(express.json());
22+
23+
app.post('/anthropic/v1/messages', (req, res) => {
24+
// The prompt names the response id, so each call's span can be told apart.
25+
const id = req.body.messages[0].content;
26+
27+
if (!req.body.stream) {
28+
res.json({
29+
id,
30+
type: 'message',
31+
role: 'assistant',
32+
model: req.body.model,
33+
content: [{ type: 'text', text: 'Hello!' }],
34+
stop_reason: 'end_turn',
35+
stop_sequence: null,
36+
usage: { input_tokens: 10, output_tokens: 15 },
37+
});
38+
return;
39+
}
40+
41+
res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive' });
42+
const events = streamEvents(id, req.body.model);
43+
events.forEach((event, index) => {
44+
setTimeout(() => {
45+
res.write(`event: ${event.type}\n`);
46+
res.write(`data: ${JSON.stringify(event)}\n\n`);
47+
if (index === events.length - 1) {
48+
res.end();
49+
}
50+
}, index * 10);
51+
});
52+
});
53+
54+
app.post('/anthropic/v1/complete', (req, res) => {
55+
res.json({
56+
id: req.body.prompt.includes('asResponse') ? 'compl_v0_as_response' : 'compl_v0',
57+
type: 'completion',
58+
model: req.body.model,
59+
completion: 'Hello!',
60+
stop_reason: 'stop_sequence',
61+
});
62+
});
63+
64+
return new Promise(resolve => {
65+
const server = app.listen(0, () => {
66+
resolve(server);
67+
});
68+
});
69+
}
70+
71+
async function run() {
72+
const server = await startMockAnthropicServer();
73+
74+
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
75+
const client = new Anthropic({
76+
apiKey: 'mock-api-key',
77+
baseURL: `http://localhost:${server.address().port}/anthropic`,
78+
});
79+
const model = 'claude-3-haiku-20240307';
80+
81+
await client.messages.create({ model, max_tokens: 100, messages: [{ role: 'user', content: 'msg_v0_create' }] });
82+
await client.beta.messages.create({
83+
model,
84+
max_tokens: 100,
85+
messages: [{ role: 'user', content: 'msg_v0_beta_create' }],
86+
});
87+
88+
const stream = await client.messages.create({
89+
model,
90+
max_tokens: 100,
91+
stream: true,
92+
messages: [{ role: 'user', content: 'msg_v0_stream' }],
93+
});
94+
for await (const _ of stream) {
95+
// consume
96+
}
97+
98+
await client.messages
99+
.stream({ model, max_tokens: 100, messages: [{ role: 'user', content: 'msg_v0_stream_helper' }] })
100+
.finalMessage();
101+
102+
await client.completions.create({
103+
model: 'claude-2',
104+
max_tokens_to_sample: 100,
105+
prompt: '\n\nHuman: Hello\n\nAssistant:',
106+
});
107+
const response = await client.completions
108+
.create({ model: 'claude-2', max_tokens_to_sample: 100, prompt: '\n\nHuman: asResponse\n\nAssistant:' })
109+
.asResponse();
110+
await response.json();
111+
});
112+
113+
await Sentry.flush(2000);
114+
115+
server.close();
116+
}
117+
118+
run();
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import type { SerializedStreamedSpanContainer } from '@sentry/core';
2+
import {
3+
GEN_AI_REQUEST_MODEL,
4+
GEN_AI_RESPONSE_ID,
5+
GEN_AI_RESPONSE_STREAMING,
6+
SENTRY_OP,
7+
SENTRY_ORIGIN,
8+
} from '@sentry/conventions/attributes';
9+
import { afterAll, describe, expect } from 'vitest';
10+
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../../utils/runner';
11+
12+
type SerializedSpan = SerializedStreamedSpanContainer['items'][number];
13+
14+
const MESSAGE_CALLS = [
15+
{ id: 'msg_v0_create', streaming: false },
16+
{ id: 'msg_v0_beta_create', streaming: false },
17+
{ id: 'msg_v0_stream', streaming: true },
18+
{ id: 'msg_v0_stream_helper', streaming: true },
19+
];
20+
21+
// The main suite runs on 1.x. This keeps the last 0.x version covered, including `completions`,
22+
// which 1.0 removed.
23+
describe('Anthropic integration (0.x)', () => {
24+
afterAll(() => {
25+
cleanupChildProcesses();
26+
});
27+
28+
createEsmAndCjsTests(
29+
__dirname,
30+
'scenario.mjs',
31+
'instrument.mjs',
32+
(createRunner, test) => {
33+
test('emits one span per call', async () => {
34+
await createRunner()
35+
.unordered()
36+
.expect({
37+
span: container => {
38+
expect(container.items.find(span => span.is_segment && span.name === 'main')).toBeDefined();
39+
const spans = container.items.filter(
40+
span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.anthropic',
41+
);
42+
expect(spans).toHaveLength(6);
43+
for (const span of spans) {
44+
expect(span.attributes[SENTRY_OP]?.value, span.name).toBe('gen_ai.chat');
45+
expect(span.status, span.name).toBe('ok');
46+
}
47+
48+
const spanForId = (id: string): SerializedSpan[] =>
49+
spans.filter(span => span.attributes[GEN_AI_RESPONSE_ID]?.value === id);
50+
51+
for (const { id, streaming } of MESSAGE_CALLS) {
52+
expect(spanForId(id), id).toHaveLength(1);
53+
expect(spanForId(id)[0]!.attributes[GEN_AI_RESPONSE_STREAMING]?.value ?? false, id).toBe(streaming);
54+
}
55+
56+
// `.asResponse()` hands back the raw response, so its span has no response id.
57+
const completionSpans = spans.filter(span => span.attributes[GEN_AI_REQUEST_MODEL]?.value === 'claude-2');
58+
expect(completionSpans.map(span => span.attributes[GEN_AI_RESPONSE_ID]?.value)).toEqual([
59+
'compl_v0',
60+
undefined,
61+
]);
62+
},
63+
})
64+
.start()
65+
.completed();
66+
});
67+
},
68+
{ additionalDependencies: { '@anthropic-ai/sdk': '0.133.0' } },
69+
);
70+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
dataCollection: { genAI: { inputs: false, outputs: false } },
9+
transport: loggingTransport,
10+
// Registers Sentry as the global tracer provider, which is what 1.x traces itself through.
11+
enableOpenTelemetrySetup: process.env.OTEL_SETUP === 'true',
12+
integrations:
13+
process.env.WITHOUT_ANTHROPIC === 'true'
14+
? integrations => integrations.filter(integration => integration.name !== 'Anthropic_AI')
15+
: undefined,
16+
});

0 commit comments

Comments
 (0)