@@ -14,7 +14,7 @@ import {
1414 HUMANLOOP_FILE_KEY ,
1515 HUMANLOOP_FILE_TYPE_KEY ,
1616 HUMANLOOP_LOG_KEY ,
17- HUMANLOOP_WRAPPED_FUNCTION_NAME ,
17+ HUMANLOOP_META_FUNCTION_NAME ,
1818} from "./constants" ;
1919import {
2020 NestedDict ,
@@ -44,7 +44,7 @@ export class HumanloopSpanProcessor implements SpanProcessor {
4444
4545 async forceFlush ( ) : Promise < void > { }
4646
47- onStart ( span : Span , parentContext : Context ) : void {
47+ onStart ( span : Span , _ : Context ) : void {
4848 // Handle stream case: when Prompt instrumented function calls a provider with streaming: true
4949 // The instrumentor span will end only when the ChunksResponse is consumed, which can happen
5050 // after the span created by the Prompt utility finishes. To handle this, we register all instrumentor
@@ -61,22 +61,23 @@ export class HumanloopSpanProcessor implements SpanProcessor {
6161 async shutdown ( ) : Promise < void > { }
6262
6363 /**
64- * Handles spans at the end of their lifecycle.
65- * Enriches Humanloop spans and send both HL and
64+ * Handles spans at the end of their lifecycle. Enriches Humanloop spans and send both HL and
6665 * non-HL spans to the exporter.
6766 */
6867 onEnd ( span : ReadableSpan ) : void {
6968 if ( isHumanloopSpan ( span ) ) {
7069 new Promise < void > ( ( resolve ) => {
71- while ( true ) {
70+ const checkChildrenSpans = ( ) => {
7271 const childrenSpans = this . children . get ( span . spanContext ( ) . spanId ) ;
7372 if (
74- ( childrenSpans || [ ] ) ? .every ( ( childSpan ) => childSpan . complete )
73+ ( childrenSpans || [ ] ) . every ( ( childSpan ) => childSpan . complete )
7574 ) {
76- break ;
75+ resolve ( ) ;
76+ } else {
77+ setTimeout ( checkChildrenSpans , 100 ) ;
7778 }
78- }
79- resolve ( ) ;
79+ } ;
80+ checkChildrenSpans ( ) ;
8081 } ) . then ( ( _ ) => {
8182 // All children/ instrumentor spans have arrived, we can process the
8283 // Humanloop parent span owning them
@@ -126,13 +127,21 @@ export class HumanloopSpanProcessor implements SpanProcessor {
126127 : childSpan ,
127128 ) ,
128129 ) ;
129- }
130130
131- this . spanExporter . export ( [ span ] , ( result : ExportResult ) => {
132- if ( result . code !== ExportResultCode . SUCCESS ) {
133- console . error ( "Failed to export span:" , result . error ) ;
134- }
135- } ) ;
131+ // Export the instrumentor span
132+ this . spanExporter . export ( [ span ] , ( result : ExportResult ) => {
133+ if ( result . code !== ExportResultCode . SUCCESS ) {
134+ console . error ( "Failed to export span:" , result . error ) ;
135+ }
136+ } ) ;
137+ } else {
138+ // Unknown span, export as it is
139+ this . spanExporter . export ( [ span ] , ( result : ExportResult ) => {
140+ if ( result . code !== ExportResultCode . SUCCESS ) {
141+ console . error ( "Failed to export span:" , result . error ) ;
142+ }
143+ } ) ;
144+ }
136145 }
137146
138147 /**
@@ -190,7 +199,7 @@ export class HumanloopSpanProcessor implements SpanProcessor {
190199 const prompt = ( hlFile . prompt || { } ) as unknown as PromptKernelRequest ;
191200 if ( ! ( "model" in prompt ) || ! prompt . model ) {
192201 const functionName =
193- promptSpan . attributes [ HUMANLOOP_WRAPPED_FUNCTION_NAME ] ;
202+ promptSpan . attributes [ HUMANLOOP_META_FUNCTION_NAME ] ;
194203 throw Error (
195204 `Error in ${ functionName } : the LLM provider and model could not be inferred. Call one of the supported providers in your prompt function definition or define them in the promptKernel argument of the prompt() function wrapper.` ,
196205 ) ;
0 commit comments