/**
 * Copyright 2018, OpenCensus Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ import { Annotation , Attributes , Link , LinkType , MessageEvent , MessageEventType , Span , SpanKind , } from '@opencensus/core' ; import { google , opencensus } from './types' ; param value returns const stringToTruncatableString = ( value ) : opencensus . proto . trace . v1 . TruncatableString => { return { value , truncatedByteCount : null } ; } ; param millis returns const millisToTimestamp = ( millis ) : google . protobuf . Timestamp => { let milliseconds = 0 ; if ( typeof millis === 'number' ) { milliseconds = millis ; } else { milliseconds = millis . getTime ( ) ; } const seconds = Math . floor ( milliseconds / 1000 ) ; const nanos = ( milliseconds % 1000 ) * 1000000 ; return { seconds , nanos } ; } ; param hex returns const hexStringToUint8Array = ( hex ) : Uint8Array | null => { if ( ! hex ) return null ; const match = hex . match ( /.{1,2}/g ) ; if ( ! match ) return null ; return new Uint8Array ( match . map ( byte => parseInt ( byte , 16 ) ) ) ; } ; /**
 * Adapts a string spanKind value to a `opencensus.proto.trace.v1.Span.SpanKind`
 * enum type.
 */ const spanKindToEnum = ( kind ) : opencensus . proto . trace . v1 . Span . SpanKind => { switch ( kind ) { case SpanKind . SERVER : { return opencensus . proto . trace . v1 . Span . SpanKind . SERVER ; } case SpanKind . CLIENT : { return opencensus . proto . trace . v1 . Span . SpanKind . CLIENT ; } default : { return opencensus . proto . trace . v1 . Span . SpanKind . SPAN_KIND_UNSPECIFIED ; } } } ; param attributes returns const adaptAttributes = ( attributes , droppedAttributesCount ) : opencensus . proto . trace . v1 . Span . Attributes | null => { if ( ! attributes ) { return null ; } const attributeMap = { } ; Object . getOwnPropertyNames ( attributes ) . forEach ( name => { const value = attributes [ name ] ; let stringValue = null ; let intValue = null ; let boolValue = null ; switch ( typeof value ) { case 'number' : { intValue = value as number ; break ; } case 'boolean' : { boolValue = value as boolean ; break ; } case 'string' : { stringValue = { value : value as string , truncatedByteCount : null } ; break ; } default : { } } attributeMap [ name ] = { stringValue , intValue , boolValue } ; } ) ; return { attributeMap , droppedAttributesCount } ; } ; param value return const adaptMessageEventType = ( type ) : opencensus . proto . trace . v1 . Span . TimeEvent . MessageEvent . Type => { switch ( type ) { case MessageEventType . SENT : { return opencensus . proto . trace . v1 . Span . TimeEvent . MessageEvent . Type . SENT ; } case MessageEventType . RECEIVED : { return opencensus . proto . trace . v1 . Span . TimeEvent . MessageEvent . Type . RECEIVED ; } default : { return opencensus . proto . trace . v1 . Span . TimeEvent . MessageEvent . Type . TYPE_UNSPECIFIED ; } } } ; param annotations param messageEvents returns const adaptTimeEvents = ( annotations , messageEvents , droppedAnnotationsCount , droppedMessageEventsCount ) : opencensus . proto . trace . v1 . Span . TimeEvents => { const timeEvents = [ ] ; if ( annotations ) { annotations . forEach ( annotation => { timeEvents . push ( { time : null , annotation : { description : stringToTruncatableString ( annotation . description ) , attributes : adaptAttributes ( annotation . attributes , 0 ) , } , } ) ; } ) ; } if ( messageEvents ) { messageEvents . forEach ( messageEvent => { timeEvents . push ( { time : millisToTimestamp ( messageEvent . timestamp ) , messageEvent : { id : messageEvent . id , type : adaptMessageEventType ( messageEvent . type ) , uncompressedSize : messageEvent . uncompressedSize || 0 , compressedSize : messageEvent . compressedSize || 0 , } , } ) ; } ) ; } return { timeEvent : timeEvents , droppedAnnotationsCount , droppedMessageEventsCount , } ; } ; param traceState returns const adaptTraceState = ( traceState ? ) : opencensus . proto . trace . v1 . Span . Tracestate => { const entries = ! traceState ? null : traceState . split ( ',' ) . map ( state => { const [ key , value ] = state . split ( '=' ) ; return { key , value } ; } ) ; return { entries } ; } ; param link returns const adaptLink = ( link ) : opencensus . proto . trace . v1 . Span . Link => { const traceId = hexStringToUint8Array ( link . traceId ) ; const spanId = hexStringToUint8Array ( link . spanId ) ; let type ; switch ( link . type ) { case LinkType . CHILD_LINKED_SPAN : { type = opencensus . proto . trace . v1 . Span . Link . Type . CHILD_LINKED_SPAN ; break ; } case LinkType . PARENT_LINKED_SPAN : { type = opencensus . proto . trace . v1 . Span . Link . Type . PARENT_LINKED_SPAN ; break ; } default : { type = opencensus . proto . trace . v1 . Span . Link . Type . TYPE_UNSPECIFIED ; } } const attributes = adaptAttributes ( link . attributes , 0 ) ; return { traceId , spanId , type , attributes } ; } ; param links returns const adaptLinks = ( links = [ ] , droppedLinksCount ) : opencensus . proto . trace . v1 . Span . Links => { return { link : links . map ( adaptLink ) , droppedLinksCount } ; } ; param value returns const adaptBoolean = ( value ) : google . protobuf . BoolValue => ( { value } ) ; param span returns export const adaptSpan = ( span ) : opencensus . proto . trace . v1 . Span => { return { traceId : hexStringToUint8Array ( span . traceId ) , spanId : hexStringToUint8Array ( span . id ) , tracestate : adaptTraceState ( span . traceState ) , parentSpanId : hexStringToUint8Array ( span . parentSpanId ) , name : stringToTruncatableString ( span . name ) , kind : spanKindToEnum ( span . kind ) , startTime : millisToTimestamp ( span . startTime ) , endTime : millisToTimestamp ( span . endTime ) , attributes : adaptAttributes ( span . attributes , span . droppedAttributesCount ) , stackTrace : null , timeEvents : adaptTimeEvents ( span . annotations , span . messageEvents , span . droppedAnnotationsCount , span . droppedMessageEventsCount ) , links : adaptLinks ( span . links , span . droppedLinksCount ) , status : span . status , sameProcessAsParentSpan : adaptBoolean ( ! span . remoteParent ) , childSpanCount : null , } ; } ; param span returns export const adaptRootSpan = ( span ) : opencensus . proto . trace . v1 . Span [ ] => { return [ adaptSpan ( span ) , ... span . spans . map ( adaptSpan ) ] ; } ; /**
 * Options for creating a `opencensus.proto.agent.common.v1.Node` type.
 */ export interface CreateNodeOptions { serviceName : string ; exporterVersion : string ; coreVersion : string ; hostName : string ; processStartTimeMillis : number ; attributes : Record < string , string > | undefined ; } param options returns export const createNode = ( options ) : opencensus . proto . agent . common . v1 . Node => { return { identifier : { hostName : options . hostName , pid : process . pid , startTimestamp : millisToTimestamp ( options . processStartTimeMillis ) , } , libraryInfo : { language : opencensus . proto . agent . common . v1 . LibraryInfo . Language . NODE_JS , exporterVersion : options . exporterVersion , coreLibraryVersion : options . coreVersion , } , serviceInfo : { name : options . serviceName } , attributes : options . attributes , } ; } ;