import { logDebug } from "../utils" ; import { SampleMode , Source } from "./constants" ; import { TraceContext } from "./context" ; import { TraceHeaders } from "./trace-context-service" ; export interface SpanContext { toTraceId ( ) : string ; toSpanId ( ) : string ; } export interface TraceOptions { resource ? : string ; service ? : string ; type ? : string ; tags ? : { [ key ] : any ; } ; childOf ? : SpanContext ; } export class TracerWrapper { private tracer : any ; constructor ( ) { try { const path = require . resolve ( "dd-trace" , { paths : [ "/var/task/node_modules" , ... module . paths ] } ) ; this . tracer = require ( path ) ; return ; } catch ( err ) { if ( err instanceof Object || err instanceof Error ) { logDebug ( "Couldn't require dd-trace from main" , err ) ; } } } public get isTracerAvailable ( ) : boolean { return this . tracer !== undefined && this . tracer . _tracer !== undefined && "_service" in this . tracer . _tracer ; } public get currentSpan ( ) : any | null { if ( ! this . isTracerAvailable ) { return null ; } return this . tracer . scope ( ) . active ( ) ; } public extract ( event ) { if ( ! this . isTracerAvailable ) { return null ; } return this . tracer . extract ( "http_headers" , event ) as SpanContext | null ; } public wrap < T = ( ... args : any [ ] ) => any > ( name , options , fn ) { if ( ! this . isTracerAvailable ) { return fn ; } return this . tracer . wrap ( name , options , fn ) ; } public traceContext ( ) { if ( ! this . isTracerAvailable ) { return ; } const span = this . currentSpan ; if ( span === null ) { return ; } const parentID = span . context ( ) . toSpanId ( ) ; const traceID = span . context ( ) . toTraceId ( ) ; return { parentID , sampleMode : SampleMode . AUTO_KEEP , source : Source . Event , traceID , } ; } }