From 1f72957e0ea8f23a21ccc5f7482866ad99d7ffbc Mon Sep 17 00:00:00 2001 From: restrry Date: Tue, 16 Nov 2021 13:38:35 +0100 Subject: [PATCH] do not enable RUM agent when nodejs is run in contextPropagationOnly mode --- .../server/http_resources/get_apm_config.test.ts | 16 ++++++++++++++++ src/core/server/http_resources/get_apm_config.ts | 10 ++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/core/server/http_resources/get_apm_config.test.ts b/src/core/server/http_resources/get_apm_config.test.ts index bd867375f46d60..4c8d40550f7db6 100644 --- a/src/core/server/http_resources/get_apm_config.test.ts +++ b/src/core/server/http_resources/get_apm_config.test.ts @@ -34,6 +34,22 @@ describe('getApmConfig', () => { expect(getApmConfig('/path')).toBeNull(); }); + it('returns null if apm is enabled with contextPropagationOnly: true', () => { + getConfigurationMock.mockReturnValue({ + active: true, + contextPropagationOnly: true, + }); + expect(getApmConfig('/path')).toBeDefined(); + }); + + it('returns null if apm is enabled with disableSend: true', () => { + getConfigurationMock.mockReturnValue({ + active: true, + disableSend: true, + }); + expect(getApmConfig('/path')).toBeDefined(); + }); + it('calls `getConfig` with the correct parameters', () => { getApmConfig('/path'); diff --git a/src/core/server/http_resources/get_apm_config.ts b/src/core/server/http_resources/get_apm_config.ts index 6ea172b162d285..2472f7e97d17a7 100644 --- a/src/core/server/http_resources/get_apm_config.ts +++ b/src/core/server/http_resources/get_apm_config.ts @@ -6,12 +6,18 @@ * Side Public License, v 1. */ -import agent from 'elastic-apm-node'; +import agent, { AgentConfigOptions } from 'elastic-apm-node'; import { getConfiguration } from '@kbn/apm-config-loader'; +function shouldInstrumentClient(config?: AgentConfigOptions): boolean { + return Boolean( + config?.active === true && config.contextPropagationOnly !== true && config.disableSend !== true + ); +} + export const getApmConfig = (requestPath: string) => { const baseConfig = getConfiguration('kibana-frontend'); - if (!baseConfig?.active) { + if (!shouldInstrumentClient(baseConfig)) { return null; }