import { ApmFields } from '../apm_fields' ; import { aggregate } from './aggregate' ; export function getSpanDestinationMetrics ( events ) { const exitSpans = events . filter ( ( event ) => ! ! event [ 'span.destination.service.resource' ] ) ; const metricsets = aggregate ( exitSpans , [ 'event.outcome' , 'agent.name' , 'service.environment' , 'service.name' , 'span.destination.service.resource' , ] ) ; return metricsets . map ( ( metricset ) => { let count = 0 ; let sum = 0 ; for ( const event of metricset . events ) { count ++ ; sum += event [ 'span.duration.us' ] ! ; } return { ... metricset . key , [ 'metricset.name' ] : 'span_destination' , 'span.destination.service.response_time.sum.us' : sum , 'span.destination.service.response_time.count' : count , } ; } ) ; }