Skip to content

Commit e30ea5c

Browse files
committed
Merge branch 'develop' into cg-JS-1207
2 parents 8f07b99 + f78aa3f commit e30ea5c

File tree

9 files changed

+679
-21
lines changed

9 files changed

+679
-21
lines changed

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ module.exports = [
240240
import: createImport('init'),
241241
ignore: [...builtinModules, ...nodePrefixedBuiltinModules],
242242
gzip: true,
243-
limit: '160 KB',
243+
limit: '161 KB',
244244
},
245245
{
246246
name: '@sentry/node - without tracing',

dev-packages/e2e-tests/test-applications/nextjs-16-tunnel/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@sentry/core": "latest || *",
2828
"ai": "^3.0.0",
2929
"import-in-the-middle": "^1",
30-
"next": "16.0.9",
30+
"next": "16.0.10",
3131
"react": "19.1.0",
3232
"react-dom": "19.1.0",
3333
"require-in-the-middle": "^7",

dev-packages/e2e-tests/test-applications/nextjs-pages-dir/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"@types/node": "^18.19.1",
2121
"@types/react": "18.0.26",
2222
"@types/react-dom": "18.0.9",
23-
"next": "14.2.32",
23+
"next": "14.2.35",
2424
"react": "18.2.0",
2525
"react-dom": "18.2.0",
2626
"typescript": "~5.0.0"
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { instrumentAnthropicAiClient } from '@sentry/core';
2+
import * as Sentry from '@sentry/node';
3+
4+
class MockAnthropic {
5+
constructor(config) {
6+
this.apiKey = config.apiKey;
7+
this.baseURL = config.baseURL;
8+
9+
// Create messages object with create method
10+
this.messages = {
11+
create: this._messagesCreate.bind(this),
12+
};
13+
}
14+
15+
/**
16+
* Create a mock message
17+
*/
18+
async _messagesCreate(params) {
19+
// Simulate processing time
20+
await new Promise(resolve => setTimeout(resolve, 10));
21+
22+
return {
23+
id: 'msg-truncation-test',
24+
type: 'message',
25+
role: 'assistant',
26+
content: [
27+
{
28+
type: 'text',
29+
text: 'This is the number **3**.',
30+
},
31+
],
32+
model: params.model,
33+
stop_reason: 'end_turn',
34+
stop_sequence: null,
35+
usage: {
36+
input_tokens: 10,
37+
output_tokens: 15,
38+
},
39+
};
40+
}
41+
}
42+
43+
async function run() {
44+
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
45+
const mockClient = new MockAnthropic({
46+
apiKey: 'mock-api-key',
47+
});
48+
49+
const client = instrumentAnthropicAiClient(mockClient);
50+
51+
// Send the image showing the number 3
52+
await client.messages.create({
53+
model: 'claude-3-haiku-20240307',
54+
max_tokens: 1024,
55+
messages: [
56+
{
57+
role: 'user',
58+
content: [
59+
{
60+
type: 'image',
61+
source: {
62+
type: 'base64',
63+
media_type: 'image/png',
64+
data: 'base64-mumbo-jumbo'.repeat(100),
65+
},
66+
},
67+
],
68+
},
69+
{
70+
role: 'user',
71+
content: 'what number is this?',
72+
},
73+
],
74+
temperature: 0.7,
75+
});
76+
});
77+
}
78+
79+
run();

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,4 +661,52 @@ describe('Anthropic integration', () => {
661661
});
662662
},
663663
);
664+
665+
createEsmAndCjsTests(__dirname, 'scenario-media-truncation.mjs', 'instrument-with-pii.mjs', (createRunner, test) => {
666+
test('truncates media attachment, keeping all other details', async () => {
667+
await createRunner()
668+
.ignore('event')
669+
.expect({
670+
transaction: {
671+
transaction: 'main',
672+
spans: expect.arrayContaining([
673+
expect.objectContaining({
674+
data: expect.objectContaining({
675+
'gen_ai.operation.name': 'messages',
676+
'sentry.op': 'gen_ai.messages',
677+
'sentry.origin': 'auto.ai.anthropic',
678+
'gen_ai.system': 'anthropic',
679+
'gen_ai.request.model': 'claude-3-haiku-20240307',
680+
'gen_ai.request.messages': JSON.stringify([
681+
{
682+
role: 'user',
683+
content: [
684+
{
685+
type: 'image',
686+
source: {
687+
type: 'base64',
688+
media_type: 'image/png',
689+
data: '[Filtered]',
690+
},
691+
},
692+
],
693+
},
694+
{
695+
role: 'user',
696+
content: 'what number is this?',
697+
},
698+
]),
699+
}),
700+
description: 'messages claude-3-haiku-20240307',
701+
op: 'gen_ai.messages',
702+
origin: 'auto.ai.anthropic',
703+
status: 'ok',
704+
}),
705+
]),
706+
},
707+
})
708+
.start()
709+
.completed();
710+
});
711+
});
664712
});

