@ai-sdk/otel 1.0.0-beta.5 → 1.0.0-beta.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +328 -0
- package/dist/index.d.ts +50 -5
- package/dist/index.js +1131 -100
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/assemble-operation-name.ts +2 -2
- package/src/gen-ai-format-messages.ts +573 -0
- package/src/gen-ai-open-telemetry-integration.ts +924 -0
- package/src/get-base-telemetry-attributes.ts +11 -14
- package/src/index.ts +1 -0
- package/src/open-telemetry-integration.ts +29 -33
- package/src/select-telemetry-attributes.ts +3 -3
- package/dist/index.d.mts +0 -52
- package/dist/index.mjs +0 -845
- package/dist/index.mjs.map +0 -1
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { Attributes, AttributeValue } from '@opentelemetry/api';
|
|
2
|
-
import type {
|
|
2
|
+
import type { LanguageModelCallOptions } from 'ai';
|
|
3
3
|
|
|
4
4
|
export function getBaseTelemetryAttributes({
|
|
5
5
|
model,
|
|
6
6
|
settings,
|
|
7
|
-
telemetry,
|
|
8
7
|
headers,
|
|
8
|
+
context,
|
|
9
9
|
}: {
|
|
10
10
|
model: { modelId: string; provider: string };
|
|
11
|
-
settings:
|
|
12
|
-
telemetry: TelemetrySettings | undefined;
|
|
11
|
+
settings: LanguageModelCallOptions;
|
|
13
12
|
headers: Record<string, string | undefined> | undefined;
|
|
13
|
+
context: Record<string, unknown> | undefined;
|
|
14
14
|
}): Attributes {
|
|
15
15
|
return {
|
|
16
16
|
'ai.model.provider': model.provider,
|
|
@@ -22,16 +22,13 @@ export function getBaseTelemetryAttributes({
|
|
|
22
22
|
return attributes;
|
|
23
23
|
}, {} as Attributes),
|
|
24
24
|
|
|
25
|
-
// add
|
|
26
|
-
...Object.entries(
|
|
27
|
-
(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
},
|
|
33
|
-
{} as Attributes,
|
|
34
|
-
),
|
|
25
|
+
// add context as attributes:
|
|
26
|
+
...Object.entries(context ?? {}).reduce((attributes, [key, value]) => {
|
|
27
|
+
if (value != undefined) {
|
|
28
|
+
attributes[`ai.settings.context.${key}`] = value as AttributeValue;
|
|
29
|
+
}
|
|
30
|
+
return attributes;
|
|
31
|
+
}, {} as Attributes),
|
|
35
32
|
|
|
36
33
|
// request headers
|
|
37
34
|
...Object.entries(headers ?? {}).reduce((attributes, [key, value]) => {
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { LanguageModelV4Prompt } from '@ai-sdk/provider';
|
|
2
|
+
import type { Context as AISDKContext } from '@ai-sdk/provider-utils';
|
|
2
3
|
import {
|
|
3
4
|
Attributes,
|
|
4
5
|
AttributeValue,
|
|
5
6
|
context,
|
|
6
|
-
Context,
|
|
7
|
+
Context as OpenTelemetryContext,
|
|
7
8
|
Span,
|
|
8
9
|
SpanStatusCode,
|
|
9
10
|
trace,
|
|
@@ -14,7 +15,6 @@ import type {
|
|
|
14
15
|
EmbedOnFinishEvent,
|
|
15
16
|
EmbedOnStartEvent,
|
|
16
17
|
EmbedStartEvent,
|
|
17
|
-
GenerationContext,
|
|
18
18
|
ObjectOnFinishEvent,
|
|
19
19
|
ObjectOnStartEvent,
|
|
20
20
|
ObjectOnStepFinishEvent,
|
|
@@ -32,7 +32,7 @@ import type {
|
|
|
32
32
|
RerankOnStartEvent,
|
|
33
33
|
RerankStartEvent,
|
|
34
34
|
TelemetryIntegration,
|
|
35
|
-
|
|
35
|
+
TelemetryOptions,
|
|
36
36
|
ToolSet,
|
|
37
37
|
} from 'ai';
|
|
38
38
|
import { assembleOperationName } from './assemble-operation-name';
|
|
@@ -56,13 +56,13 @@ function recordSpanError(span: Span, error: unknown): void {
|
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
function shouldRecord(
|
|
59
|
-
telemetry:
|
|
60
|
-
): telemetry is
|
|
61
|
-
return telemetry?.isEnabled
|
|
59
|
+
telemetry: TelemetryOptions | undefined,
|
|
60
|
+
): telemetry is TelemetryOptions {
|
|
61
|
+
return telemetry?.isEnabled !== false;
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
function selectAttributes(
|
|
65
|
-
telemetry:
|
|
65
|
+
telemetry: TelemetryOptions | undefined,
|
|
66
66
|
attributes: Record<
|
|
67
67
|
string,
|
|
68
68
|
| AttributeValue
|
|
@@ -110,9 +110,9 @@ function selectAttributes(
|
|
|
110
110
|
|
|
111
111
|
interface OtelStepStartEvent<
|
|
112
112
|
TOOLS extends ToolSet = ToolSet,
|
|
113
|
-
|
|
113
|
+
RUNTIME_CONTEXT extends AISDKContext = AISDKContext,
|
|
114
114
|
OUTPUT extends Output = Output,
|
|
115
|
-
> extends OnStepStartEvent<TOOLS,
|
|
115
|
+
> extends OnStepStartEvent<TOOLS, RUNTIME_CONTEXT, OUTPUT> {
|
|
116
116
|
readonly promptMessages?: LanguageModelV4Prompt;
|
|
117
117
|
readonly stepTools?: ReadonlyArray<Record<string, unknown>>;
|
|
118
118
|
readonly stepToolChoice?: unknown;
|
|
@@ -120,14 +120,14 @@ interface OtelStepStartEvent<
|
|
|
120
120
|
|
|
121
121
|
interface CallState {
|
|
122
122
|
operationId: string;
|
|
123
|
-
telemetry:
|
|
123
|
+
telemetry: TelemetryOptions | undefined;
|
|
124
124
|
rootSpan: Span | undefined;
|
|
125
|
-
rootContext:
|
|
125
|
+
rootContext: OpenTelemetryContext | undefined;
|
|
126
126
|
stepSpan: Span | undefined;
|
|
127
|
-
stepContext:
|
|
128
|
-
embedSpans: Map<string, { span: Span; context:
|
|
129
|
-
rerankSpan: { span: Span; context:
|
|
130
|
-
toolSpans: Map<string, { span: Span; context:
|
|
127
|
+
stepContext: OpenTelemetryContext | undefined;
|
|
128
|
+
embedSpans: Map<string, { span: Span; context: OpenTelemetryContext }>;
|
|
129
|
+
rerankSpan: { span: Span; context: OpenTelemetryContext } | undefined;
|
|
130
|
+
toolSpans: Map<string, { span: Span; context: OpenTelemetryContext }>;
|
|
131
131
|
baseTelemetryAttributes: Attributes;
|
|
132
132
|
settings: Record<string, unknown>;
|
|
133
133
|
}
|
|
@@ -176,12 +176,12 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
176
176
|
|
|
177
177
|
onStart(
|
|
178
178
|
event:
|
|
179
|
-
| OnStartEvent
|
|
179
|
+
| OnStartEvent
|
|
180
180
|
| ObjectOnStartEvent
|
|
181
181
|
| EmbedOnStartEvent
|
|
182
182
|
| RerankOnStartEvent,
|
|
183
183
|
): void {
|
|
184
|
-
if (event.isEnabled
|
|
184
|
+
if (event.isEnabled === false) return;
|
|
185
185
|
|
|
186
186
|
if (
|
|
187
187
|
event.operationId === 'ai.embed' ||
|
|
@@ -204,16 +204,15 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
204
204
|
return;
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
-
this.onGenerateStart(event as OnStartEvent
|
|
207
|
+
this.onGenerateStart(event as OnStartEvent);
|
|
208
208
|
}
|
|
209
209
|
|
|
210
|
-
private onGenerateStart(event: OnStartEvent
|
|
211
|
-
const telemetry:
|
|
210
|
+
private onGenerateStart(event: OnStartEvent): void {
|
|
211
|
+
const telemetry: TelemetryOptions = {
|
|
212
212
|
isEnabled: event.isEnabled,
|
|
213
213
|
recordInputs: event.recordInputs,
|
|
214
214
|
recordOutputs: event.recordOutputs,
|
|
215
215
|
functionId: event.functionId,
|
|
216
|
-
metadata: event.metadata,
|
|
217
216
|
};
|
|
218
217
|
|
|
219
218
|
const settings: Record<string, unknown> = {
|
|
@@ -230,9 +229,9 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
230
229
|
|
|
231
230
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
232
231
|
model: { provider: event.provider, modelId: event.modelId },
|
|
233
|
-
telemetry,
|
|
234
232
|
headers: event.headers,
|
|
235
233
|
settings,
|
|
234
|
+
context: event.runtimeContext as Record<string, unknown> | undefined,
|
|
236
235
|
});
|
|
237
236
|
|
|
238
237
|
const attributes = selectAttributes(telemetry, {
|
|
@@ -272,12 +271,11 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
272
271
|
}
|
|
273
272
|
|
|
274
273
|
private onObjectOperationStart(event: ObjectOnStartEvent): void {
|
|
275
|
-
const telemetry:
|
|
274
|
+
const telemetry: TelemetryOptions = {
|
|
276
275
|
isEnabled: event.isEnabled,
|
|
277
276
|
recordInputs: event.recordInputs,
|
|
278
277
|
recordOutputs: event.recordOutputs,
|
|
279
278
|
functionId: event.functionId,
|
|
280
|
-
metadata: event.metadata,
|
|
281
279
|
};
|
|
282
280
|
|
|
283
281
|
const settings: Record<string, unknown> = {
|
|
@@ -293,9 +291,9 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
293
291
|
|
|
294
292
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
295
293
|
model: { provider: event.provider, modelId: event.modelId },
|
|
296
|
-
telemetry,
|
|
297
294
|
headers: event.headers,
|
|
298
295
|
settings,
|
|
296
|
+
context: undefined,
|
|
299
297
|
});
|
|
300
298
|
|
|
301
299
|
const attributes = selectAttributes(telemetry, {
|
|
@@ -444,12 +442,11 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
444
442
|
}
|
|
445
443
|
|
|
446
444
|
private onEmbedOperationStart(event: EmbedOnStartEvent): void {
|
|
447
|
-
const telemetry:
|
|
445
|
+
const telemetry: TelemetryOptions = {
|
|
448
446
|
isEnabled: event.isEnabled,
|
|
449
447
|
recordInputs: event.recordInputs,
|
|
450
448
|
recordOutputs: event.recordOutputs,
|
|
451
449
|
functionId: event.functionId,
|
|
452
|
-
metadata: event.metadata,
|
|
453
450
|
};
|
|
454
451
|
|
|
455
452
|
const settings: Record<string, unknown> = {
|
|
@@ -458,9 +455,9 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
458
455
|
|
|
459
456
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
460
457
|
model: { provider: event.provider, modelId: event.modelId },
|
|
461
|
-
telemetry,
|
|
462
458
|
headers: event.headers,
|
|
463
459
|
settings,
|
|
460
|
+
context: undefined,
|
|
464
461
|
});
|
|
465
462
|
|
|
466
463
|
const value = event.value;
|
|
@@ -503,7 +500,7 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
503
500
|
});
|
|
504
501
|
}
|
|
505
502
|
|
|
506
|
-
onStepStart(event: OtelStepStartEvent
|
|
503
|
+
onStepStart(event: OtelStepStartEvent): void {
|
|
507
504
|
const state = this.getCallState(event.callId);
|
|
508
505
|
if (!state?.rootSpan || !state.rootContext) return;
|
|
509
506
|
|
|
@@ -619,7 +616,7 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
619
616
|
},
|
|
620
617
|
}),
|
|
621
618
|
);
|
|
622
|
-
} catch
|
|
619
|
+
} catch {
|
|
623
620
|
// JSON.stringify might fail for non-serializable results
|
|
624
621
|
}
|
|
625
622
|
} else {
|
|
@@ -929,12 +926,11 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
929
926
|
}
|
|
930
927
|
|
|
931
928
|
private onRerankOperationStart(event: RerankOnStartEvent): void {
|
|
932
|
-
const telemetry:
|
|
929
|
+
const telemetry: TelemetryOptions = {
|
|
933
930
|
isEnabled: event.isEnabled,
|
|
934
931
|
recordInputs: event.recordInputs,
|
|
935
932
|
recordOutputs: event.recordOutputs,
|
|
936
933
|
functionId: event.functionId,
|
|
937
|
-
metadata: event.metadata,
|
|
938
934
|
};
|
|
939
935
|
|
|
940
936
|
const settings: Record<string, unknown> = {
|
|
@@ -943,9 +939,9 @@ export class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
|
943
939
|
|
|
944
940
|
const baseTelemetryAttributes = getBaseTelemetryAttributes({
|
|
945
941
|
model: { provider: event.provider, modelId: event.modelId },
|
|
946
|
-
telemetry,
|
|
947
942
|
headers: event.headers,
|
|
948
943
|
settings,
|
|
944
|
+
context: undefined,
|
|
949
945
|
});
|
|
950
946
|
|
|
951
947
|
const attributes = selectAttributes(telemetry, {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Attributes, AttributeValue } from '@opentelemetry/api';
|
|
2
|
-
import type {
|
|
2
|
+
import type { TelemetryOptions } from 'ai';
|
|
3
3
|
|
|
4
4
|
type ResolvableAttributeValue = () =>
|
|
5
5
|
| AttributeValue
|
|
@@ -10,7 +10,7 @@ export async function selectTelemetryAttributes({
|
|
|
10
10
|
telemetry,
|
|
11
11
|
attributes,
|
|
12
12
|
}: {
|
|
13
|
-
telemetry?:
|
|
13
|
+
telemetry?: TelemetryOptions;
|
|
14
14
|
attributes: {
|
|
15
15
|
[attributeKey: string]:
|
|
16
16
|
| AttributeValue
|
|
@@ -20,7 +20,7 @@ export async function selectTelemetryAttributes({
|
|
|
20
20
|
};
|
|
21
21
|
}): Promise<Attributes> {
|
|
22
22
|
// when telemetry is disabled, return an empty object to avoid serialization overhead:
|
|
23
|
-
if (telemetry?.isEnabled
|
|
23
|
+
if (telemetry?.isEnabled === false) {
|
|
24
24
|
return {};
|
|
25
25
|
}
|
|
26
26
|
|
package/dist/index.d.mts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { LanguageModelV4Prompt } from '@ai-sdk/provider';
|
|
2
|
-
import { Tracer } from '@opentelemetry/api';
|
|
3
|
-
import { TelemetryIntegration, OnStartEvent, ToolSet, OutputInterface, ObjectOnStartEvent, EmbedOnStartEvent, RerankOnStartEvent, ObjectOnStepStartEvent, ObjectOnStepFinishEvent, GenerationContext, OnStepStartEvent, OnToolCallStartEvent, OnToolCallFinishEvent, OnStepFinishEvent, OnFinishEvent, ObjectOnFinishEvent, EmbedOnFinishEvent, RerankOnFinishEvent, EmbedStartEvent, EmbedFinishEvent, RerankStartEvent, RerankFinishEvent, OnChunkEvent } from 'ai';
|
|
4
|
-
|
|
5
|
-
interface OtelStepStartEvent<TOOLS extends ToolSet = ToolSet, CONTEXT extends GenerationContext<TOOLS> = GenerationContext<TOOLS>, OUTPUT extends OutputInterface = OutputInterface> extends OnStepStartEvent<TOOLS, CONTEXT, OUTPUT> {
|
|
6
|
-
readonly promptMessages?: LanguageModelV4Prompt;
|
|
7
|
-
readonly stepTools?: ReadonlyArray<Record<string, unknown>>;
|
|
8
|
-
readonly stepToolChoice?: unknown;
|
|
9
|
-
}
|
|
10
|
-
declare class OpenTelemetryIntegration implements TelemetryIntegration {
|
|
11
|
-
private readonly callStates;
|
|
12
|
-
/**
|
|
13
|
-
* The tracer to use for the telemetry data.
|
|
14
|
-
*/
|
|
15
|
-
private readonly tracer;
|
|
16
|
-
constructor(options?: {
|
|
17
|
-
tracer?: Tracer;
|
|
18
|
-
});
|
|
19
|
-
private getCallState;
|
|
20
|
-
private cleanupCallState;
|
|
21
|
-
executeTool<T>({ callId, toolCallId, execute, }: {
|
|
22
|
-
callId: string;
|
|
23
|
-
toolCallId: string;
|
|
24
|
-
execute: () => PromiseLike<T>;
|
|
25
|
-
}): PromiseLike<T>;
|
|
26
|
-
onStart(event: OnStartEvent<ToolSet, OutputInterface> | ObjectOnStartEvent | EmbedOnStartEvent | RerankOnStartEvent): void;
|
|
27
|
-
private onGenerateStart;
|
|
28
|
-
private onObjectOperationStart;
|
|
29
|
-
/** @deprecated */
|
|
30
|
-
onObjectStepStart(event: ObjectOnStepStartEvent): void;
|
|
31
|
-
/** @deprecated */
|
|
32
|
-
onObjectStepFinish(event: ObjectOnStepFinishEvent): void;
|
|
33
|
-
private onEmbedOperationStart;
|
|
34
|
-
onStepStart(event: OtelStepStartEvent<ToolSet, OutputInterface>): void;
|
|
35
|
-
onToolCallStart(event: OnToolCallStartEvent<ToolSet>): void;
|
|
36
|
-
onToolCallFinish(event: OnToolCallFinishEvent<ToolSet>): void;
|
|
37
|
-
onStepFinish(event: OnStepFinishEvent<ToolSet>): void;
|
|
38
|
-
onFinish(event: OnFinishEvent<ToolSet> | ObjectOnFinishEvent<unknown> | EmbedOnFinishEvent | RerankOnFinishEvent): void;
|
|
39
|
-
private onGenerateFinish;
|
|
40
|
-
private onObjectOperationFinish;
|
|
41
|
-
private onEmbedOperationFinish;
|
|
42
|
-
onEmbedStart(event: EmbedStartEvent): void;
|
|
43
|
-
onEmbedFinish(event: EmbedFinishEvent): void;
|
|
44
|
-
private onRerankOperationStart;
|
|
45
|
-
private onRerankOperationFinish;
|
|
46
|
-
onRerankStart(event: RerankStartEvent): void;
|
|
47
|
-
onRerankFinish(event: RerankFinishEvent): void;
|
|
48
|
-
onChunk(event: OnChunkEvent<ToolSet>): void;
|
|
49
|
-
onError(error: unknown): void;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export { OpenTelemetryIntegration };
|