import * as bodyParser from 'body-parser' ; import * as express from 'express' ; import * as net from 'net' ; import { COLLECTOR_CONFIG } from './config' ; export function serviceName ( span ) { if ( span . annotations && span . annotations . length ) { if ( span . annotations [ 0 ] . endpoint ) { return span . annotations [ 0 ] . endpoint . serviceName ; } } } export interface IMockCollector { server : net . Server ; reset : ( ) => void ; traces : ( ) => any ; close : ( ) => Promise < void > ; } export function createServer ( ) { const app = express ( ) ; app . use ( bodyParser . json ( ) ) ; let traces = { } ; function recordTraces ( body ) { body . forEach ( ( next ) => { const traceId = next . traceId ; const id = next . id ; if ( traces [ traceId ] === undefined ) { traces [ traceId ] = { } ; } traces [ traceId ] [ id ] = { traceId : next . traceId , id : next . id , parentId : next . parentId , duration : next . duration , serviceName : serviceName ( next ) , } ; } ) ; } app . post ( '/api/v1/spans' , ( req , res ) : void => { if ( req . body && req . body . length ) { recordTraces ( req . body ) ; } res . sendStatus ( 202 ) ; } ) ; app . post ( '/api/v2/spans' , ( req , res ) : void => { if ( req . body && req . body . length ) { recordTraces ( req . body ) ; } res . sendStatus ( 202 ) ; } ) ; return new Promise ( ( resolve , reject ) => { const server = app . listen ( COLLECTOR_CONFIG . port , ( err ? ) => { if ( err ) { console . error ( `MockCollection unable to start: ${ err . message }` ) ; reject ( err ) ; } else { console . log ( `MockCollector listening on port[${ COLLECTOR_CONFIG . port }]` ) ; resolve ( { server , reset ( ) { traces = { } ; } , traces ( ) { const tracesToReturn = traces ; traces = { } ; return tracesToReturn ; } , close ( ) { return new Promise ( ( res , rej ) => { server . close ( ( ) => { console . log ( 'MockCollector closed' ) ; server . unref ( ) ; res ( ) ; } ) ; } ) ; } , } ) ; } } ) ; } ) ; }