@ai-sdk/otel 1.0.160 → 1.0.162
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 +18 -0
- package/dist/index.d.ts +56 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +371 -70
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/durable-span-ids.ts +190 -0
- package/src/gen-ai-format-messages.ts +1 -0
- package/src/index.ts +1 -0
- package/src/open-telemetry.ts +418 -107
- package/src/supplemental-attributes.ts +4 -0
package/src/open-telemetry.ts
CHANGED
|
@@ -4,12 +4,14 @@ import {
|
|
|
4
4
|
SpanKind,
|
|
5
5
|
SpanStatusCode,
|
|
6
6
|
trace,
|
|
7
|
+
createTraceState,
|
|
7
8
|
type Attributes,
|
|
8
9
|
type Context as OpenTelemetryContext,
|
|
9
10
|
type Span,
|
|
10
11
|
type Tracer,
|
|
11
12
|
} from '@opentelemetry/api';
|
|
12
13
|
import type {
|
|
14
|
+
DurableTelemetryContext,
|
|
13
15
|
EmbeddingModelCallEndEvent,
|
|
14
16
|
EmbedEndEvent,
|
|
15
17
|
EmbedStartEvent,
|
|
@@ -72,6 +74,10 @@ import {
|
|
|
72
74
|
type OpenTelemetrySpanType,
|
|
73
75
|
type SupplementalAttributeOptions,
|
|
74
76
|
} from './supplemental-attributes';
|
|
77
|
+
import {
|
|
78
|
+
deriveSpanId,
|
|
79
|
+
type OpenTelemetryIdGenerator,
|
|
80
|
+
} from './durable-span-ids';
|
|
75
81
|
|
|
76
82
|
export type {
|
|
77
83
|
EnrichSpan,
|
|
@@ -94,6 +100,8 @@ interface CallState {
|
|
|
94
100
|
inferenceContext: OpenTelemetryContext | undefined;
|
|
95
101
|
inferenceMetrics?: {
|
|
96
102
|
startTime: number;
|
|
103
|
+
/** Epoch milliseconds when the call was reported after the fact. */
|
|
104
|
+
startTimestamp?: number;
|
|
97
105
|
provider: string;
|
|
98
106
|
modelId: string;
|
|
99
107
|
};
|
|
@@ -108,6 +116,31 @@ interface CallState {
|
|
|
108
116
|
runtimeContext: Record<string, unknown> | undefined;
|
|
109
117
|
baseSupplementalAttributes: Attributes;
|
|
110
118
|
objectSystemInstructions?: GenerateObjectStartEvent['system'];
|
|
119
|
+
/**
|
|
120
|
+
* The root and step spans of a durable run use derived IDs; its model calls
|
|
121
|
+
* were reported live from the durable steps.
|
|
122
|
+
*/
|
|
123
|
+
durableContext?: DurableTelemetryContext;
|
|
124
|
+
/** A model call reported live from a durable step, without a root span. */
|
|
125
|
+
liveDurableContext?: DurableTelemetryContext;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Returns the time an event occurred when it is reported after the fact.
|
|
130
|
+
* A `Date` is used because OpenTelemetry treats numbers smaller than the
|
|
131
|
+
* process time origin as `performance.now()` values.
|
|
132
|
+
*/
|
|
133
|
+
function getEventTime(event: unknown): Date | undefined {
|
|
134
|
+
const timestamp = getEventTimestamp(event);
|
|
135
|
+
return timestamp == null ? undefined : new Date(timestamp);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function getEventTimestamp(event: unknown): number | undefined {
|
|
139
|
+
const timestamp =
|
|
140
|
+
event != null && typeof event === 'object' && 'timestamp' in event
|
|
141
|
+
? event.timestamp
|
|
142
|
+
: undefined;
|
|
143
|
+
return typeof timestamp === 'number' ? timestamp : undefined;
|
|
111
144
|
}
|
|
112
145
|
|
|
113
146
|
function msToSeconds(durationMs: number | undefined): number | undefined {
|
|
@@ -132,17 +165,50 @@ export class OpenTelemetry implements Telemetry {
|
|
|
132
165
|
private readonly callStates = new Map<string, CallState>();
|
|
133
166
|
|
|
134
167
|
private readonly tracer: Tracer;
|
|
168
|
+
private readonly durable: OpenTelemetryIdGenerator | undefined;
|
|
135
169
|
private readonly agentSpanName: OpenTelemetryOptions['agentSpanName'];
|
|
136
170
|
private readonly supplementalAttributes: SupplementalAttributeOptions;
|
|
137
171
|
private readonly enrichSpan: EnrichSpan | undefined;
|
|
138
172
|
|
|
139
173
|
constructor(options: OpenTelemetryOptions = {}) {
|
|
140
174
|
this.tracer = options.tracer ?? trace.getTracer('gen_ai');
|
|
175
|
+
this.durable = options.durable;
|
|
141
176
|
this.agentSpanName = options.agentSpanName;
|
|
142
177
|
this.supplementalAttributes = normalizeSupplementalAttributes(options);
|
|
143
178
|
this.enrichSpan = options.enrichSpan;
|
|
144
179
|
}
|
|
145
180
|
|
|
181
|
+
readonly experimental_durable: NonNullable<
|
|
182
|
+
Telemetry['experimental_durable']
|
|
183
|
+
> = {
|
|
184
|
+
captureContext: event => {
|
|
185
|
+
if (this.durable == null) return undefined;
|
|
186
|
+
const { spanName, rootSpanOptions } = this.prepareGenerateStart(event);
|
|
187
|
+
return {
|
|
188
|
+
...this.durable.captureContext(
|
|
189
|
+
event.callId,
|
|
190
|
+
spanName,
|
|
191
|
+
SpanKind.INTERNAL,
|
|
192
|
+
rootSpanOptions.attributes,
|
|
193
|
+
),
|
|
194
|
+
rootSpanName: spanName,
|
|
195
|
+
};
|
|
196
|
+
},
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
private withDurableSampling<T>(state: CallState, startSpan: () => T): T {
|
|
200
|
+
const run = state.durableContext ?? state.liveDurableContext;
|
|
201
|
+
return run != null && this.durable != null
|
|
202
|
+
? this.durable.withIds(
|
|
203
|
+
{
|
|
204
|
+
samplingDecision: run.samplingDecision,
|
|
205
|
+
traceState: run.traceState,
|
|
206
|
+
},
|
|
207
|
+
startSpan,
|
|
208
|
+
)
|
|
209
|
+
: startSpan();
|
|
210
|
+
}
|
|
211
|
+
|
|
146
212
|
private getCallState(callId: string): CallState | undefined {
|
|
147
213
|
return this.callStates.get(callId);
|
|
148
214
|
}
|
|
@@ -158,17 +224,26 @@ export class OpenTelemetry implements Telemetry {
|
|
|
158
224
|
provider,
|
|
159
225
|
responseModelId,
|
|
160
226
|
errorType,
|
|
227
|
+
endTimestamp,
|
|
161
228
|
}: {
|
|
162
229
|
responseTimeMs?: number;
|
|
163
230
|
provider?: string;
|
|
164
231
|
responseModelId?: string;
|
|
165
232
|
errorType?: string;
|
|
233
|
+
/** Epoch milliseconds of the event that ends the call, if reported. */
|
|
234
|
+
endTimestamp?: number;
|
|
166
235
|
} = {},
|
|
167
236
|
): void {
|
|
168
237
|
const inference = state.inferenceMetrics;
|
|
169
238
|
if (inference == null) return;
|
|
170
239
|
state.inferenceMetrics = undefined;
|
|
171
240
|
|
|
241
|
+
const durationMs =
|
|
242
|
+
responseTimeMs ??
|
|
243
|
+
(inference.startTimestamp != null && endTimestamp != null
|
|
244
|
+
? Math.max(0, endTimestamp - inference.startTimestamp)
|
|
245
|
+
: performance.now() - inference.startTime);
|
|
246
|
+
|
|
172
247
|
// Resolve the meter when recording so a provider registered after this
|
|
173
248
|
// integration is constructed can still receive metrics.
|
|
174
249
|
const histogram = metrics
|
|
@@ -185,7 +260,7 @@ export class OpenTelemetry implements Telemetry {
|
|
|
185
260
|
});
|
|
186
261
|
|
|
187
262
|
histogram.record(
|
|
188
|
-
|
|
263
|
+
durationMs / 1000,
|
|
189
264
|
selectAttributes(state.telemetry, {
|
|
190
265
|
'gen_ai.operation.name': 'chat',
|
|
191
266
|
'gen_ai.provider.name': mapProviderName(provider ?? inference.provider),
|
|
@@ -446,9 +521,9 @@ export class OpenTelemetry implements Telemetry {
|
|
|
446
521
|
});
|
|
447
522
|
}
|
|
448
523
|
|
|
449
|
-
private
|
|
524
|
+
private prepareGenerateStart(
|
|
450
525
|
event: InferTelemetryEvent<GenerateTextStartEvent>,
|
|
451
|
-
)
|
|
526
|
+
) {
|
|
452
527
|
const telemetry: TelemetryOptions = {
|
|
453
528
|
recordInputs: event.recordInputs,
|
|
454
529
|
recordOutputs: event.recordOutputs,
|
|
@@ -515,22 +590,83 @@ export class OpenTelemetry implements Telemetry {
|
|
|
515
590
|
...baseSupplementalAttributes,
|
|
516
591
|
});
|
|
517
592
|
|
|
518
|
-
const spanName =
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
593
|
+
const spanName =
|
|
594
|
+
event.durableContext?.run.rootSpanName ??
|
|
595
|
+
this.getAgentSpanName({
|
|
596
|
+
operationName,
|
|
597
|
+
modelId: event.modelId,
|
|
598
|
+
functionId: telemetry.functionId,
|
|
599
|
+
});
|
|
600
|
+
const rootSpanOptions = {
|
|
601
|
+
attributes:
|
|
602
|
+
event.durableContext?.run.rootAttributes ??
|
|
603
|
+
this.getSpanAttributes({
|
|
604
|
+
attributes,
|
|
605
|
+
spanType: 'operation',
|
|
606
|
+
operationId: event.operationId,
|
|
607
|
+
callId: event.callId,
|
|
608
|
+
runtimeContext,
|
|
609
|
+
}),
|
|
531
610
|
kind: SpanKind.INTERNAL,
|
|
532
|
-
|
|
533
|
-
|
|
611
|
+
startTime: getEventTime(event),
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
return {
|
|
615
|
+
spanName,
|
|
616
|
+
rootSpanOptions,
|
|
617
|
+
telemetry,
|
|
618
|
+
settings,
|
|
619
|
+
runtimeContext,
|
|
620
|
+
baseSupplementalAttributes,
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
private onGenerateStart(
|
|
625
|
+
event: InferTelemetryEvent<GenerateTextStartEvent>,
|
|
626
|
+
): void {
|
|
627
|
+
const {
|
|
628
|
+
spanName,
|
|
629
|
+
rootSpanOptions,
|
|
630
|
+
telemetry,
|
|
631
|
+
settings,
|
|
632
|
+
runtimeContext,
|
|
633
|
+
baseSupplementalAttributes,
|
|
634
|
+
} = this.prepareGenerateStart(event);
|
|
635
|
+
const run = event.durableContext?.run;
|
|
636
|
+
let parentContext = context.active();
|
|
637
|
+
let rootSpan: Span;
|
|
638
|
+
if (run != null && this.durable != null) {
|
|
639
|
+
parentContext = trace.deleteSpan(parentContext);
|
|
640
|
+
if (run.parentSpanContext != null) {
|
|
641
|
+
parentContext = trace.setSpanContext(parentContext, {
|
|
642
|
+
...run.parentSpanContext,
|
|
643
|
+
traceState:
|
|
644
|
+
run.parentSpanContext.traceState == null
|
|
645
|
+
? undefined
|
|
646
|
+
: createTraceState(run.parentSpanContext.traceState),
|
|
647
|
+
isRemote: true,
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
rootSpan = this.durable.withIds(
|
|
651
|
+
{
|
|
652
|
+
traceId: run.traceId,
|
|
653
|
+
spanId: run.rootSpanId,
|
|
654
|
+
samplingDecision: run.samplingDecision,
|
|
655
|
+
traceState: run.traceState,
|
|
656
|
+
},
|
|
657
|
+
() => this.tracer.startSpan(spanName, rootSpanOptions, parentContext),
|
|
658
|
+
);
|
|
659
|
+
} else {
|
|
660
|
+
rootSpan = this.tracer.startSpan(spanName, rootSpanOptions);
|
|
661
|
+
}
|
|
662
|
+
const actual = rootSpan.spanContext();
|
|
663
|
+
const durableContext =
|
|
664
|
+
run != null &&
|
|
665
|
+
actual.traceId === run.traceId &&
|
|
666
|
+
actual.spanId === run.rootSpanId
|
|
667
|
+
? run
|
|
668
|
+
: undefined;
|
|
669
|
+
const rootContext = trace.setSpan(parentContext, rootSpan);
|
|
534
670
|
|
|
535
671
|
this.callStates.set(event.callId, {
|
|
536
672
|
operationId: event.operationId,
|
|
@@ -550,6 +686,7 @@ export class OpenTelemetry implements Telemetry {
|
|
|
550
686
|
modelId: event.modelId,
|
|
551
687
|
runtimeContext,
|
|
552
688
|
baseSupplementalAttributes,
|
|
689
|
+
durableContext,
|
|
553
690
|
});
|
|
554
691
|
}
|
|
555
692
|
|
|
@@ -890,41 +1027,73 @@ export class OpenTelemetry implements Telemetry {
|
|
|
890
1027
|
}),
|
|
891
1028
|
});
|
|
892
1029
|
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
{
|
|
896
|
-
attributes:
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
1030
|
+
const rootContext = state.rootContext;
|
|
1031
|
+
const options = {
|
|
1032
|
+
attributes: this.getSpanAttributes({
|
|
1033
|
+
attributes: stepAttributes,
|
|
1034
|
+
spanType: 'step',
|
|
1035
|
+
operationId: state.operationId,
|
|
1036
|
+
callId: event.callId,
|
|
1037
|
+
runtimeContext: state.runtimeContext,
|
|
1038
|
+
}),
|
|
1039
|
+
kind: SpanKind.INTERNAL,
|
|
1040
|
+
startTime: getEventTime(event),
|
|
1041
|
+
};
|
|
1042
|
+
const startStepSpan = () =>
|
|
1043
|
+
this.tracer.startSpan(
|
|
1044
|
+
`step ${event.steps.length + 1}`,
|
|
1045
|
+
options,
|
|
1046
|
+
rootContext,
|
|
1047
|
+
);
|
|
1048
|
+
state.stepSpan =
|
|
1049
|
+
state.durableContext != null && this.durable != null
|
|
1050
|
+
? this.durable.withIds(
|
|
1051
|
+
{
|
|
1052
|
+
spanId: deriveSpanId(event.callId, 'step', event.stepNumber),
|
|
1053
|
+
samplingDecision: state.durableContext.samplingDecision,
|
|
1054
|
+
traceState: state.durableContext.traceState,
|
|
1055
|
+
},
|
|
1056
|
+
startStepSpan,
|
|
1057
|
+
)
|
|
1058
|
+
: startStepSpan();
|
|
1059
|
+
state.stepContext = trace.setSpan(rootContext, state.stepSpan);
|
|
908
1060
|
}
|
|
909
1061
|
|
|
910
|
-
onLanguageModelCallStart(
|
|
911
|
-
|
|
1062
|
+
onLanguageModelCallStart(
|
|
1063
|
+
event: InferTelemetryEvent<LanguageModelCallStartEvent>,
|
|
1064
|
+
): void {
|
|
1065
|
+
const existingState = this.getCallState(event.callId);
|
|
1066
|
+
// The durable steps already reported the run's model calls live.
|
|
1067
|
+
if (existingState?.durableContext) return;
|
|
1068
|
+
|
|
1069
|
+
const state = existingState ?? this.startDurableModelCall(event);
|
|
912
1070
|
if (!state?.stepContext) return;
|
|
913
1071
|
|
|
914
1072
|
// A retried stream may start a new attempt without ending the failed one.
|
|
915
|
-
|
|
1073
|
+
if (state.liveDurableContext && state.inferenceSpan != null) {
|
|
1074
|
+
state.inferenceSpan.setStatus({ code: SpanStatusCode.ERROR });
|
|
1075
|
+
state.inferenceSpan.end(getEventTime(event));
|
|
1076
|
+
state.inferenceSpan = undefined;
|
|
1077
|
+
state.inferenceContext = undefined;
|
|
1078
|
+
}
|
|
1079
|
+
this.recordInferenceDuration(state, {
|
|
1080
|
+
errorType: '_OTHER',
|
|
1081
|
+
endTimestamp: getEventTimestamp(event),
|
|
1082
|
+
});
|
|
916
1083
|
|
|
917
1084
|
const { telemetry } = state;
|
|
918
1085
|
const providerName = mapProviderName(event.provider);
|
|
919
1086
|
state.inferenceToolDefinitions = event.tools;
|
|
920
1087
|
state.inferenceMetrics = {
|
|
921
1088
|
startTime: performance.now(),
|
|
1089
|
+
startTimestamp: getEventTimestamp(event),
|
|
922
1090
|
provider: event.provider,
|
|
923
1091
|
modelId: event.modelId,
|
|
924
1092
|
};
|
|
925
1093
|
|
|
926
1094
|
const inferenceAttributes = selectAttributes(telemetry, {
|
|
927
1095
|
'gen_ai.operation.name': 'chat',
|
|
1096
|
+
...(state.liveDurableContext ? state.baseSupplementalAttributes : {}),
|
|
928
1097
|
'gen_ai.provider.name': providerName,
|
|
929
1098
|
'gen_ai.request.model': event.modelId,
|
|
930
1099
|
'gen_ai.request.frequency_penalty': state.settings.frequencyPenalty as
|
|
@@ -968,20 +1137,24 @@ export class OpenTelemetry implements Telemetry {
|
|
|
968
1137
|
},
|
|
969
1138
|
});
|
|
970
1139
|
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
}
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
1140
|
+
const inferenceSpanAttributes = this.getSpanAttributes({
|
|
1141
|
+
attributes: inferenceAttributes,
|
|
1142
|
+
spanType: 'languageModel',
|
|
1143
|
+
operationId: state.operationId,
|
|
1144
|
+
callId: event.callId,
|
|
1145
|
+
runtimeContext: state.runtimeContext,
|
|
1146
|
+
});
|
|
1147
|
+
const startInferenceSpan = () =>
|
|
1148
|
+
this.tracer.startSpan(
|
|
1149
|
+
`chat ${event.modelId}`,
|
|
1150
|
+
{
|
|
1151
|
+
attributes: inferenceSpanAttributes,
|
|
1152
|
+
kind: SpanKind.CLIENT,
|
|
1153
|
+
startTime: getEventTime(event),
|
|
1154
|
+
},
|
|
1155
|
+
state.stepContext,
|
|
1156
|
+
);
|
|
1157
|
+
state.inferenceSpan = this.withDurableSampling(state, startInferenceSpan);
|
|
985
1158
|
state.inferenceContext = trace.setSpan(
|
|
986
1159
|
state.stepContext,
|
|
987
1160
|
state.inferenceSpan,
|
|
@@ -990,13 +1163,14 @@ export class OpenTelemetry implements Telemetry {
|
|
|
990
1163
|
|
|
991
1164
|
onLanguageModelCallEnd(event: LanguageModelCallEndEvent<ToolSet>): void {
|
|
992
1165
|
const state = this.getCallState(event.callId);
|
|
993
|
-
if (!state?.inferenceSpan) return;
|
|
1166
|
+
if (state?.durableContext || !state?.inferenceSpan) return;
|
|
994
1167
|
|
|
995
1168
|
this.recordInferenceDuration(state, {
|
|
996
1169
|
responseTimeMs: event.performance.responseTimeMs,
|
|
997
1170
|
provider: event.provider,
|
|
998
1171
|
responseModelId: event.modelId,
|
|
999
1172
|
errorType: event.finishReason === 'error' ? '_OTHER' : undefined,
|
|
1173
|
+
endTimestamp: getEventTimestamp(event),
|
|
1000
1174
|
});
|
|
1001
1175
|
|
|
1002
1176
|
if (event.finishReason === 'error') {
|
|
@@ -1100,6 +1274,7 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1100
1274
|
}
|
|
1101
1275
|
}
|
|
1102
1276
|
|
|
1277
|
+
const endTime = getEventTime(event);
|
|
1103
1278
|
const recordedToolCallIds = new Set<string>();
|
|
1104
1279
|
for (const part of event.content) {
|
|
1105
1280
|
if (
|
|
@@ -1111,27 +1286,31 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1111
1286
|
}
|
|
1112
1287
|
recordedToolCallIds.add(part.toolCallId);
|
|
1113
1288
|
|
|
1114
|
-
const
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
input: () => JSON.stringify(part.input),
|
|
1125
|
-
},
|
|
1126
|
-
}),
|
|
1127
|
-
spanType: 'tool',
|
|
1128
|
-
operationId: state.operationId,
|
|
1129
|
-
callId: event.callId,
|
|
1130
|
-
runtimeContext: state.runtimeContext,
|
|
1289
|
+
const toolSpanOptions = {
|
|
1290
|
+
attributes: this.getSpanAttributes({
|
|
1291
|
+
attributes: selectAttributes(telemetry, {
|
|
1292
|
+
'gen_ai.operation.name': 'execute_tool',
|
|
1293
|
+
'gen_ai.tool.name': part.toolName,
|
|
1294
|
+
'gen_ai.tool.call.id': part.toolCallId,
|
|
1295
|
+
'gen_ai.tool.type': 'extension',
|
|
1296
|
+
'gen_ai.tool.call.arguments': {
|
|
1297
|
+
input: () => JSON.stringify(part.input),
|
|
1298
|
+
},
|
|
1131
1299
|
}),
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1300
|
+
spanType: 'tool',
|
|
1301
|
+
operationId: state.operationId,
|
|
1302
|
+
callId: event.callId,
|
|
1303
|
+
runtimeContext: state.runtimeContext,
|
|
1304
|
+
}),
|
|
1305
|
+
kind: SpanKind.INTERNAL,
|
|
1306
|
+
startTime: endTime,
|
|
1307
|
+
};
|
|
1308
|
+
const toolSpan = this.withDurableSampling(state, () =>
|
|
1309
|
+
this.tracer.startSpan(
|
|
1310
|
+
`execute_tool ${part.toolName}`,
|
|
1311
|
+
toolSpanOptions,
|
|
1312
|
+
state.inferenceContext,
|
|
1313
|
+
),
|
|
1135
1314
|
);
|
|
1136
1315
|
|
|
1137
1316
|
const toolOutput = finalToolOutputs.get(part.toolCallId);
|
|
@@ -1151,13 +1330,121 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1151
1330
|
recordErrorOnSpan(toolSpan, toolOutput.error);
|
|
1152
1331
|
}
|
|
1153
1332
|
|
|
1154
|
-
toolSpan.end();
|
|
1333
|
+
toolSpan.end(endTime);
|
|
1155
1334
|
}
|
|
1156
1335
|
|
|
1157
|
-
state.inferenceSpan.end();
|
|
1336
|
+
state.inferenceSpan.end(endTime);
|
|
1158
1337
|
state.inferenceSpan = undefined;
|
|
1159
1338
|
state.inferenceContext = undefined;
|
|
1160
1339
|
state.inferenceToolDefinitions = undefined;
|
|
1340
|
+
|
|
1341
|
+
if (state.liveDurableContext) {
|
|
1342
|
+
this.cleanupCallState(event.callId);
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
/**
|
|
1347
|
+
* Starts a model call reported live from a durable step. The run's root and
|
|
1348
|
+
* step spans are reported when the run ends, so the model call is parented
|
|
1349
|
+
* to the step span's derived ID.
|
|
1350
|
+
*/
|
|
1351
|
+
private startDurableModelCall(
|
|
1352
|
+
event: InferTelemetryEvent<LanguageModelCallStartEvent>,
|
|
1353
|
+
): CallState | undefined {
|
|
1354
|
+
const durable = event.durableContext;
|
|
1355
|
+
if (
|
|
1356
|
+
durable?.phase !== 'live' ||
|
|
1357
|
+
durable.stepNumber == null ||
|
|
1358
|
+
this.durable == null
|
|
1359
|
+
)
|
|
1360
|
+
return undefined;
|
|
1361
|
+
const run = durable.run;
|
|
1362
|
+
const stepContext = trace.setSpanContext(context.active(), {
|
|
1363
|
+
traceId: run.traceId,
|
|
1364
|
+
spanId: deriveSpanId(event.callId, 'step', durable.stepNumber),
|
|
1365
|
+
traceFlags: run.traceFlags,
|
|
1366
|
+
traceState:
|
|
1367
|
+
run.traceState == null ? undefined : createTraceState(run.traceState),
|
|
1368
|
+
isRemote: true,
|
|
1369
|
+
});
|
|
1370
|
+
const telemetryEvent =
|
|
1371
|
+
event as InferTelemetryEvent<LanguageModelCallStartEvent>;
|
|
1372
|
+
const state: CallState = {
|
|
1373
|
+
operationId: 'ai.workflowAgent.stream',
|
|
1374
|
+
telemetry: {
|
|
1375
|
+
recordInputs: telemetryEvent.recordInputs,
|
|
1376
|
+
recordOutputs: telemetryEvent.recordOutputs,
|
|
1377
|
+
functionId: telemetryEvent.functionId,
|
|
1378
|
+
},
|
|
1379
|
+
rootSpan: undefined,
|
|
1380
|
+
rootContext: undefined,
|
|
1381
|
+
stepSpan: undefined,
|
|
1382
|
+
stepContext,
|
|
1383
|
+
inferenceSpan: undefined,
|
|
1384
|
+
inferenceContext: undefined,
|
|
1385
|
+
embedSpans: new Map(),
|
|
1386
|
+
rerankSpan: undefined,
|
|
1387
|
+
decisionSpan: undefined,
|
|
1388
|
+
toolSpans: new Map(),
|
|
1389
|
+
settings: {
|
|
1390
|
+
maxOutputTokens: event.maxOutputTokens,
|
|
1391
|
+
temperature: event.temperature,
|
|
1392
|
+
topP: event.topP,
|
|
1393
|
+
topK: event.topK,
|
|
1394
|
+
presencePenalty: event.presencePenalty,
|
|
1395
|
+
frequencyPenalty: event.frequencyPenalty,
|
|
1396
|
+
stopSequences: event.stopSequences,
|
|
1397
|
+
seed: event.seed,
|
|
1398
|
+
},
|
|
1399
|
+
provider: event.provider,
|
|
1400
|
+
modelId: event.modelId,
|
|
1401
|
+
runtimeContext: durable.runtimeContext,
|
|
1402
|
+
baseSupplementalAttributes: selectSupplementalAttributes(
|
|
1403
|
+
{
|
|
1404
|
+
recordInputs: telemetryEvent.recordInputs,
|
|
1405
|
+
recordOutputs: telemetryEvent.recordOutputs,
|
|
1406
|
+
},
|
|
1407
|
+
this.supplementalAttributes,
|
|
1408
|
+
{
|
|
1409
|
+
runtimeContext: getRuntimeContextAttributes(durable.runtimeContext),
|
|
1410
|
+
headers: getHeaderAttributes(durable.headers),
|
|
1411
|
+
},
|
|
1412
|
+
),
|
|
1413
|
+
liveDurableContext: run,
|
|
1414
|
+
};
|
|
1415
|
+
this.callStates.set(event.callId, state);
|
|
1416
|
+
return state;
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
/**
|
|
1420
|
+
* Ends a model call reported live from a durable step that failed or was
|
|
1421
|
+
* aborted before its end event.
|
|
1422
|
+
*/
|
|
1423
|
+
private endDurableModelCall(
|
|
1424
|
+
callId: string,
|
|
1425
|
+
{
|
|
1426
|
+
error,
|
|
1427
|
+
errorType,
|
|
1428
|
+
endTimestamp,
|
|
1429
|
+
}: {
|
|
1430
|
+
error?: unknown;
|
|
1431
|
+
errorType: string;
|
|
1432
|
+
endTimestamp: number | undefined;
|
|
1433
|
+
},
|
|
1434
|
+
): void {
|
|
1435
|
+
const state = this.getCallState(callId);
|
|
1436
|
+
if (!state?.liveDurableContext) return;
|
|
1437
|
+
|
|
1438
|
+
this.recordInferenceDuration(state, { errorType, endTimestamp });
|
|
1439
|
+
if (state.inferenceSpan) {
|
|
1440
|
+
if (error !== undefined) {
|
|
1441
|
+
recordErrorOnSpan(state.inferenceSpan, error);
|
|
1442
|
+
}
|
|
1443
|
+
state.inferenceSpan.end(
|
|
1444
|
+
endTimestamp == null ? undefined : new Date(endTimestamp),
|
|
1445
|
+
);
|
|
1446
|
+
}
|
|
1447
|
+
this.cleanupCallState(callId);
|
|
1161
1448
|
}
|
|
1162
1449
|
|
|
1163
1450
|
onToolExecutionStart(event: ToolExecutionStartEvent<ToolSet>): void {
|
|
@@ -1179,19 +1466,19 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1179
1466
|
});
|
|
1180
1467
|
|
|
1181
1468
|
const spanName = `execute_tool ${toolCall.toolName}`;
|
|
1182
|
-
const
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
parentContext,
|
|
1469
|
+
const toolSpanOptions = {
|
|
1470
|
+
attributes: this.getSpanAttributes({
|
|
1471
|
+
attributes,
|
|
1472
|
+
spanType: 'tool',
|
|
1473
|
+
operationId: state.operationId,
|
|
1474
|
+
callId: event.callId,
|
|
1475
|
+
runtimeContext: state.runtimeContext,
|
|
1476
|
+
}),
|
|
1477
|
+
kind: SpanKind.INTERNAL,
|
|
1478
|
+
startTime: getEventTime(event),
|
|
1479
|
+
};
|
|
1480
|
+
const toolSpan = this.withDurableSampling(state, () =>
|
|
1481
|
+
this.tracer.startSpan(spanName, toolSpanOptions, parentContext),
|
|
1195
1482
|
);
|
|
1196
1483
|
const toolContext = trace.setSpan(parentContext, toolSpan);
|
|
1197
1484
|
|
|
@@ -1234,7 +1521,7 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1234
1521
|
recordErrorOnSpan(span, toolOutput.error);
|
|
1235
1522
|
}
|
|
1236
1523
|
|
|
1237
|
-
span.end();
|
|
1524
|
+
span.end(getEventTime(event));
|
|
1238
1525
|
state.toolSpans.delete(event.toolCall.toolCallId);
|
|
1239
1526
|
}
|
|
1240
1527
|
|
|
@@ -1245,6 +1532,7 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1245
1532
|
// Streams can close after an error chunk without a model-call end event.
|
|
1246
1533
|
this.recordInferenceDuration(state, {
|
|
1247
1534
|
errorType: event.finishReason === 'error' ? '_OTHER' : undefined,
|
|
1535
|
+
endTimestamp: getEventTimestamp(event),
|
|
1248
1536
|
});
|
|
1249
1537
|
|
|
1250
1538
|
if (event.finishReason === 'error') {
|
|
@@ -1264,7 +1552,7 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1264
1552
|
}),
|
|
1265
1553
|
);
|
|
1266
1554
|
|
|
1267
|
-
state.stepSpan.end();
|
|
1555
|
+
state.stepSpan.end(getEventTime(event));
|
|
1268
1556
|
state.stepSpan = undefined;
|
|
1269
1557
|
state.stepContext = undefined;
|
|
1270
1558
|
}
|
|
@@ -1280,17 +1568,18 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1280
1568
|
|
|
1281
1569
|
this.recordInferenceDuration(state, {
|
|
1282
1570
|
errorType: event.error instanceof Error ? event.error.name : '_OTHER',
|
|
1571
|
+
endTimestamp: getEventTimestamp(event),
|
|
1283
1572
|
});
|
|
1284
1573
|
|
|
1285
1574
|
if (state.inferenceSpan) {
|
|
1286
1575
|
recordErrorOnSpan(state.inferenceSpan, event.error);
|
|
1287
|
-
state.inferenceSpan.end();
|
|
1576
|
+
state.inferenceSpan.end(getEventTime(event));
|
|
1288
1577
|
state.inferenceSpan = undefined;
|
|
1289
1578
|
state.inferenceContext = undefined;
|
|
1290
1579
|
}
|
|
1291
1580
|
|
|
1292
1581
|
recordErrorOnSpan(state.stepSpan, event.error);
|
|
1293
|
-
state.stepSpan.end();
|
|
1582
|
+
state.stepSpan.end(getEventTime(event));
|
|
1294
1583
|
state.stepSpan = undefined;
|
|
1295
1584
|
state.stepContext = undefined;
|
|
1296
1585
|
}
|
|
@@ -1474,7 +1763,7 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1474
1763
|
}),
|
|
1475
1764
|
);
|
|
1476
1765
|
|
|
1477
|
-
state.rootSpan.end();
|
|
1766
|
+
state.rootSpan.end(getEventTime(event));
|
|
1478
1767
|
this.cleanupCallState(event.callId);
|
|
1479
1768
|
}
|
|
1480
1769
|
|
|
@@ -1981,43 +2270,55 @@ export class OpenTelemetry implements Telemetry {
|
|
|
1981
2270
|
|
|
1982
2271
|
onAbort(event: GenerateTextAbortEvent<ToolSet>): void {
|
|
1983
2272
|
const state = this.getCallState(event.callId);
|
|
2273
|
+
if (state?.liveDurableContext) {
|
|
2274
|
+
this.endDurableModelCall(event.callId, {
|
|
2275
|
+
errorType: 'AbortError',
|
|
2276
|
+
endTimestamp: getEventTimestamp(event),
|
|
2277
|
+
});
|
|
2278
|
+
return;
|
|
2279
|
+
}
|
|
1984
2280
|
if (!state?.rootSpan) return;
|
|
1985
2281
|
|
|
1986
|
-
|
|
2282
|
+
const endTime = getEventTime(event);
|
|
2283
|
+
|
|
2284
|
+
this.recordInferenceDuration(state, {
|
|
2285
|
+
errorType: 'AbortError',
|
|
2286
|
+
endTimestamp: getEventTimestamp(event),
|
|
2287
|
+
});
|
|
1987
2288
|
|
|
1988
2289
|
for (const { span: toolSpan } of state.toolSpans.values()) {
|
|
1989
|
-
toolSpan.end();
|
|
2290
|
+
toolSpan.end(endTime);
|
|
1990
2291
|
}
|
|
1991
2292
|
state.toolSpans.clear();
|
|
1992
2293
|
|
|
1993
2294
|
if (state.inferenceSpan) {
|
|
1994
|
-
state.inferenceSpan.end();
|
|
2295
|
+
state.inferenceSpan.end(endTime);
|
|
1995
2296
|
state.inferenceSpan = undefined;
|
|
1996
2297
|
state.inferenceContext = undefined;
|
|
1997
2298
|
}
|
|
1998
2299
|
|
|
1999
2300
|
if (state.stepSpan) {
|
|
2000
|
-
state.stepSpan.end();
|
|
2301
|
+
state.stepSpan.end(endTime);
|
|
2001
2302
|
state.stepSpan = undefined;
|
|
2002
2303
|
state.stepContext = undefined;
|
|
2003
2304
|
}
|
|
2004
2305
|
|
|
2005
2306
|
for (const { span: embedSpan } of state.embedSpans.values()) {
|
|
2006
|
-
embedSpan.end();
|
|
2307
|
+
embedSpan.end(endTime);
|
|
2007
2308
|
}
|
|
2008
2309
|
state.embedSpans.clear();
|
|
2009
2310
|
|
|
2010
2311
|
if (state.rerankSpan) {
|
|
2011
|
-
state.rerankSpan.span.end();
|
|
2312
|
+
state.rerankSpan.span.end(endTime);
|
|
2012
2313
|
state.rerankSpan = undefined;
|
|
2013
2314
|
}
|
|
2014
2315
|
|
|
2015
2316
|
if (state.decisionSpan) {
|
|
2016
|
-
state.decisionSpan.span.end();
|
|
2317
|
+
state.decisionSpan.span.end(endTime);
|
|
2017
2318
|
state.decisionSpan = undefined;
|
|
2018
2319
|
}
|
|
2019
2320
|
|
|
2020
|
-
state.rootSpan.end();
|
|
2321
|
+
state.rootSpan.end(endTime);
|
|
2021
2322
|
this.cleanupCallState(event.callId);
|
|
2022
2323
|
}
|
|
2023
2324
|
|
|
@@ -2026,55 +2327,65 @@ export class OpenTelemetry implements Telemetry {
|
|
|
2026
2327
|
if (!event?.callId) return;
|
|
2027
2328
|
|
|
2028
2329
|
const state = this.getCallState(event.callId);
|
|
2330
|
+
const actualError = event.error ?? error;
|
|
2331
|
+
if (state?.liveDurableContext) {
|
|
2332
|
+
this.endDurableModelCall(event.callId, {
|
|
2333
|
+
error: actualError,
|
|
2334
|
+
errorType: actualError instanceof Error ? actualError.name : '_OTHER',
|
|
2335
|
+
endTimestamp: getEventTimestamp(event),
|
|
2336
|
+
});
|
|
2337
|
+
return;
|
|
2338
|
+
}
|
|
2029
2339
|
if (!state?.rootSpan) return;
|
|
2030
2340
|
|
|
2031
|
-
const
|
|
2341
|
+
const endTime = getEventTime(event);
|
|
2032
2342
|
|
|
2033
2343
|
this.recordInferenceDuration(state, {
|
|
2034
2344
|
errorType: actualError instanceof Error ? actualError.name : '_OTHER',
|
|
2345
|
+
endTimestamp: getEventTimestamp(event),
|
|
2035
2346
|
});
|
|
2036
2347
|
|
|
2037
2348
|
for (const { span: toolSpan } of state.toolSpans.values()) {
|
|
2038
2349
|
recordErrorOnSpan(toolSpan, actualError);
|
|
2039
|
-
toolSpan.end();
|
|
2350
|
+
toolSpan.end(endTime);
|
|
2040
2351
|
}
|
|
2041
2352
|
state.toolSpans.clear();
|
|
2042
2353
|
|
|
2043
2354
|
if (state.inferenceSpan) {
|
|
2044
2355
|
recordErrorOnSpan(state.inferenceSpan, actualError);
|
|
2045
|
-
state.inferenceSpan.end();
|
|
2356
|
+
state.inferenceSpan.end(endTime);
|
|
2046
2357
|
state.inferenceSpan = undefined;
|
|
2047
2358
|
state.inferenceContext = undefined;
|
|
2048
2359
|
}
|
|
2049
2360
|
|
|
2050
2361
|
if (state.stepSpan) {
|
|
2051
2362
|
recordErrorOnSpan(state.stepSpan, actualError);
|
|
2052
|
-
state.stepSpan.end();
|
|
2363
|
+
state.stepSpan.end(endTime);
|
|
2053
2364
|
state.stepSpan = undefined;
|
|
2054
2365
|
state.stepContext = undefined;
|
|
2055
2366
|
}
|
|
2056
2367
|
|
|
2057
2368
|
for (const { span: embedSpan } of state.embedSpans.values()) {
|
|
2058
2369
|
recordErrorOnSpan(embedSpan, actualError);
|
|
2059
|
-
embedSpan.end();
|
|
2370
|
+
embedSpan.end(endTime);
|
|
2060
2371
|
}
|
|
2061
2372
|
state.embedSpans.clear();
|
|
2062
2373
|
|
|
2063
2374
|
if (state.rerankSpan) {
|
|
2064
2375
|
recordErrorOnSpan(state.rerankSpan.span, actualError);
|
|
2065
|
-
state.rerankSpan.span.end();
|
|
2376
|
+
state.rerankSpan.span.end(endTime);
|
|
2066
2377
|
state.rerankSpan = undefined;
|
|
2067
2378
|
}
|
|
2068
2379
|
|
|
2069
2380
|
if (state.decisionSpan) {
|
|
2070
2381
|
recordErrorOnSpan(state.decisionSpan.span, actualError);
|
|
2071
|
-
state.decisionSpan.span.end();
|
|
2382
|
+
state.decisionSpan.span.end(endTime);
|
|
2072
2383
|
state.decisionSpan = undefined;
|
|
2073
2384
|
}
|
|
2074
2385
|
|
|
2075
2386
|
recordErrorOnSpan(state.rootSpan, actualError);
|
|
2076
2387
|
|
|
2077
|
-
state.rootSpan.end();
|
|
2388
|
+
state.rootSpan.end(endTime);
|
|
2078
2389
|
this.cleanupCallState(event.callId);
|
|
2079
2390
|
}
|
|
2080
2391
|
}
|