File tree Expand file tree Collapse file tree
src/instrumentation/openai Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -186,15 +186,29 @@ abstract class BaseStreamHandler {
186186
187187 protected finalizeSpan ( code : SpanStatusCode ) : void {
188188 if ( code === SpanStatusCode . OK ) {
189- finalizeSpanSuccess (
189+ setResponseAttributes (
190190 this . span ,
191191 this . completeResponse as Record < string , unknown > ,
192192 ) ;
193- } else {
194- recordSpanTiming ( this . span , SpanAttributes . LLM_RESPONSE_DURATION ) ;
195- this . span . setStatus ( { code } ) ;
196- this . span . end ( ) ;
197193 }
194+ recordSpanTiming ( this . span , SpanAttributes . LLM_RESPONSE_DURATION ) ;
195+ // Only set TTFT at finalize when no content was detected during
196+ // streaming (degenerate case). When streaming produced content,
197+ // TTFT was already recorded in processChunk on the first chunk.
198+ if ( ! this . firstContentRecorded ) {
199+ recordSpanTiming (
200+ this . span ,
201+ SpanAttributes . LLM_PERFORMANCE_TIME_TO_FIRST_TOKEN ,
202+ { recordEventTimestamp : true } ,
203+ ) ;
204+ recordSpanTiming (
205+ this . span ,
206+ SpanAttributes . LLM_PERFORMANCE_RELATIVE_TIME_TO_FIRST_TOKEN ,
207+ { useRootSpan : true } ,
208+ ) ;
209+ }
210+ this . span . setStatus ( { code } ) ;
211+ this . span . end ( ) ;
198212 }
199213}
200214
You can’t perform that action at this time.
0 commit comments