-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathindex.ts
190 lines (173 loc) · 6.68 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* eslint-disable complexity */
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, defineIntegration, spanToJSON } from '@sentry/core';
import type { IntegrationFn } from '@sentry/core';
import { generateInstrumentOnce } from '../../../otel/instrument';
import { addOriginToSpan } from '../../../utils/addOriginToSpan';
import { SentryVercelAiInstrumentation, sentryVercelAiPatched } from './instrumentation';
export const instrumentVercelAi = generateInstrumentOnce('vercelAI', () => new SentryVercelAiInstrumentation({}));
const _vercelAIIntegration = (() => {
return {
name: 'VercelAI',
setupOnce() {
instrumentVercelAi();
},
processEvent(event) {
if (event.type === 'transaction' && event.spans?.length) {
for (const span of event.spans) {
const { data: attributes, description: name } = span;
if (!name || span.origin !== 'auto.vercelai.otel') {
continue;
}
if (attributes['ai.usage.completionTokens'] != undefined) {
attributes['ai.completion_tokens.used'] = attributes['ai.usage.completionTokens'];
}
if (attributes['ai.usage.promptTokens'] != undefined) {
attributes['ai.prompt_tokens.used'] = attributes['ai.usage.promptTokens'];
}
if (
typeof attributes['ai.usage.completionTokens'] == 'number' &&
typeof attributes['ai.usage.promptTokens'] == 'number'
) {
attributes['ai.total_tokens.used'] =
attributes['ai.usage.completionTokens'] + attributes['ai.usage.promptTokens'];
}
}
}
return event;
},
setup(client) {
client.on('spanStart', span => {
if (!sentryVercelAiPatched) {
return;
}
const { data: attributes, description: name } = spanToJSON(span);
if (!name) {
return;
}
// The id of the model
const aiModelId = attributes['ai.model.id'];
// the provider of the model
const aiModelProvider = attributes['ai.model.provider'];
// both of these must be defined for the integration to work
if (typeof aiModelId !== 'string' || typeof aiModelProvider !== 'string' || !aiModelId || !aiModelProvider) {
return;
}
let isPipelineSpan = false;
switch (name) {
case 'ai.generateText': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.pipeline.generateText');
isPipelineSpan = true;
break;
}
case 'ai.generateText.doGenerate': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.run.doGenerate');
break;
}
case 'ai.streamText': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.pipeline.streamText');
isPipelineSpan = true;
break;
}
case 'ai.streamText.doStream': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.run.doStream');
break;
}
case 'ai.generateObject': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.pipeline.generateObject');
isPipelineSpan = true;
break;
}
case 'ai.generateObject.doGenerate': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.run.doGenerate');
break;
}
case 'ai.streamObject': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.pipeline.streamObject');
isPipelineSpan = true;
break;
}
case 'ai.streamObject.doStream': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.run.doStream');
break;
}
case 'ai.embed': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.pipeline.embed');
isPipelineSpan = true;
break;
}
case 'ai.embed.doEmbed': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.embeddings');
break;
}
case 'ai.embedMany': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.pipeline.embedMany');
isPipelineSpan = true;
break;
}
case 'ai.embedMany.doEmbed': {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.embeddings');
break;
}
case 'ai.toolCall':
case 'ai.stream.firstChunk':
case 'ai.stream.finish':
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.run');
break;
}
addOriginToSpan(span, 'auto.vercelai.otel');
const nameWthoutAi = name.replace('ai.', '');
span.setAttribute('ai.pipeline.name', nameWthoutAi);
span.updateName(nameWthoutAi);
// If a Telemetry name is set and it is a pipeline span, use that as the operation name
const functionId = attributes['ai.telemetry.functionId'];
if (functionId && typeof functionId === 'string' && isPipelineSpan) {
span.updateName(functionId);
span.setAttribute('ai.pipeline.name', functionId);
}
if (attributes['ai.prompt']) {
span.setAttribute('ai.input_messages', attributes['ai.prompt']);
}
if (attributes['ai.model.id']) {
span.setAttribute('ai.model_id', attributes['ai.model.id']);
}
span.setAttribute('ai.streaming', name.includes('stream'));
});
},
};
}) satisfies IntegrationFn;
/**
* Adds Sentry tracing instrumentation for the [ai](https://www.npmjs.com/package/ai) library.
*
* For more information, see the [`ai` documentation](https://sdk.vercel.ai/docs/ai-sdk-core/telemetry).
*
* @example
* ```javascript
* const Sentry = require('@sentry/node');
*
* Sentry.init({
* integrations: [Sentry.vercelAIIntegration()],
* });
* ```
*
* By default this integration adds tracing support to all `ai` function calls. If you need to disable
* collecting spans for a specific call, you can do so by setting `experimental_telemetry.isEnabled` to
* `false` in the first argument of the function call.
*
* ```javascript
* const result = await generateText({
* model: openai('gpt-4-turbo'),
* experimental_telemetry: { isEnabled: false },
* });
* ```
*
* If you want to collect inputs and outputs for a specific call, you must specifically opt-in to each
* function call by setting `experimental_telemetry.recordInputs` and `experimental_telemetry.recordOutputs`
* to `true`.
*
* ```javascript
* const result = await generateText({
* model: openai('gpt-4-turbo'),
* experimental_telemetry: { isEnabled: true, recordInputs: true, recordOutputs: true },
* });
*/
export const vercelAIIntegration = defineIntegration(_vercelAIIntegration);