import { DEFAULT_MAX_SPAN } from '../constant' ; import { PageContext , Span as SpanInterface , SpanContext , utils } from '../shared' ; const { timestampWithMs , isPlainObject } = utils ; internal hideconstructor hidden export class SpanRecorder { public spans : Span [ ] = [ ] ; public add ( span ) { if ( this . spans . length > DEFAULT_MAX_SPAN ) { delete span . spanRecorder ; } else { this . spans . push ( span ) ; } } } /**
 * Span contains all data about a span
 */ export class Span implements SpanInterface , SpanContext { public startTimestamp : number = timestampWithMs ( ) ; public endTimestamp ? : number ; public description ? : string ; public spanRecorder ? : SpanRecorder ; public pageContexts : PageContext [ ] = [ ] ; public constructor ( spanContext ? ) { if ( ! spanContext || ! isPlainObject ( spanContext ) ) { return this ; } if ( spanContext . description ) { this . description = spanContext . description ; } if ( typeof spanContext . startTimestamp !== 'undefined' ) { this . startTimestamp = spanContext . startTimestamp ; } if ( typeof spanContext . endTimestamp !== 'undefined' ) { this . endTimestamp = spanContext . endTimestamp ; } } addPageContext ( ctx ) { this . pageContexts . push ( ctx ) ; } inheritDoc deprecated public child ( spanContext ? ) { return this . startChild ( spanContext ) ; } inheritDoc public startChild ( spanContext ? ) { const span = new Span ( { ... spanContext , } ) ; span . spanRecorder = this . spanRecorder ; if ( span . spanRecorder ) { span . spanRecorder . add ( span ) ; } return span ; } inheritDoc public finish ( endTimestamp ? ) { this . endTimestamp = typeof endTimestamp === 'number' ? endTimestamp : timestampWithMs ( ) ; } }