Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clean-deers-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ai': patch
---

feat(ai): rootSpan flag for telemetry options
1 change: 1 addition & 0 deletions packages/ai/src/embed/embed-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Only applicable for HTTP-based providers.
},
}),
tracer,
root: telemetry?.rootSpan,
fn: async span => {
const [maxEmbeddingsPerCall, supportsParallelCalls] = await Promise.all([
model.maxEmbeddingsPerCall,
Expand Down
1 change: 1 addition & 0 deletions packages/ai/src/embed/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Only applicable for HTTP-based providers.
},
}),
tracer,
root: telemetry?.rootSpan,
fn: async span => {
const { embedding, usage, response, providerMetadata } = await retry(() =>
// nested spans to align with the embedMany telemetry data:
Expand Down
1 change: 1 addition & 0 deletions packages/ai/src/generate-object/generate-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ Default and recommended: 'auto' (best mode for the model).
},
}),
tracer,
root: telemetry?.rootSpan,
fn: async span => {
let result: string;
let finishReason: FinishReason;
Expand Down
1 change: 1 addition & 0 deletions packages/ai/src/generate-object/stream-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ class DefaultStreamObjectResult<PARTIAL, RESULT, ELEMENT_STREAM>
},
}),
tracer,
root: telemetry?.rootSpan,
endWhenDone: false,
fn: async rootSpan => {
const standardizedPrompt = await standardizePrompt({
Expand Down
1 change: 1 addition & 0 deletions packages/ai/src/generate-text/generate-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ A function that attempts to repair a tool call that failed to parse.
},
}),
tracer,
root: telemetry?.rootSpan,
fn: async span => {
const initialMessages = initialPrompt.messages;
const responseMessages: Array<ResponseMessage> = [];
Expand Down
1 change: 1 addition & 0 deletions packages/ai/src/generate-text/stream-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,7 @@ class DefaultStreamTextResult<TOOLS extends ToolSet, OUTPUT, PARTIAL_OUTPUT>
},
}),
tracer,
root: telemetry?.rootSpan,
endWhenDone: false,
fn: async rootSpanArg => {
rootSpan = rootSpanArg;
Expand Down
4 changes: 3 additions & 1 deletion packages/ai/src/telemetry/record-span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { Attributes, Span, Tracer, SpanStatusCode } from '@opentelemetry/api';
export function recordSpan<T>({
name,
tracer,
root,
attributes,
fn,
endWhenDone = true,
}: {
name: string;
tracer: Tracer;
root?: boolean;
attributes: Attributes;
fn: (span: Span) => Promise<T>;
endWhenDone?: boolean;
}) {
return tracer.startActiveSpan(name, { attributes }, async span => {
return tracer.startActiveSpan(name, { attributes, root }, async span => {
try {
const result = await fn(span);

Expand Down
5 changes: 5 additions & 0 deletions packages/ai/src/telemetry/telemetry-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ export type TelemetrySettings = {
* A custom tracer to use for the telemetry data.
*/
tracer?: Tracer;

/**
* Whether to create a root span for the operation.
*/
rootSpan?: boolean;
};