@ai-sdk/otel 1.0.147 → 1.0.149

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.147",
3
+ "version": "1.0.149",
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.31",
29
+ "@ai-sdk/provider": "4.0.32",
30
30
  "@opentelemetry/api": "1.9.1",
31
- "ai": "7.0.147"
31
+ "ai": "7.0.149"
32
32
  },
33
33
  "devDependencies": {
34
- "@ai-sdk/provider-utils": "5.0.64",
34
+ "@ai-sdk/provider-utils": "5.0.66",
35
35
  "@opentelemetry/sdk-trace-base": "2.7.1",
36
36
  "@types/node": "22.19.19",
37
37
  "@vercel/ai-tsconfig": "0.0.0",
@@ -125,11 +125,13 @@ export class OpenTelemetry implements Telemetry {
125
125
  private readonly callStates = new Map<string, CallState>();
126
126
 
127
127
  private readonly tracer: Tracer;
128
+ private readonly agentSpanName: OpenTelemetryOptions['agentSpanName'];
128
129
  private readonly supplementalAttributes: SupplementalAttributeOptions;
129
130
  private readonly enrichSpan: EnrichSpan | undefined;
130
131
 
131
132
  constructor(options: OpenTelemetryOptions = {}) {
132
133
  this.tracer = options.tracer ?? trace.getTracer('gen_ai');
134
+ this.agentSpanName = options.agentSpanName;
133
135
  this.supplementalAttributes = normalizeSupplementalAttributes(options);
134
136
  this.enrichSpan = options.enrichSpan;
135
137
  }
@@ -142,6 +144,37 @@ export class OpenTelemetry implements Telemetry {
142
144
  this.callStates.delete(callId);
143
145
  }
144
146
 
147
+ private getAgentSpanName({
148
+ operationName,
149
+ modelId,
150
+ functionId,
151
+ }: {
152
+ operationName: string;
153
+ modelId: string;
154
+ functionId: string | undefined;
155
+ }): string {
156
+ const defaultName = `${operationName} ${modelId}`;
157
+
158
+ // Other operations, such as harness turns, share the generation handler.
159
+ if (operationName !== 'invoke_agent') {
160
+ return defaultName;
161
+ }
162
+
163
+ if (this.agentSpanName === 'functionId') {
164
+ return `invoke_agent ${functionId ?? modelId}`;
165
+ }
166
+
167
+ if (typeof this.agentSpanName === 'function') {
168
+ try {
169
+ return this.agentSpanName({ modelId, functionId }) ?? defaultName;
170
+ } catch {
171
+ return defaultName;
172
+ }
173
+ }
174
+
175
+ return defaultName;
176
+ }
177
+
145
178
  private getSpanAttributes({
146
179
  attributes,
147
180
  spanType,
@@ -429,7 +462,11 @@ export class OpenTelemetry implements Telemetry {
429
462
  ...baseSupplementalAttributes,
430
463
  });
431
464
 
432
- const spanName = `${operationName} ${event.modelId}`;
465
+ const spanName = this.getAgentSpanName({
466
+ operationName,
467
+ modelId: event.modelId,
468
+ functionId: telemetry.functionId,
469
+ });
433
470
  const rootSpan = this.tracer.startSpan(spanName, {
434
471
  attributes: this.getSpanAttributes({
435
472
  attributes,
@@ -536,7 +573,11 @@ export class OpenTelemetry implements Telemetry {
536
573
  }),
537
574
  });
538
575
 
539
- const spanName = `${operationName} ${event.modelId}`;
576
+ const spanName = this.getAgentSpanName({
577
+ operationName,
578
+ modelId: event.modelId,
579
+ functionId: telemetry.functionId,
580
+ });
540
581
  const rootSpan = this.tracer.startSpan(spanName, {
541
582
  attributes: this.getSpanAttributes({
542
583
  attributes,
@@ -44,6 +44,24 @@ export type OpenTelemetryOptions = {
44
44
  */
45
45
  tracer?: Tracer;
46
46
 
47
+ /**
48
+ * Controls root agent span names for text and object generation.
49
+ *
50
+ * With `modelId` (the default), names are `invoke_agent {modelId}`.
51
+ * With `functionId`, names are `invoke_agent {functionId}`, falling back to
52
+ * the model ID when no function ID is provided.
53
+ *
54
+ * A callback returns the complete span name. If it returns `undefined` or
55
+ * throws, the default model-based name is used.
56
+ */
57
+ agentSpanName?:
58
+ | 'modelId'
59
+ | 'functionId'
60
+ | ((options: {
61
+ modelId: string;
62
+ functionId: string | undefined;
63
+ }) => string | undefined);
64
+
47
65
  /**
48
66
  * Adds custom attributes to spans when they are created. These attributes are
49
67
  * not AI SDK-owned semantics and are intended for observability integrations.