packages/browser/src/tracing/linkedTraces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ export function addPreviousTraceSpanLink(
176176
if (Date.now() / 1000 - previousTraceInfo.startTimestamp <= PREVIOUS_TRACE_MAX_DURATION) {
177177
if (DEBUG_BUILD) {
178178
debug.log(
179-
`Adding previous_trace ${previousTraceSpanCtx} link to span ${{
179+
`Adding previous_trace \`${JSON.stringify(previousTraceSpanCtx)}\` link to span \`${JSON.stringify({
180180
op: spanJson.op,
181181
...span.spanContext(),
182-
}}`,
182+
})}\``,
183183
);
184184
}
185185

packages/browser/test/tracing/linkedTraces.test.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Span } from '@sentry/core';
2-
import { addChildSpanToSpan, SentrySpan, spanToJSON, timestampInSeconds } from '@sentry/core';
2+
import { addChildSpanToSpan, debug, SentrySpan, spanToJSON, timestampInSeconds } from '@sentry/core';
33
import { beforeEach, describe, expect, it, vi } from 'vitest';
44
import { BrowserClient } from '../../src';
55
import type { PreviousTraceInfo } from '../../src/tracing/linkedTraces';
@@ -201,6 +201,47 @@ describe('addPreviousTraceSpanLink', () => {
201201
});
202202
});
203203

204+
it('logs a debug message when adding a previous trace link (with stringified context)', () => {
205+
const debugLogSpy = vi.spyOn(debug, 'log');
206+
207+
const currentSpanStart = timestampInSeconds();
208+
209+
const previousTraceInfo: PreviousTraceInfo = {
210+
spanContext: { traceId: '123', spanId: '456', traceFlags: 1 },
211+
startTimestamp: currentSpanStart - PREVIOUS_TRACE_MAX_DURATION + 1,
212+
sampleRand: 0.0126,
213+
sampleRate: 0.5,
214+
};
215+
216+
const currentSpan = new SentrySpan({
217+
name: 'test',
218+
op: 'navigation',
219+
startTimestamp: currentSpanStart,
220+
parentSpanId: '789',
221+
spanId: 'abc',
222+
traceId: 'def',
223+
sampled: true,
224+
});
225+
226+
const oldPropagationContext = {
227+
sampleRand: 0.0126,
228+
traceId: '123',
229+
sampled: true,
230+
dsc: { sample_rand: '0.0126', sample_rate: '0.5' },
231+
};
232+
233+
addPreviousTraceSpanLink(previousTraceInfo, currentSpan, oldPropagationContext);
234+
235+
expect(debugLogSpy).not.toHaveBeenCalledWith(expect.stringContaining('[object Object]'));
236+
expect(debugLogSpy).toHaveBeenCalledWith(
237+
expect.stringContaining(
238+
'Adding previous_trace `{"traceId":"123","spanId":"456","traceFlags":1}` link to span `{"op":"navigation","spanId":"abc","traceId":"def","traceFlags":1}`',
239+
),
240+
);
241+
242+
debugLogSpy.mockRestore();
243+
});
244+
204245
it(`doesn't add a previous_trace span link if the previous trace was created more than ${PREVIOUS_TRACE_MAX_DURATION}s ago`, () => {
205246
const currentSpanStart = timestampInSeconds();
206247

0 commit comments

Comments
 (0)