Skip to content

Commit f88aef1

Browse files
committed
update with tests
1 parent 2861132 commit f88aef1

File tree

5 files changed

+84
-16
lines changed

5 files changed

+84
-16
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class MockAnthropic {
1010
create: this._messagesCreate.bind(this),
1111
countTokens: this._messagesCountTokens.bind(this)
1212
};
13+
14+
this.models = {
15+
retrieve: this._modelsRetrieve.bind(this),
16+
};
1317
}
1418

1519
/**
@@ -46,22 +50,28 @@ class MockAnthropic {
4650
};
4751
}
4852

49-
async _messagesCountTokens(params) {
53+
async _messagesCountTokens() {
5054
// Simulate processing time
5155
await new Promise(resolve => setTimeout(resolve, 10));
5256

53-
if (params.model === 'error-model') {
54-
const error = new Error('Model not found');
55-
error.status = 404;
56-
error.headers = { 'x-request-id': 'mock-request-123' };
57-
throw error;
58-
}
59-
60-
// For countTokens, just return input_tokens to match implementation
57+
// For countTokens, just return input_tokens
6158
return {
6259
input_tokens: 15
6360
}
6461
}
62+
63+
async _modelsRetrieve(modelId) {
64+
// Simulate processing time
65+
await new Promise(resolve => setTimeout(resolve, 10));
66+
67+
// Match what the actual implementation would return
68+
return {
69+
id: modelId,
70+
name: modelId,
71+
created_at: 1715145600,
72+
model: modelId, // Add model field to match the check in addResponseAttributes
73+
};
74+
}
6575
}
6676

6777
async function run() {
@@ -100,6 +110,9 @@ async function run() {
100110
{ role: 'user', content: 'What is the capital of France?' },
101111
],
102112
});
113+
114+
// Fourth test: models.retrieve
115+
await client.models.retrieve('claude-3-haiku-20240307');
103116
});
104117
}
105118

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,22 @@ describe('Anthropic integration', () => {
5858
origin: 'auto.ai.anthropic',
5959
status: 'ok',
6060
}),
61+
// Fourth span - models.retrieve
62+
expect.objectContaining({
63+
data: {
64+
'gen_ai.operation.name': 'retrieve',
65+
'sentry.op': 'gen_ai.retrieve',
66+
'sentry.origin': 'auto.ai.anthropic',
67+
'gen_ai.system': 'anthropic',
68+
'gen_ai.request.model': 'claude-3-haiku-20240307',
69+
'gen_ai.response.id': 'claude-3-haiku-20240307',
70+
'gen_ai.response.model': 'claude-3-haiku-20240307',
71+
},
72+
description: 'retrieve claude-3-haiku-20240307',
73+
op: 'gen_ai.retrieve',
74+
origin: 'auto.ai.anthropic',
75+
status: 'ok',
76+
}),
6177
]),
6278
};
6379

@@ -119,6 +135,22 @@ describe('Anthropic integration', () => {
119135
origin: 'auto.ai.anthropic',
120136
status: 'ok',
121137
}),
138+
// Fourth span - models.retrieve with PII
139+
expect.objectContaining({
140+
data: {
141+
'gen_ai.operation.name': 'retrieve',
142+
'sentry.op': 'gen_ai.retrieve',
143+
'sentry.origin': 'auto.ai.anthropic',
144+
'gen_ai.system': 'anthropic',
145+
'gen_ai.request.model': 'claude-3-haiku-20240307',
146+
'gen_ai.response.id': 'claude-3-haiku-20240307',
147+
'gen_ai.response.model': 'claude-3-haiku-20240307',
148+
},
149+
description: 'retrieve claude-3-haiku-20240307',
150+
op: 'gen_ai.retrieve',
151+
origin: 'auto.ai.anthropic',
152+
status: 'ok',
153+
}),
122154
]),
123155
};
124156

@@ -141,6 +173,18 @@ describe('Anthropic integration', () => {
141173
}),
142174
op: 'gen_ai.messages.countTokens',
143175
}),
176+
// Check models.retrieve with options
177+
expect.objectContaining({
178+
data: expect.objectContaining({
179+
'gen_ai.operation.name': 'retrieve',
180+
'gen_ai.system': 'anthropic',
181+
'gen_ai.request.model': 'claude-3-haiku-20240307',
182+
'gen_ai.response.id': 'claude-3-haiku-20240307',
183+
'gen_ai.response.model': 'claude-3-haiku-20240307',
184+
}),
185+
op: 'gen_ai.retrieve',
186+
description: 'retrieve claude-3-haiku-20240307',
187+
}),
144188
]),
145189
};
146190

packages/core/src/utils/anthropic-ai/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const ANTHROPIC_AI_INTEGRATION_NAME = 'Anthropic_AI';
55
export const ANTHROPIC_AI_INSTRUMENTED_METHODS = [
66
'messages.create',
77
'messages.countTokens',
8-
'models.list',
98
'models.get',
109
'completions.create',
10+
'models.retrieve',
1111
] as const;

packages/core/src/utils/anthropic-ai/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ function extractRequestAttributes(args: unknown[], methodPath: string): Record<s
5151
attributes[GEN_AI_REQUEST_FREQUENCY_PENALTY_ATTRIBUTE] = params.frequency_penalty;
5252
attributes[GEN_AI_REQUEST_MAX_TOKENS_ATTRIBUTE] = params.max_tokens;
5353
} else {
54-
attributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE] = 'unknown';
54+
if (methodPath === 'models.retrieve' || methodPath === 'models.get') {
55+
// models.retrieve(model-id) and models.get(model-id)
56+
attributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE] = args[0];
57+
} else {
58+
attributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE] = 'unknown';
59+
}
5560
}
5661

5762
return attributes;
@@ -109,6 +114,11 @@ function addResponseAttributes(span: Span, response: AnthropicAiResponse, record
109114
span.setAttributes({
110115
[ANTHROPIC_AI_RESPONSE_TIMESTAMP_ATTRIBUTE]: new Date(response.created * 1000).toISOString(),
111116
});
117+
if ('created_at' in response && typeof response.created_at === 'number') {
118+
span.setAttributes({
119+
[ANTHROPIC_AI_RESPONSE_TIMESTAMP_ATTRIBUTE]: new Date(response.created_at * 1000).toISOString(),
120+
});
121+
}
112122
}
113123

114124
if (response.usage) {
@@ -174,10 +184,10 @@ function instrumentMethod<T extends unknown[], R>(
174184
captureException(error, {
175185
mechanism: {
176186
handled: false,
177-
},
178-
type: 'auto.ai.anthropic',
179-
data: {
180-
function: methodPath,
187+
type: 'auto.ai.anthropic',
188+
data: {
189+
function: methodPath,
190+
},
181191
},
182192
});
183193
throw error;

packages/core/src/utils/anthropic-ai/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ export type AnthropicAiResponse = {
2020
[key: string]: unknown; // Allow for additional unknown properties
2121
id: string;
2222
model: string;
23-
created: number;
23+
created?: number;
24+
created_at?: number; // Available for Models.retrieve
2425
messages?: Array<Message>;
2526
content?: string; // Available for Messages.create
2627
completion?: string; // Available for Completions.create

0 commit comments

Comments
 (0)