Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
dgieselaar committed Jan 20, 2021
1 parent 8841f31 commit 87bd80e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ function formatObj(obj: Record<string, any>) {

export async function callAsyncWithDebug<T>({
cb,
getMessage,
getDebugMessage,
debug,
}: {
cb: () => Promise<T>;
getMessage: () => { body: string; title: string };
getDebugMessage: () => { body: string; title: string };
debug: boolean;
}) {
if (!debug) {
Expand All @@ -42,7 +42,7 @@ export async function callAsyncWithDebug<T>({
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}) ===`)
Expand All @@ -59,16 +59,18 @@ export async function callAsyncWithDebug<T>({
return res;
}

export const getSearchDebugBody = (params: Record<string, any>) =>
`GET ${params.index}/_search\n${formatObj(params.body)}`;

export const getDefaultDebugBody = (
export const getDebugBody = (
params: Record<string, any>,
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}`;
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ export function cancelEsRequestOnAbort<T extends TransportRequestPromise<any>>(
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -99,8 +99,8 @@ export function createApmEventClient({

return unwrapEsResponse(searchPromise);
},
getMessage: () => ({
body: getSearchDebugBody(searchParams),
getDebugMessage: () => ({
body: getDebugBody(searchParams, 'search'),
title: getDebugTitle(request),
}),
debug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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,
});
Expand Down

0 comments on commit 87bd80e

Please sign in to comment.