import { Serializable } from '../serializable' ; import { Span } from './span' ; import { Transaction } from './transaction' ; import { generateLongId } from '../utils/generate_id' ; import { ApmFields } from './apm_fields' ; export class BaseSpan extends Serializable < ApmFields > { private readonly _children : BaseSpan [ ] = [ ] ; constructor ( fields ) { super ( { ... fields , 'event.outcome' : 'unknown' , 'trace.id' : generateLongId ( ) , 'processor.name' : 'transaction' , } ) ; } parent ( span ) { this . fields [ 'trace.id' ] = span . fields [ 'trace.id' ] ; this . fields [ 'parent.id' ] = span . isSpan ( ) ? span . fields [ 'span.id' ] : span . fields [ 'transaction.id' ] ; if ( this . isSpan ( ) ) { this . fields [ 'transaction.id' ] = span . fields [ 'transaction.id' ] ; } this . _children . forEach ( ( child ) => { child . parent ( this ) ; } ) ; return this ; } children ( ... children ) { children . forEach ( ( child ) => { child . parent ( this ) ; } ) ; this . _children . push ( ... children ) ; return this ; } success ( ) { this . fields [ 'event.outcome' ] = 'success' ; return this ; } failure ( ) { this . fields [ 'event.outcome' ] = 'failure' ; return this ; } outcome ( outcome ) { this . fields [ 'event.outcome' ] = outcome ; return this ; } serialize ( ) { return [ this . fields , ... this . _children . flatMap ( ( child ) => child . serialize ( ) ) ] ; } isSpan ( ) { return this . fields [ 'processor.event' ] === 'span' ; } isTransaction ( ) { return this . fields [ 'processor.event' ] === 'transaction' ; } }