import { CacheDuplexEndPoint } from '../CacheDuplexEndPoint' ; import { CommonCache } from '../../util/CommonCache' ; import { SLOW_TRACE , ERROR_TRACE , SKIP_RATE } from '../../trace/Constants' ; export class TraceEndPoint extends CacheDuplexEndPoint { group : string = 'trace' ; private _pushData ( data ) { let cache = this . cacheMap . get ( data . appName ) ; if ( ! cache ) { cache = new CommonCache ( this . config [ 'cacheSize' ] || CacheDuplexEndPoint . DEFAULT_CACHE_SIZE ) ; this . cacheMap . set ( data . appName , cache ) ; } this . debug ( 'push data by rate' ) ; cache . push ( data ) ; } processReporter ( data , reply ? ) { if ( data . appName && data . traceId ) { if ( this . cacheByRate ( data ) ) { this . _pushData ( data ) ; } else if ( ( data . status & SLOW_TRACE || data . status & ERROR_TRACE ) && this . getPriority ( ) ) { data [ SKIP_RATE ] = true ; this . _pushData ( data ) ; } else { this . debug ( 'drop trace: %j' , data ) ; } } } async invoke ( args = { by : 'size' , value : 0 } ) { const results = await super . invoke ( args ) ; if ( args . traceId ) { return results . filter ( ( item ) => { return item . traceId === args . traceId ; } ) ; } return results ; } getPriority ( ) { return this . config [ 'priority' ] ; } getRate ( ) { return this . config [ 'rate' ] ; } cacheByRate ( data ) { return Math . round ( Math . random ( ) * 100 ) <= this . getRate ( ) ; } }