import * as childProcess from 'child_process' ; import { HrTime , Span , SpanAttributes , SpanKind , SpanStatus , } from '@opentelemetry/api' ; import * as assert from 'assert' ; import { ReadableSpan } from '@opentelemetry/sdk-trace-base' ; import { hrTimeToMilliseconds , hrTimeToMicroseconds , } from '@opentelemetry/core' ; import * as path from 'path' ; const dockerRunCmds = { cassandra : 'docker run --rm -d --name otel-cassandra -p 9042:9042 bitnami/cassandra:3' , memcached : 'docker run --rm -d --name otel-memcached -p 11211:11211 memcached:1.6.9-alpine' , mssql : 'docker run --rm -d --name otel-mssql -p 1433:1433 -e SA_PASSWORD=mssql_passw0rd -e ACCEPT_EULA=Y mcr.microsoft.com/mssql/server:2017-latest' , mysql : 'docker run --rm -d --name otel-mysql -p 33306:3306 -e MYSQL_ROOT_PASSWORD=rootpw -e MYSQL_DATABASE=test_db -e MYSQL_USER=otel -e MYSQL_PASSWORD=secret circleci/mysql:5.7' , postgres : 'docker run --rm -d --name otel-postgres -p 54320:5432 -e POSTGRES_PASSWORD=postgres postgres:13-alpine' , redis : 'docker run --rm -d --name otel-redis -p 63790:6379 redis:alpine' , } ; export function startDocker ( db ) { const tasks = [ run ( dockerRunCmds [ db ] ) ] ; for ( let i = 0 ; i < tasks . length ; i ++ ) { const task = tasks [ i ] ; if ( task && task . code !== 0 ) { console . error ( 'Failed to start container!' ) ; console . error ( task . output ) ; return false ; } } return true ; } export function cleanUpDocker ( db ) { run ( `docker stop otel-${ db }` ) ; run ( `docker rm otel-${ db }` ) ; } function run ( cmd ) { try { const proc = childProcess . spawnSync ( cmd , { shell : true , } ) ; if ( proc . status !== 0 ) { console . error ( 'Failed run command:' , cmd ) ; console . error ( proc . output ) ; } return { code : proc . status , output : proc . output . map ( v => String . fromCharCode . apply ( null , v as any ) ) . join ( '' ) , } ; } catch ( e ) { console . log ( e ) ; return ; } } export const assertSpan = ( span , kind , attributes , events , status ) => { assert . strictEqual ( span . spanContext ( ) . traceId . length , 32 ) ; assert . strictEqual ( span . spanContext ( ) . spanId . length , 16 ) ; assert . strictEqual ( span . kind , kind ) ; assert . ok ( span . endTime ) ; assert . strictEqual ( span . links . length , 0 ) ; assert . ok ( hrTimeToMicroseconds ( span . startTime ) < hrTimeToMicroseconds ( span . endTime ) ) ; assert . ok ( hrTimeToMilliseconds ( span . endTime ) > 0 ) ; assert . deepStrictEqual ( span . attributes , attributes ) ; assert . deepStrictEqual ( span . events , events ) ; assert . strictEqual ( span . status . code , status . code ) ; if ( status . message ) { assert . strictEqual ( span . status . message , status . message ) ; } } ; export const assertPropagation = ( childSpan , parentSpan ) => { const targetSpanContext = childSpan . spanContext ( ) ; const sourceSpanContext = parentSpan . spanContext ( ) ; assert . strictEqual ( targetSpanContext . traceId , sourceSpanContext . traceId ) ; assert . strictEqual ( childSpan . parentSpanId , sourceSpanContext . spanId ) ; assert . strictEqual ( targetSpanContext . traceFlags , sourceSpanContext . traceFlags ) ; assert . notStrictEqual ( targetSpanContext . spanId , sourceSpanContext . spanId ) ; } ; /**
 * Represents a timed event.
 * A timed event is an event with a timestamp.
 */ export interface TimedEvent { time : HrTime ; /** The name of the event. */ name : string ; /** The attributes of the event. */ attributes ? : SpanAttributes ; } export const getPackageVersion = ( packageName ) => { const packagePath = require . resolve ( packageName ) ; const packageJsonPath = path . join ( path . dirname ( packagePath ) , 'package.json' ) ; return require ( packageJsonPath ) . version ; } ;