diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/call_async_with_debug.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/call_async_with_debug.ts index 42418c706c15a..9d612d82d99bb 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/call_async_with_debug.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/call_async_with_debug.ts @@ -15,11 +15,11 @@ function formatObj(obj: Record) { export async function callAsyncWithDebug({ cb, - getMessage, + getDebugMessage, debug, }: { cb: () => Promise; - getMessage: () => { body: string; title: string }; + getDebugMessage: () => { body: string; title: string }; debug: boolean; }) { if (!debug) { @@ -42,7 +42,7 @@ export async function callAsyncWithDebug({ const diff = process.hrtime(startTime); const duration = `${Math.round(diff[0] * 1000 + diff[1] / 1e6)}ms`; - const { title, body } = getMessage(); + const { title, body } = getDebugMessage(); console.log( chalk.bold[highlightColor](`=== Debug: ${title} (${duration}) ===`) @@ -59,16 +59,18 @@ export async function callAsyncWithDebug({ return res; } -export const getSearchDebugBody = (params: Record) => - `GET ${params.index}/_search\n${formatObj(params.body)}`; - -export const getDefaultDebugBody = ( +export const getDebugBody = ( params: Record, operationName: string -) => - `${chalk.bold('ES operation:')} ${operationName}\n${chalk.bold( +) => { + if (operationName === 'search') { + return `GET ${params.index}/_search\n${formatObj(params.body)}`; + } + + return `${chalk.bold('ES operation:')} ${operationName}\n${chalk.bold( 'ES query:' )}\n${formatObj(params)}`; +}; export const getDebugTitle = (request: KibanaRequest) => `${request.route.method.toUpperCase()} ${request.route.path}`; diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/cancel_es_request_on_abort.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/cancel_es_request_on_abort.ts index e9b61a27f4380..52064e54081a5 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/cancel_es_request_on_abort.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/cancel_es_request_on_abort.ts @@ -15,13 +15,7 @@ export function cancelEsRequestOnAbort>( promise.abort(); }); - // using .catch() here means unsubscribe will be called - // after it has thrown an error, so we use .then(onSuccess, onFailure) - // syntax - promise.then( - () => subscription.unsubscribe(), - () => subscription.unsubscribe() - ); + promise.finally(() => subscription.unsubscribe()); return promise; } diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts index cc4ea5fd35173..b2e25994d6fe6 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_apm_event_client/index.ts @@ -25,7 +25,7 @@ import { unpackProcessorEvents } from './unpack_processor_events'; import { callAsyncWithDebug, getDebugTitle, - getSearchDebugBody, + getDebugBody, } from '../call_async_with_debug'; import { cancelEsRequestOnAbort } from '../cancel_es_request_on_abort'; @@ -99,8 +99,8 @@ export function createApmEventClient({ return unwrapEsResponse(searchPromise); }, - getMessage: () => ({ - body: getSearchDebugBody(searchParams), + getDebugMessage: () => ({ + body: getDebugBody(searchParams, 'search'), title: getDebugTitle(request), }), debug, diff --git a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_internal_es_client/index.ts b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_internal_es_client/index.ts index 32cccb5fffe0e..69f596520d216 100644 --- a/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_internal_es_client/index.ts +++ b/x-pack/plugins/apm/server/lib/helpers/create_es_client/create_internal_es_client/index.ts @@ -14,8 +14,8 @@ import { } from '../../../../../../../typings/elasticsearch'; import { callAsyncWithDebug, + getDebugBody, getDebugTitle, - getDefaultDebugBody, } from '../call_async_with_debug'; import { cancelEsRequestOnAbort } from '../cancel_es_request_on_abort'; @@ -43,9 +43,9 @@ export function createInternalESClient({ }) { return callAsyncWithDebug({ cb: () => unwrapEsResponse(cancelEsRequestOnAbort(cb(), request)), - getMessage: () => ({ + getDebugMessage: () => ({ title: getDebugTitle(request), - body: getDefaultDebugBody(params, operationName), + body: getDebugBody(params, operationName), }), debug: context.params.query._debug, });