import { asMutableArray } from '../../../../common/utils/as_mutable_array' ; import { ERROR_GROUP_ID , SERVICE_NAME , TRANSACTION_SAMPLED , } from '../../../../common/elasticsearch_fieldnames' ; import { ProcessorEvent } from '../../../../common/processor_event' ; import { rangeQuery , kqlQuery } from '../../../../../observability/server' ; import { environmentQuery } from '../../../../common/utils/environment_query' ; import { getTransaction } from '../../transactions/get_transaction' ; import { Setup } from '../../../lib/helpers/setup_request' ; export async function getErrorGroupSample ( { environment , kuery , serviceName , groupId , setup , start , end , } ) { const { apmEventClient } = setup ; const params = { apm : { events : [ ProcessorEvent . error as const ] , } , body : { size : 1 , query : { bool : { filter : [ { term : { [ SERVICE_NAME ] : serviceName } } , { term : { [ ERROR_GROUP_ID ] : groupId } } , ... rangeQuery ( start , end ) , ... environmentQuery ( environment ) , ... kqlQuery ( kuery ) , ] , should : [ { term : { [ TRANSACTION_SAMPLED ] : true } } ] , } , } , sort : asMutableArray ( [ { _score : { order : 'desc' } } , { '@timestamp' : { order : 'desc' } } , ] as const ) , } , } ; const resp = await apmEventClient . search ( 'get_error_group_sample' , params ) ; const error = resp . hits . hits [ 0 ] ?. _source ; const transactionId = error ?. transaction ?. id ; const traceId = error ?. trace ?. id ; let transaction ; if ( transactionId && traceId ) { transaction = await getTransaction ( { transactionId , traceId , setup , start , end , } ) ; } return { transaction , error , occurrencesCount : resp . hits . total . value , } ; }