import { SpanContext , TraceFlags } from '@opentelemetry/api' ; const VERSION_ID = 0 ; const TRACE_ID_FIELD_ID = 0 ; const SPAN_ID_FIELD_ID = 1 ; const TRACE_OPTION_FIELD_ID = 2 ; const ID_SIZE = 1 ; const TRACE_ID_SIZE = 16 ; const SPAN_ID_SIZE = 8 ; const TRACE_OPTION_SIZE = 1 ; const VERSION_ID_OFFSET = 0 ; const TRACE_ID_FIELD_ID_OFFSET = VERSION_ID_OFFSET + ID_SIZE ; const TRACE_ID_OFFSET = TRACE_ID_FIELD_ID_OFFSET + ID_SIZE ; const SPAN_ID_FIELD_ID_OFFSET = TRACE_ID_OFFSET + TRACE_ID_SIZE ; const SPAN_ID_OFFSET = SPAN_ID_FIELD_ID_OFFSET + ID_SIZE ; const TRACE_OPTION_FIELD_ID_OFFSET = SPAN_ID_OFFSET + SPAN_ID_SIZE ; const TRACE_OPTIONS_OFFSET = TRACE_OPTION_FIELD_ID_OFFSET + ID_SIZE ; const FORMAT_LENGTH = 4 * ID_SIZE + TRACE_ID_SIZE + SPAN_ID_SIZE + TRACE_OPTION_SIZE ; /**
 * Used to encode SpanContext to binary (toBytes) or decode SpanContext
 * from binary (fromBytes). Derived from a previous implementation in
 * opentelemetry-core (see README.md). Supports the main
 * GrpcCensusPropagator class.
 */ export class BinaryTraceContext { param spanContext returns static toBytes ( spanContext ) { /**
         *  0           1           2
         *  0 1 2345678901234567 8 90123456 7 8
         * -------------------------------------
         * | | |                | |        | | |
         * -------------------------------------
         *  ^ ^      ^           ^    ^     ^ ^
         *  | |      |           |    |     | `-- options value
         *  | |      |           |    |     `---- options field ID (2)
         *  | |      |           |    `---------- spanID value
         *  | |      |           `--------------- spanID field ID (1)
         *  | |      `--------------------------- traceID value
         *  | `---------------------------------- traceID field ID (0)
         *  `------------------------------------ version (0)
         */ const traceId = spanContext . traceId ; const spanId = spanContext . spanId ; const buf = new Uint8Array ( FORMAT_LENGTH ) ; let j = TRACE_ID_OFFSET ; for ( let i = TRACE_ID_OFFSET ; i < SPAN_ID_FIELD_ID_OFFSET ; i ++ ) { buf [ j ++ ] = parseInt ( traceId . substr ( ( i - TRACE_ID_OFFSET ) * 2 , 2 ) , 16 ) ; } buf [ j ++ ] = SPAN_ID_FIELD_ID ; for ( let i = SPAN_ID_OFFSET ; i < TRACE_OPTION_FIELD_ID_OFFSET ; i ++ ) { buf [ j ++ ] = parseInt ( spanId . substr ( ( i - SPAN_ID_OFFSET ) * 2 , 2 ) , 16 ) ; } buf [ j ++ ] = TRACE_OPTION_FIELD_ID ; buf [ j ++ ] = Number ( spanContext . traceFlags ) || TraceFlags . NONE ; return buf ; } param buf returns static fromBytes ( buf ) { const result = { traceId : '' , spanId : '' , traceFlags : TraceFlags . NONE , } ; if ( buf . length !== FORMAT_LENGTH ) return null ; if ( buf [ VERSION_ID_OFFSET ] !== VERSION_ID || buf [ TRACE_ID_FIELD_ID_OFFSET ] !== TRACE_ID_FIELD_ID || buf [ SPAN_ID_FIELD_ID_OFFSET ] !== SPAN_ID_FIELD_ID || buf [ TRACE_OPTION_FIELD_ID_OFFSET ] !== TRACE_OPTION_FIELD_ID ) { return null ; } result . isRemote = true ; result . traceId = toHex ( buf . slice ( TRACE_ID_OFFSET , SPAN_ID_FIELD_ID_OFFSET ) ) ; result . spanId = toHex ( buf . slice ( SPAN_ID_OFFSET , TRACE_OPTION_FIELD_ID_OFFSET ) ) ; result . traceFlags = buf [ TRACE_OPTIONS_OFFSET ] ; return result ; } } param buff returns function toHex ( buff ) { let out = '' ; for ( let i = 0 ; i < buff . length ; ++ i ) { const n = buff [ i ] ; if ( n < 16 ) { out += '0' + n . toString ( 16 ) ; } else { out += n . toString ( 16 ) ; } } return out ; }