import { ApmError } from './apm_error' ; import { BaseSpan } from './base_span' ; import { generateShortId } from '../utils/generate_id' ; import { ApmFields } from './apm_fields' ; export class Transaction extends BaseSpan { private _sampled : boolean = true ; private readonly _errors : ApmError [ ] = [ ] ; constructor ( fields ) { super ( { ... fields , 'processor.event' : 'transaction' , 'transaction.id' : generateShortId ( ) , 'transaction.sampled' : true , } ) ; } parent ( span ) { super . parent ( span ) ; this . _errors . forEach ( ( error ) => { error . fields [ 'trace.id' ] = this . fields [ 'trace.id' ] ; error . fields [ 'transaction.id' ] = this . fields [ 'transaction.id' ] ; error . fields [ 'transaction.type' ] = this . fields [ 'transaction.type' ] ; } ) ; return this ; } errors ( ... errors ) { errors . forEach ( ( error ) => { error . fields [ 'trace.id' ] = this . fields [ 'trace.id' ] ; error . fields [ 'transaction.id' ] = this . fields [ 'transaction.id' ] ; error . fields [ 'transaction.type' ] = this . fields [ 'transaction.type' ] ; } ) ; this . _errors . push ( ... errors ) ; return this ; } duration ( duration ) { this . fields [ 'transaction.duration.us' ] = duration * 1000 ; return this ; } sample ( sampled = true ) { this . _sampled = sampled ; return this ; } serialize ( ) { const [ transaction , ... spans ] = super . serialize ( ) ; const errors = this . _errors . flatMap ( ( error ) => error . serialize ( ) ) ; const events = [ transaction ] ; if ( this . _sampled ) { events . push ( ... spans ) ; } return events . concat ( errors ) ; } }