@ai-sdk/otel 1.0.161 → 1.0.163

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/otel",
3
- "version": "1.0.161",
3
+ "version": "1.0.163",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -26,12 +26,12 @@
26
26
  }
27
27
  },
28
28
  "dependencies": {
29
- "@ai-sdk/provider": "4.0.35",
29
+ "@ai-sdk/provider": "4.0.36",
30
30
  "@opentelemetry/api": "1.9.1",
31
- "ai": "7.0.161"
31
+ "ai": "7.0.163"
32
32
  },
33
33
  "devDependencies": {
34
- "@ai-sdk/provider-utils": "5.0.73",
34
+ "@ai-sdk/provider-utils": "5.0.75",
35
35
  "@opentelemetry/sdk-metrics": "2.8.0",
36
36
  "@opentelemetry/sdk-trace-base": "2.7.1",
37
37
  "@types/node": "22.19.19",
@@ -0,0 +1,190 @@
1
+ import {
2
+ context,
3
+ trace,
4
+ TraceFlags,
5
+ createTraceState,
6
+ isSpanContextValid,
7
+ type Attributes,
8
+ type Context,
9
+ type Link,
10
+ type SpanKind,
11
+ type TraceState,
12
+ } from '@opentelemetry/api';
13
+ import type { DurableTelemetryContext } from 'ai';
14
+
15
+ /** Compatible with the OpenTelemetry SDK's Sampler interface. */
16
+ type Sampler = {
17
+ shouldSample(
18
+ parentContext: Context,
19
+ traceId: string,
20
+ spanName: string,
21
+ spanKind: SpanKind,
22
+ attributes: Attributes,
23
+ links: Link[],
24
+ ): {
25
+ decision: 0 | 1 | 2;
26
+ traceState?: TraceState;
27
+ attributes?: Attributes;
28
+ };
29
+ };
30
+
31
+ /**
32
+ * Instance-scoped SDK bridge for durable tracing. Pass the same instance as
33
+ * the SDK's idGenerator and sampler, and as OpenTelemetry's durable option.
34
+ * Without a custom sampler, uses parent-based sampling with roots enabled.
35
+ */
36
+ export class OpenTelemetryIdGenerator {
37
+ private requested?: {
38
+ traceId?: string;
39
+ spanId?: string;
40
+ samplingDecision?: 0 | 1 | 2;
41
+ traceState?: string;
42
+ };
43
+
44
+ constructor(private readonly options: { sampler?: Sampler } = {}) {}
45
+
46
+ generateTraceId(): string {
47
+ const requested = this.requested?.traceId;
48
+ if (requested == null) return randomHex(16);
49
+ this.requested!.traceId = undefined;
50
+ return requested;
51
+ }
52
+
53
+ generateSpanId(): string {
54
+ const requested = this.requested?.spanId;
55
+ if (requested == null) return randomHex(8);
56
+ this.requested!.spanId = undefined;
57
+ return requested;
58
+ }
59
+
60
+ shouldSample(
61
+ ...args: Parameters<Sampler['shouldSample']>
62
+ ): ReturnType<Sampler['shouldSample']> {
63
+ if (this.requested?.samplingDecision != null) {
64
+ return {
65
+ decision: this.requested.samplingDecision,
66
+ traceState:
67
+ this.requested.traceState == null
68
+ ? trace.getSpanContext(args[0])?.traceState
69
+ : createTraceState(this.requested.traceState),
70
+ };
71
+ }
72
+ if (this.options.sampler != null)
73
+ return this.options.sampler.shouldSample(...args);
74
+ const parent = trace.getSpanContext(args[0]);
75
+ return {
76
+ decision:
77
+ parent == null ||
78
+ !isSpanContextValid(parent) ||
79
+ (parent.traceFlags & TraceFlags.SAMPLED) !== 0
80
+ ? 2
81
+ : 0,
82
+ traceState: parent?.traceState,
83
+ };
84
+ }
85
+
86
+ toString(): string {
87
+ return 'AiSdkDurableSampler';
88
+ }
89
+
90
+ captureContext(
91
+ callId: string,
92
+ name: string,
93
+ kind: SpanKind,
94
+ attributes: Attributes,
95
+ ): DurableTelemetryContext {
96
+ const activeParent = trace.getSpanContext(context.active());
97
+ const parent =
98
+ activeParent != null && isSpanContextValid(activeParent)
99
+ ? activeParent
100
+ : undefined;
101
+ const traceId = parent?.traceId ?? deriveTraceId(callId);
102
+ const sampling = this.shouldSample(
103
+ context.active(),
104
+ traceId,
105
+ name,
106
+ kind,
107
+ attributes,
108
+ [],
109
+ );
110
+ return {
111
+ traceId,
112
+ rootSpanId: deriveSpanId(callId, 'root'),
113
+ traceFlags:
114
+ sampling.decision === 2 ? TraceFlags.SAMPLED : TraceFlags.NONE,
115
+ samplingDecision: sampling.decision,
116
+ rootAttributes: { ...attributes, ...sampling.attributes },
117
+ traceState:
118
+ sampling.traceState?.serialize() ?? parent?.traceState?.serialize(),
119
+ ...(parent == null
120
+ ? {}
121
+ : {
122
+ parentSpanContext: {
123
+ traceId: parent.traceId,
124
+ spanId: parent.spanId,
125
+ traceFlags: parent.traceFlags,
126
+ traceState: parent.traceState?.serialize(),
127
+ },
128
+ }),
129
+ };
130
+ }
131
+
132
+ /** Scope requested IDs to this bridge and restore nested requests. */
133
+ withIds<T>(
134
+ ids: {
135
+ traceId?: string;
136
+ spanId?: string;
137
+ samplingDecision?: 0 | 1 | 2;
138
+ traceState?: string;
139
+ },
140
+ startSpan: () => T,
141
+ ): T {
142
+ const previous = this.requested;
143
+ this.requested = { ...ids };
144
+ try {
145
+ return startSpan();
146
+ } finally {
147
+ this.requested = previous;
148
+ }
149
+ }
150
+ }
151
+
152
+ /** Derives the trace ID of a durable run from its telemetry call ID. */
153
+ export function deriveTraceId(callId: string): string {
154
+ return deriveHex(`${callId}:trace`, 4);
155
+ }
156
+
157
+ /** Derives a span ID of a durable run from its telemetry call ID. */
158
+ export function deriveSpanId(callId: string, ...path: Array<string | number>) {
159
+ return deriveHex([callId, ...path].join(':'), 2);
160
+ }
161
+
162
+ function randomHex(bytes: number): string {
163
+ const values = crypto.getRandomValues(new Uint8Array(bytes));
164
+ const hex = Array.from(values, value =>
165
+ value.toString(16).padStart(2, '0'),
166
+ ).join('');
167
+ return nonZero(hex);
168
+ }
169
+
170
+ /**
171
+ * Non-cryptographic hash (FNV-1a with one seed per 32-bit word). Collisions
172
+ * only matter between spans of the same run.
173
+ */
174
+ function deriveHex(input: string, words: number): string {
175
+ let hex = '';
176
+ for (let word = 0; word < words; word++) {
177
+ let hash = (0x811c9dc5 ^ Math.imul(word + 1, 0x9e3779b1)) >>> 0;
178
+ for (let index = 0; index < input.length; index++) {
179
+ hash ^= input.charCodeAt(index);
180
+ hash = Math.imul(hash, 0x01000193) >>> 0;
181
+ }
182
+ hex += hash.toString(16).padStart(8, '0');
183
+ }
184
+ return nonZero(hex);
185
+ }
186
+
187
+ /** All-zero IDs are invalid in OpenTelemetry. */
188
+ function nonZero(hex: string): string {
189
+ return /^0+$/.test(hex) ? `${hex.slice(0, -1)}1` : hex;
190
+ }
@@ -117,6 +117,7 @@ export function mapOperationName(operationId: string): string {
117
117
  'ai.streamText': 'invoke_agent',
118
118
  'ai.generateObject': 'invoke_agent',
119
119
  'ai.streamObject': 'invoke_agent',
120
+ 'ai.workflowAgent.stream': 'invoke_agent',
120
121
  'ai.embed': 'embeddings',
121
122
  'ai.embedMany': 'embeddings',
122
123
  'ai.rerank': 'rerank',
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { OpenTelemetryIdGenerator } from './durable-span-ids';
1
2
  export { OpenTelemetry } from './open-telemetry';
2
3
  export type {
3
4
  EnrichSpan,
@@ -50,6 +50,7 @@ import type {
50
50
  import { assembleOperationName } from './assemble-operation-name';
51
51
  import { getBaseTelemetryAttributes } from './get-base-telemetry-attributes';
52
52
  import { getProviderUsageAttributes } from './provider-usage-attributes';
53
+ import { redactToolDefinition } from './redact-tool-definition';
53
54
  import { sanitizeAttributeValue } from './sanitize-attribute-value';
54
55
  import {
55
56
  stringifyForTelemetry,
@@ -664,7 +665,10 @@ export class LegacyOpenTelemetry implements Telemetry {
664
665
  : undefined,
665
666
  },
666
667
  'ai.prompt.tools': {
667
- input: () => event.stepTools?.map(tool => JSON.stringify(tool)),
668
+ input: () =>
669
+ event.stepTools?.map(tool =>
670
+ JSON.stringify(redactToolDefinition(tool)),
671
+ ),
668
672
  },
669
673
  'ai.prompt.toolChoice': {
670
674
  input: () =>