@@ -4,6 +4,7 @@ import * as Model from 'effect/ai/Model'
44import * as Prompt from 'effect/ai/Prompt'
55import * as Response from 'effect/ai/Response'
66import * as ResponseIdTracker from 'effect/ai/ResponseIdTracker'
7+ import * as Telemetry from 'effect/ai/Telemetry'
78import * as Tool from 'effect/ai/Tool'
89import * as Toolkit from 'effect/ai/Toolkit'
910import * as Arr from 'effect/Array'
@@ -31,8 +32,9 @@ import * as SchemaAST from 'effect/SchemaAST'
3132import * as Scope from 'effect/Scope'
3233import * as Semaphore from 'effect/Semaphore'
3334import * as Stream from 'effect/Stream'
35+ import * as EffectString from 'effect/String'
3436import type * as Take from 'effect/Take'
35- import * as Tracer from 'effect/Tracer'
37+ import type * as Tracer from 'effect/Tracer'
3638
3739import type * as Agent from '../../core/Agent'
3840import {
@@ -1975,84 +1977,85 @@ interface ToolTelemetryDescriptor {
19751977 readonly sequenceIndex ?: number | undefined
19761978}
19771979
1980+ type TelemetryAttributeValue = string | number | boolean
1981+
1982+ type TelemetryAttributes = Readonly < Record < string , TelemetryAttributeValue > >
1983+
1984+ const addGenAiSpanAttributes = Telemetry . addSpanAttributes ( 'gen_ai' , EffectString . camelToSnake )
1985+ const addEffectAgentSpanAttributes = Telemetry . addSpanAttributes ( 'effect_agent' , EffectString . camelToSnake )
1986+
1987+ const writeTelemetryAttributes = ( span : Tracer . Span , attributes : TelemetryAttributes ) : void => {
1988+ for ( const prefix of [ 'gen_ai' , 'effect_agent' ] as const ) {
1989+ const head = `${ prefix } .`
1990+ const projected : Record < string , TelemetryAttributeValue > = { }
1991+ for ( const [ name , value ] of Object . entries ( attributes ) ) {
1992+ if ( name . startsWith ( head ) ) projected [ name . slice ( head . length ) ] = value
1993+ }
1994+ if ( prefix === 'gen_ai' ) addGenAiSpanAttributes ( span , projected )
1995+ else addEffectAgentSpanAttributes ( span , projected )
1996+ }
1997+ }
1998+
19781999/** A definition names the agent; its Thread identifies the continuing instance and conversation. */
1979- const agentTelemetryAttributes = ( context : RunContext ) => ( {
2000+ const agentTelemetryAttributes = ( context : RunContext , turnId ?: TurnId ) : TelemetryAttributes => ( {
19802001 'gen_ai.agent.name' : context . agentId ,
19812002 'gen_ai.agent.id' : context . threadId ,
19822003 'gen_ai.conversation.id' : context . threadId ,
2004+ 'effect_agent.run.id' : context . runId ,
2005+ 'effect_agent.thread.id' : context . threadId ,
2006+ ...( turnId === undefined ? { } : { 'effect_agent.turn.id' : turnId } ) ,
19832007} )
19842008
1985- /** Label Effect AI's existing span, preserving provider usage, sampling and host async context. */
1986- const modelTelemetryTracer = Effect . fnUntraced ( function * (
2009+ const modelSpanTransformer = (
19872010 context : RunContext ,
1988- turnId ?: TurnId ,
1989- onSpan ?: ( span : Tracer . Span ) => void ,
1990- ) {
1991- const delegate = yield * Tracer . Tracer
1992- const model = yield * Model . ModelName
1993- const provider = yield * Model . ProviderName
1994-
1995- return Tracer . make ( {
1996- span ( options ) {
1997- if ( options . name !== 'LanguageModel.streamText' ) return delegate . span ( options )
1998-
1999- const span = delegate . span ( { ...options , name : `chat ${ model } ` } )
2000-
2001- onSpan ?.( span )
2002-
2003- const attributes = {
2004- ...agentTelemetryAttributes ( context ) ,
2005- 'gen_ai.operation.name' : 'chat' ,
2006- 'gen_ai.request.model' : model ,
2007- 'gen_ai.provider.name' : provider ,
2008- agentId : context . agentId ,
2009- threadId : context . threadId ,
2010- runId : context . runId ,
2011- ...( turnId === undefined ? { } : { turnId } ) ,
2012- }
2013-
2014- for ( const [ key , value ] of Object . entries ( attributes ) ) span . attribute ( key , value )
2015-
2016- return span
2017- } ,
2018- ...( delegate . context === undefined ? { } : { context : delegate . context . bind ( delegate ) } ) ,
2011+ turnId : TurnId | undefined ,
2012+ onFinish ?: ( span : Tracer . Span ) => void ,
2013+ ) : Effect . Effect < Telemetry . SpanTransformer , never , Model . ModelName | Model . ProviderName > =>
2014+ Effect . gen ( function * ( ) {
2015+ const host = yield * Effect . serviceOption ( Telemetry . CurrentSpanTransformer )
2016+ const model = yield * Model . ModelName
2017+ const provider = yield * Model . ProviderName
2018+
2019+ return ( options ) => {
2020+ if ( Option . isSome ( host ) ) host . value ( options )
2021+ Telemetry . addGenAIAnnotations ( options . span , {
2022+ operation : { name : 'chat' } ,
2023+ request : { model } ,
2024+ provider : { name : provider } ,
2025+ } )
2026+ writeTelemetryAttributes ( options . span , agentTelemetryAttributes ( context , turnId ) )
2027+ onFinish ?.( options . span )
2028+ }
20192029 } )
2020- } )
20212030
20222031/** One bounded identity surface shared by canonical Tool spans and terminal logs. */
2023- const toolTelemetryAttributes = ( descriptor : ToolTelemetryDescriptor ) => ( {
2024- ...agentTelemetryAttributes ( descriptor . context ) ,
2032+ const toolTelemetryAttributes = ( descriptor : ToolTelemetryDescriptor ) : TelemetryAttributes => ( {
2033+ ...agentTelemetryAttributes ( descriptor . context , descriptor . turnId ) ,
20252034 'gen_ai.operation.name' : 'execute_tool' ,
20262035 'gen_ai.tool.name' : descriptor . toolName ,
20272036 'gen_ai.tool.type' : 'function' ,
2028- ...( descriptor . toolCallId === undefined
2029- ? { }
2030- : {
2031- 'gen_ai.tool.call.id' : descriptor . toolCallId ,
2032- toolCallId : descriptor . toolCallId ,
2033- } ) ,
2037+ ...( descriptor . toolCallId === undefined ? { } : { 'gen_ai.tool.call.id' : descriptor . toolCallId } ) ,
20342038 'effect_agent.tool.execution_class' : descriptor . executionClass ,
20352039 'effect_agent.tool.invocation_kind' : descriptor . invocationKind ,
20362040 'effect_agent.tool.failure_mode' : descriptor . failureMode ,
20372041 ...( descriptor . parentToolCallId === undefined
20382042 ? { }
2039- : {
2040- 'effect_agent.tool.parent_call.id' : descriptor . parentToolCallId ,
2041- parentToolCallId : descriptor . parentToolCallId ,
2042- } ) ,
2043+ : { 'effect_agent.tool.parent_call.id' : descriptor . parentToolCallId } ) ,
20432044 ...( descriptor . sequenceIndex === undefined
20442045 ? { }
2045- : {
2046- 'effect_agent.tool.sequence_index' : descriptor . sequenceIndex ,
2047- sequenceIndex : descriptor . sequenceIndex ,
2048- } ) ,
2049- agentId : descriptor . context . agentId ,
2050- threadId : descriptor . context . threadId ,
2051- runId : descriptor . context . runId ,
2052- turnId : descriptor . turnId ,
2053- toolName : descriptor . toolName ,
2046+ : { 'effect_agent.tool.sequence_index' : descriptor . sequenceIndex } ) ,
20542047} )
20552048
2049+ const annotateToolSpan = ( attributes : TelemetryAttributes ) : Effect . Effect < void > =>
2050+ Effect . currentSpan . pipe (
2051+ Effect . catchNoSuchElement ,
2052+ Effect . flatMap ( ( span ) =>
2053+ Option . isSome ( span )
2054+ ? Effect . sync ( ( ) => writeTelemetryAttributes ( span . value , attributes ) )
2055+ : Effect . void
2056+ ) ,
2057+ )
2058+
20562059/** Canonical content-free terminal signal for any application Tool handler attempt. */
20572060const terminalToolTelemetry = (
20582061 descriptor : ToolTelemetryDescriptor ,
@@ -2076,8 +2079,6 @@ const terminalToolTelemetry = (
20762079 ...( failureHandling === undefined
20772080 ? { }
20782081 : { 'effect_agent.tool.failure_handling' : failureHandling } ) ,
2079- toolExecutionClass : descriptor . executionClass ,
2080- toolOutcome : outcome ,
20812082 } ) ,
20822083 ) ,
20832084 ) ,
@@ -2369,7 +2370,8 @@ const executePreparedToolCall = Effect.fnUntraced(function*<
23692370 return yield * failureMarker ( )
23702371 } )
23712372
2372- const measured = started . pipe (
2373+ const measured = annotateToolSpan ( toolTelemetryAttributes ( telemetryDescriptor ) ) . pipe (
2374+ Effect . andThen ( started ) ,
23732375 Effect . andThen ( results ) ,
23742376 Effect . andThen ( commitTerminalResult ) ,
23752377 Effect . catchCause ( ( cause ) => {
@@ -2399,10 +2401,7 @@ const executePreparedToolCall = Effect.fnUntraced(function*<
23992401
24002402 return failTerminalResult ( toolCause )
24012403 } ) ,
2402- Effect . withSpan ( `execute_tool ${ call . name } ` , {
2403- kind : 'internal' ,
2404- attributes : toolTelemetryAttributes ( telemetryDescriptor ) ,
2405- } ) ,
2404+ Effect . withSpan ( `execute_tool ${ call . name } ` , { kind : 'internal' } ) ,
24062405 )
24072406
24082407 const telemetry = yield * ToolSpanTelemetry
@@ -4362,7 +4361,10 @@ const compactContext = <AgentValue extends Agent.Any, HookError, HookRequirement
43624361 context . durationDeadlineMillis ,
43634362 context . durationFailure ,
43644363 ) . pipe (
4365- Stream . provideServiceEffect ( Tracer . Tracer , modelTelemetryTracer ( context ) ) ,
4364+ Stream . provideServiceEffect (
4365+ Telemetry . CurrentSpanTransformer ,
4366+ modelSpanTransformer ( context , undefined ) ,
4367+ ) ,
43664368 Stream . runForEach ( ( part ) =>
43674369 ownModelResponsePart (
43684370 part ,
@@ -5842,7 +5844,6 @@ const makeTurn = <
58425844
58435845 let restartRequested = false
58445846 let responseStarted = false
5845- let activeModelSpan : Tracer . Span | undefined
58465847 let activeProvisionalText : ProvisionalTextModelHandle | undefined
58475848
58485849 const trace : TurnTrace = {
@@ -6613,9 +6614,12 @@ const makeTurn = <
66136614 )
66146615 : stream ,
66156616 Stream . provideServiceEffect (
6616- Tracer . Tracer ,
6617- modelTelemetryTracer ( context , turnId , ( span ) => {
6618- activeModelSpan = span
6617+ Telemetry . CurrentSpanTransformer ,
6618+ modelSpanTransformer ( context , turnId , ( span ) => {
6619+ if ( restartRequested ) {
6620+ span . attribute ( 'effect_agent.model.outcome' , 'aborted' )
6621+ span . attribute ( 'effect_agent.model.abort_reason' , 'joined-input' )
6622+ }
66196623 } ) ,
66206624 ) ,
66216625 Stream . onStart (
@@ -7529,8 +7533,6 @@ const makeTurn = <
75297533 Effect . sync ( ( ) => {
75307534 restartRequested = true
75317535 activeProvisionalText ?. discard ( )
7532- activeModelSpan ?. attribute ( 'effect_agent.model.outcome' , 'aborted' )
7533- activeModelSpan ?. attribute ( 'effect_agent.model.abort_reason' , 'joined-input' )
75347536 } )
75357537 ) ,
75367538 Effect . raceFirst (
@@ -9267,9 +9269,6 @@ function executeWithCompletion<
92679269 attributes : {
92689270 ...agentTelemetryAttributes ( context ) ,
92699271 'gen_ai.operation.name' : 'invoke_agent' ,
9270- agentId : context . agentId ,
9271- threadId : context . threadId ,
9272- runId : context . runId ,
92739272 } ,
92749273 } ) ,
92759274 Effect . provide ( engineToolServices ) ,
@@ -9864,7 +9863,7 @@ const measureProgrammaticToolCall = <R>(
98649863 let terminalResult : ProgrammaticCallOutcome | undefined
98659864 let propagatedFailure : Cause . Cause < never > | undefined
98669865
9867- const measured : Effect . Effect < ProgrammaticCallOutcome , ToolSpanFailure , R > = Effect . exit (
9866+ const body : Effect . Effect < ProgrammaticCallOutcome , ToolSpanFailure , R > = Effect . exit (
98689867 effect ,
98699868 ) . pipe (
98709869 Effect . flatMap ( ( exit ) : Effect . Effect < ProgrammaticCallOutcome , ToolSpanFailure > => {
@@ -9896,10 +9895,13 @@ const measureProgrammaticToolCall = <R>(
98969895 ) ,
98979896 )
98989897 } ) ,
9899- Effect . withSpan ( `execute_tool ${ descriptor . toolName } ` , {
9900- kind : 'internal' ,
9901- attributes : toolTelemetryAttributes ( descriptor ) ,
9902- } ) ,
9898+ )
9899+
9900+ const measured : Effect . Effect < ProgrammaticCallOutcome , ToolSpanFailure , R > = annotateToolSpan (
9901+ toolTelemetryAttributes ( descriptor ) ,
9902+ ) . pipe (
9903+ Effect . andThen ( body ) ,
9904+ Effect . withSpan ( `execute_tool ${ descriptor . toolName } ` , { kind : 'internal' } ) ,
99039905 )
99049906
99059907 return telemetry . isolateEffectSpanLifecycle ( measured ) . pipe (
0 commit comments