From ca86b98b5ae6bf3ff646289f79997f09deeb1b04 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 18 Nov 2021 10:19:15 -0500 Subject: [PATCH] Don't enable RUM agent if APM is run with contextPropagationOnly (#118685) (#118995) * do not enable RUM agent when nodejs is run in contextPropagationOnly mode * move shouldInstrumentClient to apm-config package Co-authored-by: Mikhail Shustov --- packages/kbn-apm-config-loader/src/index.ts | 1 + .../src/rum_agent_configuration.test.ts | 27 +++++++++++++++++++ .../src/rum_agent_configuration.ts | 14 ++++++++++ .../get_apm_config.test.mocks.ts | 2 ++ .../http_resources/get_apm_config.test.ts | 14 +++++----- .../server/http_resources/get_apm_config.ts | 4 +-- 6 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 packages/kbn-apm-config-loader/src/rum_agent_configuration.test.ts create mode 100644 packages/kbn-apm-config-loader/src/rum_agent_configuration.ts diff --git a/packages/kbn-apm-config-loader/src/index.ts b/packages/kbn-apm-config-loader/src/index.ts index b16f6dcfd418f1..381eb895b7eec8 100644 --- a/packages/kbn-apm-config-loader/src/index.ts +++ b/packages/kbn-apm-config-loader/src/index.ts @@ -8,4 +8,5 @@ export { getConfiguration } from './config_loader'; export { initApm } from './init_apm'; +export { shouldInstrumentClient } from './rum_agent_configuration'; export type { ApmConfiguration } from './config'; diff --git a/packages/kbn-apm-config-loader/src/rum_agent_configuration.test.ts b/packages/kbn-apm-config-loader/src/rum_agent_configuration.test.ts new file mode 100644 index 00000000000000..be4619578b5a44 --- /dev/null +++ b/packages/kbn-apm-config-loader/src/rum_agent_configuration.test.ts @@ -0,0 +1,27 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ +import { shouldInstrumentClient } from './rum_agent_configuration'; +describe('shouldInstrumentClient', () => { + it('returns false if apm is disabled', () => { + expect(shouldInstrumentClient({ active: false })).toBe(false); + }); + + it('returns false if apm is enabled with contextPropagationOnly: true', () => { + expect(shouldInstrumentClient({ active: true, contextPropagationOnly: true })).toBe(false); + }); + + it('returns false if apm is enabled with disableSend: true', () => { + expect(shouldInstrumentClient({ active: true, disableSend: true })).toBe(false); + }); + + it('returns true if apm is enabled', () => { + expect(shouldInstrumentClient({ active: true })).toBe(true); + expect(shouldInstrumentClient({ active: true, contextPropagationOnly: false })).toBe(true); + expect(shouldInstrumentClient({ active: true, disableSend: false })).toBe(true); + }); +}); diff --git a/packages/kbn-apm-config-loader/src/rum_agent_configuration.ts b/packages/kbn-apm-config-loader/src/rum_agent_configuration.ts new file mode 100644 index 00000000000000..5a73c95e21135b --- /dev/null +++ b/packages/kbn-apm-config-loader/src/rum_agent_configuration.ts @@ -0,0 +1,14 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ +import type { AgentConfigOptions } from 'elastic-apm-node'; + +export function shouldInstrumentClient(config?: AgentConfigOptions): boolean { + return Boolean( + config?.active === true && config.contextPropagationOnly !== true && config.disableSend !== true + ); +} diff --git a/src/core/server/http_resources/get_apm_config.test.mocks.ts b/src/core/server/http_resources/get_apm_config.test.mocks.ts index 8c3fa180a04f02..03d1a2575ba53a 100644 --- a/src/core/server/http_resources/get_apm_config.test.mocks.ts +++ b/src/core/server/http_resources/get_apm_config.test.mocks.ts @@ -7,8 +7,10 @@ */ export const getConfigurationMock = jest.fn(); +export const shouldInstrumentClientMock = jest.fn(() => true); jest.doMock('@kbn/apm-config-loader', () => ({ getConfiguration: getConfigurationMock, + shouldInstrumentClient: shouldInstrumentClientMock, })); export const agentMock = {} as Record; 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..9552a91da97b19 100644 --- a/src/core/server/http_resources/get_apm_config.test.ts +++ b/src/core/server/http_resources/get_apm_config.test.ts @@ -6,7 +6,11 @@ * Side Public License, v 1. */ -import { getConfigurationMock, agentMock } from './get_apm_config.test.mocks'; +import { + getConfigurationMock, + agentMock, + shouldInstrumentClientMock, +} from './get_apm_config.test.mocks'; import { getApmConfig } from './get_apm_config'; const defaultApmConfig = { @@ -17,6 +21,7 @@ const defaultApmConfig = { describe('getApmConfig', () => { beforeEach(() => { getConfigurationMock.mockReturnValue(defaultApmConfig); + shouldInstrumentClientMock.mockReturnValue(true); }); afterEach(() => { @@ -25,12 +30,7 @@ describe('getApmConfig', () => { }); it('returns null if apm is disabled', () => { - getConfigurationMock.mockReturnValue({ - active: false, - }); - expect(getApmConfig('/path')).toBeNull(); - - getConfigurationMock.mockReturnValue(undefined); + shouldInstrumentClientMock.mockReturnValue(false); expect(getApmConfig('/path')).toBeNull(); }); diff --git a/src/core/server/http_resources/get_apm_config.ts b/src/core/server/http_resources/get_apm_config.ts index 6ea172b162d285..3e7be65f966527 100644 --- a/src/core/server/http_resources/get_apm_config.ts +++ b/src/core/server/http_resources/get_apm_config.ts @@ -7,11 +7,11 @@ */ import agent from 'elastic-apm-node'; -import { getConfiguration } from '@kbn/apm-config-loader'; +import { getConfiguration, shouldInstrumentClient } from '@kbn/apm-config-loader'; export const getApmConfig = (requestPath: string) => { const baseConfig = getConfiguration('kibana-frontend'); - if (!baseConfig?.active) { + if (!shouldInstrumentClient(baseConfig)) { return null; }