From 45e1ff83a53fbeec1a6421dd99d94df26f9a6271 Mon Sep 17 00:00:00 2001 From: restrry Date: Wed, 29 Jul 2020 16:26:55 +0200 Subject: [PATCH] log request details in case of errors --- .../server/elasticsearch/client/configure_client.test.ts | 7 ++++++- src/core/server/elasticsearch/client/configure_client.ts | 7 +++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/core/server/elasticsearch/client/configure_client.test.ts b/src/core/server/elasticsearch/client/configure_client.test.ts index 11e3199a79fd2..c6752e32f92b0 100644 --- a/src/core/server/elasticsearch/client/configure_client.test.ts +++ b/src/core/server/elasticsearch/client/configure_client.test.ts @@ -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', @@ -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]", ], ] `); diff --git a/src/core/server/elasticsearch/client/configure_client.ts b/src/core/server/elasticsearch/client/configure_client.ts index 9746ecb538b75..365e8662380e3 100644 --- a/src/core/server/elasticsearch/client/configure_client.ts +++ b/src/core/server/elasticsearch/client/configure_client.ts @@ -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'; @@ -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);