From fef4ad563b7368218581e8c42dda7e104d3dfb59 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Wed, 1 Dec 2021 00:21:07 +0100 Subject: [PATCH 01/12] First stab at surfacing deprecations from warning header --- .../elasticsearch/client/configure_client.ts | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/core/server/elasticsearch/client/configure_client.ts b/src/core/server/elasticsearch/client/configure_client.ts index 93c404593af3f..fbbf587926b6c 100644 --- a/src/core/server/elasticsearch/client/configure_client.ts +++ b/src/core/server/elasticsearch/client/configure_client.ts @@ -14,7 +14,8 @@ import type { TransportRequestParams, TransportRequestOptions, } from '@elastic/elasticsearch/lib/Transport'; -import { Logger } from '../../logging'; + +import { Logger, LogMeta } from '../../logging'; import { parseClientOptions, ElasticsearchClientConfig } from './client_config'; const noop = () => undefined; @@ -47,7 +48,7 @@ export const configureClient = ( } const client = new Client({ ...clientOptions, Transport: KibanaTransport }); - addLogging(client, logger.get('query', type)); + addLogging({ client, logger, type }); // --------------------------------------------------------------------------------- // // Hack to disable the "Product check" only in the scoped clients while we // @@ -119,7 +120,9 @@ export function getRequestDebugMeta(event: RequestEvent): { }; } -const addLogging = (client: Client, logger: Logger) => { +const addLogging = ({ client, type, logger }: { client: Client; type: string; logger: Logger }) => { + const queryLogger = logger.get('query', type); + const deprecationLogger = logger.get('deprecation', type); client.on('response', (error, event) => { if (event) { const opaqueId = event.meta.request.options.opaqueId; @@ -128,14 +131,47 @@ const addLogging = (client: Client, logger: Logger) => { http: { request: { id: event.meta.request.options.opaqueId } }, } : undefined; // do not clutter logs if opaqueId is not present + let queryMessage = ''; if (error) { if (error instanceof errors.ResponseError) { - logger.debug(`${getResponseMessage(event)} ${getErrorMessage(error)}`, meta); + queryMessage = `${getResponseMessage(event)} ${getErrorMessage(error)}`; } else { - logger.debug(getErrorMessage(error), meta); + queryMessage = getErrorMessage(error); } } else { - logger.debug(getResponseMessage(event), meta); + queryMessage = getResponseMessage(event); + } + + queryLogger.debug(queryMessage, meta); + + if (event.headers.warning ?? false) { + // Plugins can explicitly mark requests as originating from a user by + // removing the `'x-elastic-product-origin': 'kibana'` header that's + // added by default. User requests will be shown to users in the + // upgrade assistant UI as an action item that has to be addressed + // before they upgrade. + // Kibana requests will be hidden from the upgrade assistant UI and are + // only logged to help developers maintain their plugins + const requestOrigin = + (event.meta.request.params.headers != null && + (event.meta.request.params.headers['x-elastic-product-origin'] as unknown as string)) ?? + 'user'; + + const stackTrace = new Error().stack?.split('\n').slice(5).join('\n'); + + // Construct a JSON logMeta payload to make it easier for CI tools to consume + const logMeta = { + deprecation: { + message: event.headers.warning ?? 'placeholder', + requestOrigin, + query: queryMessage, + stack: stackTrace, + }, + }; + deprecationLogger.debug( + `ES DEPRECATION: ${event.headers.warning}\nOrigin:${requestOrigin}\nStack trace:\n${stackTrace}\nQuery:\n${queryMessage}`, + logMeta as unknown as LogMeta + ); } } }); From 270ed4e87c2be27494508d9af8259ddacfb7ca1f Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Wed, 1 Dec 2021 02:03:25 +0100 Subject: [PATCH 02/12] Log deprecations with error level but disable logger context by default --- .../elasticsearch/client/configure_client.ts | 16 ++++++++++------ src/core/server/logging/logging_config.ts | 8 +++++++- test/common/config.js | 2 ++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/core/server/elasticsearch/client/configure_client.ts b/src/core/server/elasticsearch/client/configure_client.ts index fbbf587926b6c..98b3581c73535 100644 --- a/src/core/server/elasticsearch/client/configure_client.ts +++ b/src/core/server/elasticsearch/client/configure_client.ts @@ -144,7 +144,7 @@ const addLogging = ({ client, type, logger }: { client: Client; type: string; lo queryLogger.debug(queryMessage, meta); - if (event.headers.warning ?? false) { + if (event.headers!.warning ?? false) { // Plugins can explicitly mark requests as originating from a user by // removing the `'x-elastic-product-origin': 'kibana'` header that's // added by default. User requests will be shown to users in the @@ -153,8 +153,10 @@ const addLogging = ({ client, type, logger }: { client: Client; type: string; lo // Kibana requests will be hidden from the upgrade assistant UI and are // only logged to help developers maintain their plugins const requestOrigin = - (event.meta.request.params.headers != null && - (event.meta.request.params.headers['x-elastic-product-origin'] as unknown as string)) ?? + (event.meta.request.options.headers != null && + (event.meta.request.options.headers[ + 'x-elastic-product-origin' + ] as unknown as string)) ?? 'user'; const stackTrace = new Error().stack?.split('\n').slice(5).join('\n'); @@ -162,14 +164,16 @@ const addLogging = ({ client, type, logger }: { client: Client; type: string; lo // Construct a JSON logMeta payload to make it easier for CI tools to consume const logMeta = { deprecation: { - message: event.headers.warning ?? 'placeholder', + message: event.headers!.warning ?? 'placeholder', requestOrigin, query: queryMessage, stack: stackTrace, }, }; - deprecationLogger.debug( - `ES DEPRECATION: ${event.headers.warning}\nOrigin:${requestOrigin}\nStack trace:\n${stackTrace}\nQuery:\n${queryMessage}`, + deprecationLogger.error( + `ES DEPRECATION: ${ + event.headers!.warning + }\nOrigin:${requestOrigin}\nStack trace:\n${stackTrace}\nQuery:\n${queryMessage}`, logMeta as unknown as LogMeta ); } diff --git a/src/core/server/logging/logging_config.ts b/src/core/server/logging/logging_config.ts index f5b75d7bb739c..fcd6e496c92b0 100644 --- a/src/core/server/logging/logging_config.ts +++ b/src/core/server/logging/logging_config.ts @@ -65,7 +65,13 @@ export const config = { defaultValue: new Map(), }), loggers: schema.arrayOf(loggerSchema, { - defaultValue: [], + defaultValue: [ + { + name: 'elasticsearch.deprecation', + level: 'off', + appenders: [], + }, + ], }), root: schema.object( { diff --git a/test/common/config.js b/test/common/config.js index 142c8ebd5cbc6..ffccdbf9d709f 100644 --- a/test/common/config.js +++ b/test/common/config.js @@ -53,6 +53,8 @@ export default function () { `--plugin-path=${path.join(__dirname, 'fixtures', 'plugins', 'newsfeed')}`, `--newsfeed.service.urlRoot=${servers.kibana.protocol}://${servers.kibana.hostname}:${servers.kibana.port}`, `--newsfeed.service.pathTemplate=/api/_newsfeed-FTS-external-service-simulators/kibana/v{VERSION}.json`, + '--logging.loggers[0].name=elasticsearch.deprecation', + '--logging.loggers[0].level=all', ], }, services, From 3d793d7c0f81bb053b540915ea2793da651f8175 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Thu, 2 Dec 2021 01:59:18 +0100 Subject: [PATCH 03/12] Don't filter out error logs from ProcRunner --- .../src/tooling_log/tooling_log_text_writer.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts index 660dae3fa1f55..328d1630cf179 100644 --- a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts +++ b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts @@ -91,8 +91,18 @@ export class ToolingLogTextWriter implements Writer { return false; } - if (this.ignoreSources && msg.source && this.ignoreSources.includes(msg.source)) { - return false; + if ( + this.ignoreSources && + msg.source && + this.ignoreSources.includes(msg.source) && + msg.type === 'write' + ) { + const txt = format(msg.args[0], ...msg.args.slice(1)); + if (/\] \[error\]\[/.test(txt)) { + return true; + } else { + return false; + } } const prefix = has(MSG_PREFIXES, msg.type) ? MSG_PREFIXES[msg.type] : ''; From 78da2cbfa8155afefc211684a9a3a4ba2bf3ec82 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Thu, 2 Dec 2021 14:40:28 +0100 Subject: [PATCH 04/12] Another try at not having messages ignored on CI --- .../tooling_log/tooling_log_text_writer.ts | 18 +++++----- .../elasticsearch/client/configure_client.ts | 35 ++++++++----------- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts index 328d1630cf179..26f1f89db146c 100644 --- a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts +++ b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts @@ -91,15 +91,15 @@ export class ToolingLogTextWriter implements Writer { return false; } - if ( - this.ignoreSources && - msg.source && - this.ignoreSources.includes(msg.source) && - msg.type === 'write' - ) { - const txt = format(msg.args[0], ...msg.args.slice(1)); - if (/\] \[error\]\[/.test(txt)) { - return true; + if (this.ignoreSources && msg.source && this.ignoreSources.includes(msg.source)) { + if (msg.type === 'write') { + const txt = format(msg.args[0], ...msg.args.slice(1)); + // Ensure that Elasticsearch deprecation log messages from Kibana aren't ignored + if (/\[deprecation\]\[elasticsearch\]/.test(txt)) { + return true; + } else { + return false; + } } else { return false; } diff --git a/src/core/server/elasticsearch/client/configure_client.ts b/src/core/server/elasticsearch/client/configure_client.ts index 98b3581c73535..1871af0f01b6f 100644 --- a/src/core/server/elasticsearch/client/configure_client.ts +++ b/src/core/server/elasticsearch/client/configure_client.ts @@ -15,7 +15,7 @@ import type { TransportRequestOptions, } from '@elastic/elasticsearch/lib/Transport'; -import { Logger, LogMeta } from '../../logging'; +import { Logger } from '../../logging'; import { parseClientOptions, ElasticsearchClientConfig } from './client_config'; const noop = () => undefined; @@ -122,7 +122,7 @@ export function getRequestDebugMeta(event: RequestEvent): { const addLogging = ({ client, type, logger }: { client: Client; type: string; logger: Logger }) => { const queryLogger = logger.get('query', type); - const deprecationLogger = logger.get('deprecation', type); + const deprecationLogger = logger.get('deprecation'); client.on('response', (error, event) => { if (event) { const opaqueId = event.meta.request.options.opaqueId; @@ -144,7 +144,7 @@ const addLogging = ({ client, type, logger }: { client: Client; type: string; lo queryLogger.debug(queryMessage, meta); - if (event.headers!.warning ?? false) { + if (event.warnings && event.warnings.length > 0) { // Plugins can explicitly mark requests as originating from a user by // removing the `'x-elastic-product-origin': 'kibana'` header that's // added by default. User requests will be shown to users in the @@ -156,26 +156,21 @@ const addLogging = ({ client, type, logger }: { client: Client; type: string; lo (event.meta.request.options.headers != null && (event.meta.request.options.headers[ 'x-elastic-product-origin' - ] as unknown as string)) ?? - 'user'; + ] as unknown as string)) === 'kibana' + ? 'kibana' + : 'user'; const stackTrace = new Error().stack?.split('\n').slice(5).join('\n'); - // Construct a JSON logMeta payload to make it easier for CI tools to consume - const logMeta = { - deprecation: { - message: event.headers!.warning ?? 'placeholder', - requestOrigin, - query: queryMessage, - stack: stackTrace, - }, - }; - deprecationLogger.error( - `ES DEPRECATION: ${ - event.headers!.warning - }\nOrigin:${requestOrigin}\nStack trace:\n${stackTrace}\nQuery:\n${queryMessage}`, - logMeta as unknown as LogMeta - ); + if (requestOrigin === 'kibana') { + deprecationLogger.info( + `ES DEPRECATION: ${event.warnings}\nOrigin:${requestOrigin}\nStack trace:\n${stackTrace}\nQuery:\n${queryMessage}` + ); + } else { + deprecationLogger.error( + `ES DEPRECATION: ${event.warnings}\nOrigin:${requestOrigin}\nStack trace:\n${stackTrace}\nQuery:\n${queryMessage}` + ); + } } } }); From 5374ac7a574e4a75583408479ee9c1270d1b5493 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Thu, 2 Dec 2021 17:19:54 +0100 Subject: [PATCH 05/12] Log deprecation logs with warn not info --- src/core/server/elasticsearch/client/configure_client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/server/elasticsearch/client/configure_client.ts b/src/core/server/elasticsearch/client/configure_client.ts index 1871af0f01b6f..9d2570a81af4e 100644 --- a/src/core/server/elasticsearch/client/configure_client.ts +++ b/src/core/server/elasticsearch/client/configure_client.ts @@ -163,7 +163,7 @@ const addLogging = ({ client, type, logger }: { client: Client; type: string; lo const stackTrace = new Error().stack?.split('\n').slice(5).join('\n'); if (requestOrigin === 'kibana') { - deprecationLogger.info( + deprecationLogger.warn( `ES DEPRECATION: ${event.warnings}\nOrigin:${requestOrigin}\nStack trace:\n${stackTrace}\nQuery:\n${queryMessage}` ); } else { From ed0e08b6a61a4676e6d2305d92f6b8f99265017d Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Fri, 3 Dec 2021 10:21:15 +0100 Subject: [PATCH 06/12] TEST: generate an artificial deprecation --- src/core/server/saved_objects/service/lib/repository.ts | 1 + test/api_integration/apis/saved_objects/find.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts index ed1ae4414f49c..a36ca8605d171 100644 --- a/src/core/server/saved_objects/service/lib/repository.ts +++ b/src/core/server/saved_objects/service/lib/repository.ts @@ -962,6 +962,7 @@ export class SavedObjectsRepository { esOptions, { ignore: [404], + headers: { 'x-elastic-product-origin': 'make deprecation' }, } ); if (statusCode === 404) { diff --git a/test/api_integration/apis/saved_objects/find.ts b/test/api_integration/apis/saved_objects/find.ts index 9b2b3a96cba5b..4ff1627ea0a6f 100644 --- a/test/api_integration/apis/saved_objects/find.ts +++ b/test/api_integration/apis/saved_objects/find.ts @@ -15,7 +15,7 @@ export default function ({ getService }: FtrProviderContext) { const kibanaServer = getService('kibanaServer'); const SPACE_ID = 'ftr-so-find'; - describe('find', () => { + describe.only('find', () => { before(async () => { await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_ID }); await kibanaServer.importExport.load( From 69f5778193b92645352a8fea5eccbd0147645c6d Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Fri, 3 Dec 2021 20:58:15 +0100 Subject: [PATCH 07/12] Let write() do it's writing --- .../kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts index 26f1f89db146c..2e62e880f8655 100644 --- a/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts +++ b/packages/kbn-dev-utils/src/tooling_log/tooling_log_text_writer.ts @@ -95,9 +95,7 @@ export class ToolingLogTextWriter implements Writer { if (msg.type === 'write') { const txt = format(msg.args[0], ...msg.args.slice(1)); // Ensure that Elasticsearch deprecation log messages from Kibana aren't ignored - if (/\[deprecation\]\[elasticsearch\]/.test(txt)) { - return true; - } else { + if (!/\[deprecation\]\[elasticsearch\]/.test(txt)) { return false; } } else { From 17b281aacab880b52a24698e19bed3a0fca1f2fe Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Sat, 4 Dec 2021 00:07:01 +0100 Subject: [PATCH 08/12] Revert "TEST: generate an artificial deprecation" This reverts commit ed0e08b6a61a4676e6d2305d92f6b8f99265017d. --- src/core/server/saved_objects/service/lib/repository.ts | 1 - test/api_integration/apis/saved_objects/find.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts index a36ca8605d171..ed1ae4414f49c 100644 --- a/src/core/server/saved_objects/service/lib/repository.ts +++ b/src/core/server/saved_objects/service/lib/repository.ts @@ -962,7 +962,6 @@ export class SavedObjectsRepository { esOptions, { ignore: [404], - headers: { 'x-elastic-product-origin': 'make deprecation' }, } ); if (statusCode === 404) { diff --git a/test/api_integration/apis/saved_objects/find.ts b/test/api_integration/apis/saved_objects/find.ts index 4ff1627ea0a6f..9b2b3a96cba5b 100644 --- a/test/api_integration/apis/saved_objects/find.ts +++ b/test/api_integration/apis/saved_objects/find.ts @@ -15,7 +15,7 @@ export default function ({ getService }: FtrProviderContext) { const kibanaServer = getService('kibanaServer'); const SPACE_ID = 'ftr-so-find'; - describe.only('find', () => { + describe('find', () => { before(async () => { await kibanaServer.spaces.create({ id: SPACE_ID, name: SPACE_ID }); await kibanaServer.importExport.load( From 54c5327bd10c7cfcb69b0cfaaf463a6d4fbd46a4 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Fri, 3 Dec 2021 21:47:08 +0100 Subject: [PATCH 09/12] Commit pre-built @kbn/pm package --- packages/kbn-pm/dist/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/kbn-pm/dist/index.js b/packages/kbn-pm/dist/index.js index 36b81ca60c098..c78c99c911e19 100644 --- a/packages/kbn-pm/dist/index.js +++ b/packages/kbn-pm/dist/index.js @@ -6639,7 +6639,17 @@ class ToolingLogTextWriter { } if (this.ignoreSources && msg.source && this.ignoreSources.includes(msg.source)) { - return false; + if (msg.type === 'write') { + const txt = (0, _util.format)(msg.args[0], ...msg.args.slice(1)); // Ensure that Elasticsearch deprecation log messages from Kibana aren't ignored + + if (/\[deprecation\]\[elasticsearch\]/.test(txt)) { + return true; + } else { + return false; + } + } else { + return false; + } } const prefix = has(MSG_PREFIXES, msg.type) ? MSG_PREFIXES[msg.type] : ''; From 098f6ec1b9f2c5281a3377227c539a95b70e627d Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Fri, 3 Dec 2021 22:18:21 +0100 Subject: [PATCH 10/12] Second try to commit pre-built @kbn/pm package --- packages/kbn-pm/dist/index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/kbn-pm/dist/index.js b/packages/kbn-pm/dist/index.js index c78c99c911e19..6723fd745f651 100644 --- a/packages/kbn-pm/dist/index.js +++ b/packages/kbn-pm/dist/index.js @@ -6642,9 +6642,7 @@ class ToolingLogTextWriter { if (msg.type === 'write') { const txt = (0, _util.format)(msg.args[0], ...msg.args.slice(1)); // Ensure that Elasticsearch deprecation log messages from Kibana aren't ignored - if (/\[deprecation\]\[elasticsearch\]/.test(txt)) { - return true; - } else { + if (!/\[deprecation\]\[elasticsearch\]/.test(txt)) { return false; } } else { From a3279a1d840c209648f0c8d30814d41922d7ba12 Mon Sep 17 00:00:00 2001 From: Rudolf Meijering Date: Fri, 3 Dec 2021 22:19:00 +0100 Subject: [PATCH 11/12] Enable deprecation logger for jest_integration even though logs aren't interleaved --- src/core/test_helpers/kbn_server.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/test_helpers/kbn_server.ts b/src/core/test_helpers/kbn_server.ts index a2aa453cf55e7..a3763ae69ea41 100644 --- a/src/core/test_helpers/kbn_server.ts +++ b/src/core/test_helpers/kbn_server.ts @@ -105,6 +105,9 @@ export function createRootWithCorePlugins(settings = {}, cliArgs: Partial Date: Fri, 3 Dec 2021 22:21:07 +0100 Subject: [PATCH 12/12] Apply suggestions from code review Co-authored-by: Luke Elmers --- .../client/configure_client.test.ts | 147 ++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/src/core/server/elasticsearch/client/configure_client.test.ts b/src/core/server/elasticsearch/client/configure_client.test.ts index 35fcb5819d015..6db433129195b 100644 --- a/src/core/server/elasticsearch/client/configure_client.test.ts +++ b/src/core/server/elasticsearch/client/configure_client.test.ts @@ -500,5 +500,152 @@ describe('configureClient', () => { `); }); }); + + describe('deprecation warnings from response headers', () => { + it('does not log when no deprecation warning header is returned', () => { + const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); + + const response = createResponseWithBody({ + seq_no_primary_term: true, + query: { + term: { user: 'kimchy' }, + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + expect(loggingSystemMock.collect(logger).warn).toEqual([]); + expect(loggingSystemMock.collect(logger).error).toEqual([]); + }); + + it('logs error when the client receives an Elasticsearch error response for a deprecated request originating from a user', () => { + const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); + + const response = createApiResponse({ + statusCode: 400, + warnings: ['GET /_path is deprecated'], + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + }, + body: { + error: { + type: 'illegal_argument_exception', + reason: 'request [/_path] contains unrecognized parameter: [name]', + }, + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + expect(loggingSystemMock.collect(logger).warn).toEqual([]); + expect(loggingSystemMock.collect(logger).error[0][0]).toMatch( + 'ES DEPRECATION: GET /_path is deprecated' + ); + expect(loggingSystemMock.collect(logger).error[0][0]).toMatch('Origin:user'); + expect(loggingSystemMock.collect(logger).error[0][0]).toMatch(/Stack trace:\n.*at/); + expect(loggingSystemMock.collect(logger).error[0][0]).toMatch( + /Query:\n.*400\n.*GET \/_path\?hello\=dolly \[illegal_argument_exception\]: request \[\/_path\] contains unrecognized parameter: \[name\]/ + ); + }); + + it('logs warning when the client receives an Elasticsearch error response for a deprecated request originating from kibana', () => { + const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); + + const response = createApiResponse({ + statusCode: 400, + warnings: ['GET /_path is deprecated'], + // Set the request header to indicate to Elasticsearch that this is a request over which users have no control + requestOptions: { headers: { 'x-elastic-product-origin': 'kibana' } }, + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + }, + body: { + error: { + type: 'illegal_argument_exception', + reason: 'request [/_path] contains unrecognized parameter: [name]', + }, + }, + }); + client.diagnostic.emit('response', new errors.ResponseError(response), response); + + expect(loggingSystemMock.collect(logger).error).toEqual([]); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch( + 'ES DEPRECATION: GET /_path is deprecated' + ); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch('Origin:kibana'); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch(/Stack trace:\n.*at/); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch( + /Query:\n.*400\n.*GET \/_path\?hello\=dolly \[illegal_argument_exception\]: request \[\/_path\] contains unrecognized parameter: \[name\]/ + ); + }); + + it('logs error when the client receives an Elasticsearch success response for a deprecated request originating from a user', () => { + const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); + + const response = createApiResponse({ + statusCode: 200, + warnings: ['GET /_path is deprecated'], + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + }, + body: { + hits: [ + { + _source: 'may the source be with you', + }, + ], + }, + }); + client.diagnostic.emit('response', null, response); + + expect(loggingSystemMock.collect(logger).warn).toEqual([]); + expect(loggingSystemMock.collect(logger).error[0][0]).toMatch( + 'ES DEPRECATION: GET /_path is deprecated' + ); + expect(loggingSystemMock.collect(logger).error[0][0]).toMatch('Origin:user'); + expect(loggingSystemMock.collect(logger).error[0][0]).toMatch(/Stack trace:\n.*at/); + expect(loggingSystemMock.collect(logger).error[0][0]).toMatch( + /Query:\n.*200\n.*GET \/_path\?hello\=dolly/ + ); + }); + + it('logs warning when the client receives an Elasticsearch success response for a deprecated request originating from kibana', () => { + const client = configureClient(createFakeConfig(), { logger, type: 'test', scoped: false }); + + const response = createApiResponse({ + statusCode: 200, + warnings: ['GET /_path is deprecated'], + // Set the request header to indicate to Elasticsearch that this is a request over which users have no control + requestOptions: { headers: { 'x-elastic-product-origin': 'kibana' } }, + params: { + method: 'GET', + path: '/_path', + querystring: { hello: 'dolly' }, + }, + body: { + hits: [ + { + _source: 'may the source be with you', + }, + ], + }, + }); + client.diagnostic.emit('response', null, response); + + expect(loggingSystemMock.collect(logger).error).toEqual([]); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch( + 'ES DEPRECATION: GET /_path is deprecated' + ); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch('Origin:kibana'); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch(/Stack trace:\n.*at/); + expect(loggingSystemMock.collect(logger).warn[0][0]).toMatch( + /Query:\n.*200\n.*GET \/_path\?hello\=dolly/ + ); + }); + }); }); });