1- import {
2- context ,
3- SpanKind as OpenTelemetrySpanKind ,
4- trace ,
5- } from "@opentelemetry/api" ;
1+ import { Tracer as OpenTelemetryTracer , trace } from "@opentelemetry/api" ;
62import { JUDGMENT_API_URL , JUDGMENT_DEFAULT_GPT_MODEL } from "../env" ;
73import { JudgmentApiClient } from "../internal/api" ;
84import {
@@ -28,7 +24,7 @@ export abstract class Tracer {
2824 protected static instances : Map < string , Tracer > = new Map ( ) ;
2925
3026 public apiClient : JudgmentApiClient ;
31- public tracer : unknown = null ;
27+ public tracer : OpenTelemetryTracer ;
3228 public serializer : Serializer = JSON . stringify ;
3329
3430 protected _initialized : boolean = false ;
@@ -50,6 +46,7 @@ export abstract class Tracer {
5046
5147 public constructor ( configuration : TracerConfiguration ) {
5248 this . configuration = configuration ;
49+ this . tracer = trace . getTracer ( this . configuration . tracerName ) ;
5350
5451 this . apiClient = new JudgmentApiClient (
5552 this . configuration . apiUrl ,
@@ -355,71 +352,58 @@ export abstract class Tracer {
355352 func : ( ...args : TArgs ) => TResult ,
356353 spanKind : SpanKind = "span" ,
357354 ) : ( ...args : TArgs ) => TResult {
358- return ( ...args : TArgs ) => {
359- const currentSpan = trace . getActiveSpan ( ) ;
360- if ( ! currentSpan ) {
361- Logger . warn ( "No active span found, skipping observe" ) ;
362- return func ( ...args ) ;
363- }
364-
355+ return ( ...args : TArgs ) : TResult => {
365356 const spanName = func . name || "anonymous" ;
366- const tracer = trace . getTracer ( this . configuration . tracerName ) ;
367-
368- return context . with ( trace . setSpan ( context . active ( ) , currentSpan ) , ( ) => {
369- return tracer . startActiveSpan (
370- spanName ,
371- { kind : OpenTelemetrySpanKind . INTERNAL } ,
372- ( span ) => {
373- try {
374- span . setAttribute (
375- OpenTelemetryKeys . AttributeKeys . JUDGMENT_SPAN_KIND ,
376- spanKind ,
377- ) ;
378-
379- const argNames = parseFunctionArgs ( func ) ;
380- if ( argNames . length === args . length ) {
381- const inputObj : Record < string , unknown > = { } ;
382- argNames . forEach ( ( name , index ) => {
383- inputObj [ name ] = args [ index ] ;
384- } ) ;
385- span . setAttribute (
386- OpenTelemetryKeys . AttributeKeys . JUDGMENT_INPUT ,
387- this . serializer ( inputObj ) ,
388- ) ;
389- }
390-
391- const result = func ( ...args ) ;
392-
393- if ( result instanceof Promise ) {
394- return result
395- . then ( ( res ) => {
396- span . setAttribute (
397- OpenTelemetryKeys . AttributeKeys . JUDGMENT_OUTPUT ,
398- this . serializer ( res ) ,
399- ) ;
400- span . end ( ) ;
401- return res ;
402- } )
403- . catch ( ( err ) => {
404- span . recordException ( err as Error ) ;
405- span . end ( ) ;
406- throw err ;
407- } ) as TResult ;
408- } else {
357+
358+ return this . tracer . startActiveSpan ( spanName , ( span ) => {
359+ try {
360+ span . setAttribute (
361+ OpenTelemetryKeys . AttributeKeys . JUDGMENT_SPAN_KIND ,
362+ spanKind ,
363+ ) ;
364+
365+ const argNames = parseFunctionArgs ( func ) ;
366+ if ( argNames . length === args . length ) {
367+ const inputObj : Record < string , unknown > = { } ;
368+ argNames . forEach ( ( name , index ) => {
369+ inputObj [ name ] = args [ index ] ;
370+ } ) ;
371+ span . setAttribute (
372+ OpenTelemetryKeys . AttributeKeys . JUDGMENT_INPUT ,
373+ this . serializer ( inputObj ) ,
374+ ) ;
375+ }
376+
377+ const result = func ( ...args ) ;
378+
379+ if ( result instanceof Promise ) {
380+ return result
381+ . then ( ( res ) => {
409382 span . setAttribute (
410383 OpenTelemetryKeys . AttributeKeys . JUDGMENT_OUTPUT ,
411- this . serializer ( result ) ,
384+ this . serializer ( res ) ,
412385 ) ;
413386 span . end ( ) ;
414- return result ;
415- }
416- } catch ( err ) {
417- span . recordException ( err as Error ) ;
418- span . end ( ) ;
419- throw err ;
420- }
421- } ,
422- ) ;
387+ return res ;
388+ } )
389+ . catch ( ( err ) => {
390+ span . recordException ( err as Error ) ;
391+ span . end ( ) ;
392+ throw err ;
393+ } ) as TResult ;
394+ } else {
395+ span . setAttribute (
396+ OpenTelemetryKeys . AttributeKeys . JUDGMENT_OUTPUT ,
397+ this . serializer ( result ) ,
398+ ) ;
399+ span . end ( ) ;
400+ return result ;
401+ }
402+ } catch ( err ) {
403+ span . recordException ( err as Error ) ;
404+ span . end ( ) ;
405+ throw err ;
406+ }
423407 } ) ;
424408 } ;
425409 }
0 commit comments