Skip to content

Commit

Permalink
log request details in case of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Jul 29, 2020
1 parent 35d4bc6 commit 45e1ff8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ describe('configureClient', () => {
const response = createApiResponse({
statusCode: 400,
headers: {},
params: {
method: 'GET',
path: '/foo',
querystring: { hello: 'dolly' },
},
body: {
error: {
type: 'illegal_argument_exception',
Expand All @@ -151,7 +156,7 @@ describe('configureClient', () => {
expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(`
Array [
Array [
"[illegal_argument_exception]: request [/_path] contains unrecognized parameter: [name]",
"400 GET /foo [illegal_argument_exception]: request [/_path] contains unrecognized parameter: [name]",
],
]
`);
Expand Down
7 changes: 5 additions & 2 deletions src/core/server/elasticsearch/client/configure_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { stringify } from 'querystring';
import { Client } from '@elastic/elasticsearch';
import { Client, RequestEvent } from '@elastic/elasticsearch';
import { Logger } from '../../logging';
import { parseClientOptions, ElasticsearchClientConfig } from './client_config';
import { isResponseError } from './errors';
Expand All @@ -35,13 +35,16 @@ export const configureClient = (
return client;
};

const stringifyEventInfo = (event: RequestEvent) =>
`${event.statusCode} ${event.meta.request.params.method} ${event.meta.request.params.path}`;

const addLogging = (client: Client, logger: Logger, logQueries: boolean) => {
client.on('response', (error, event) => {
if (error) {
const errorMessage =
// error details for response errors provided by elasticsearch
isResponseError(error)
? `[${event.body.error.type}]: ${event.body.error.reason}`
? `${stringifyEventInfo(event)} [${event.body.error.type}]: ${event.body.error.reason}`
: `[${error.name}]: ${error.message}`;

logger.error(errorMessage);
Expand Down

0 comments on commit 45e1ff8

Please sign in to comment.