Don't enable RUM agent if APM is run with contextPropagationOnly (#118685)

* do not enable RUM agent when nodejs is run in contextPropagationOnly mode

* move shouldInstrumentClient to apm-config package
This commit is contained in:
Mikhail Shustov 2021-11-18 09:30:36 +01:00 committed by GitHub
parent df8ddeeebb
commit 8a16b849f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 9 deletions

View file

@ -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';

View file

@ -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);
});
});

View file

@ -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
);
}

View file

@ -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<string, any>;

View file

@ -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();
});

View file

@ -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;
}