import chalk from 'chalk' ; import { KibanaRequest } from '../../../../../../../src/core/server' ; import { RequestStatus } from '../../../../../../../src/plugins/inspector' ; import { WrappedElasticsearchClientError } from '../../../../../observability/server' ; import { inspectableEsQueriesMap } from '../../../routes/apm_routes/register_apm_server_routes' ; import { getInspectResponse } from '../../../../../observability/server' ; function formatObj ( obj ) { return JSON . stringify ( obj , null , 2 ) ; } export async function callAsyncWithDebug < T > ( { cb , getDebugMessage , debug , request , requestType , requestParams , operationName , isCalledWithInternalUser , } ) { if ( ! debug ) { return cb ( ) ; } const hrStartTime = process . hrtime ( ) ; const startTime = Date . now ( ) ; let res ; let esError = null ; let esRequestStatus = RequestStatus . PENDING ; try { res = await cb ( ) ; esRequestStatus = RequestStatus . OK ; } catch ( e ) { esError = e ; esRequestStatus = RequestStatus . ERROR ; } if ( debug ) { const highlightColor = esError ? 'bgRed' : 'inverse' ; const diff = process . hrtime ( hrStartTime ) ; const duration = Math . round ( diff [ 0 ] * 1000 + diff [ 1 ] / 1e6 ) ; const { title , body } = getDebugMessage ( ) ; console . log ( chalk . bold [ highlightColor ] ( `=== Debug: ${ title } (${ duration }ms) ===` ) ) ; console . log ( body ) ; console . log ( `\n` ) ; const inspectableEsQueries = inspectableEsQueriesMap . get ( request ) ; if ( ! isCalledWithInternalUser && inspectableEsQueries ) { inspectableEsQueries . push ( getInspectResponse ( { esError , esRequestParams : requestParams , esRequestStatus , esResponse : res , kibanaRequest : request , operationName , startTime , } ) ) ; } } if ( esError ) { throw esError ; } return res ; } export const getDebugBody = ( { params , requestType , operationName , } ) => { const operationLine = `${ operationName }\n` ; if ( requestType === 'search' ) { return `${ operationLine }GET ${ params . index }/_search\n${ formatObj ( params . body ) }` ; } return `${ chalk . bold ( 'ES operation:' ) } ${ requestType }\n${ chalk . bold ( 'ES query:' ) }\n${ operationLine }${ formatObj ( params ) }` ; } ; export const getDebugTitle = ( request ) => `${ request . route . method . toUpperCase ( ) } ${ request . route . path }` ;