Skip to content

Commit bf9c9c5

Browse files
Merge branch 'master' into change-legend-palette-colors
2 parents 776b434 + ed5bbda commit bf9c9c5

File tree

12 files changed

+169
-69
lines changed

12 files changed

+169
-69
lines changed

packages/kbn-apm-config-loader/src/config_loader.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
import { getConfigurationFilePaths, getConfigFromFiles, applyConfigOverrides } from './utils';
1010
import { ApmConfiguration } from './config';
11+
import { ApmAgentConfig } from './types';
12+
13+
let apmConfig: ApmConfiguration | undefined;
1114

1215
/**
1316
* Load the APM configuration.
1417
*
1518
* @param argv the `process.argv` arguments
1619
* @param rootDir The root directory of kibana (where the sources and the `package.json` file are)
17-
* @param production true for production builds, false otherwise
20+
* @param isDistributable true for production builds, false otherwise
1821
*/
1922
export const loadConfiguration = (
2023
argv: string[],
@@ -24,5 +27,19 @@ export const loadConfiguration = (
2427
const configPaths = getConfigurationFilePaths(argv);
2528
const rawConfiguration = getConfigFromFiles(configPaths);
2629
applyConfigOverrides(rawConfiguration, argv);
27-
return new ApmConfiguration(rootDir, rawConfiguration, isDistributable);
30+
31+
apmConfig = new ApmConfiguration(rootDir, rawConfiguration, isDistributable);
32+
return apmConfig;
33+
};
34+
35+
export const getConfiguration = (serviceName: string): ApmAgentConfig | undefined => {
36+
// integration test runner starts a kibana server that import the module without initializing APM.
37+
// so we need to check initialization of the config.
38+
// note that we can't just load the configuration during this module's import
39+
// because jest IT are ran with `--config path-to-jest-config.js` which conflicts with the CLI's `config` arg
40+
// causing the config loader to try to load the jest js config as yaml and throws.
41+
if (apmConfig) {
42+
return apmConfig.getConfig(serviceName);
43+
}
44+
return undefined;
2845
};

packages/kbn-apm-config-loader/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
export { loadConfiguration } from './config_loader';
9+
export { getConfiguration } from './config_loader';
10+
export { initApm } from './init_apm';
1011
export type { ApmConfiguration } from './config';
1112
export type { ApmAgentConfig } from './types';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
export const mockLoadConfiguration = jest.fn();
10+
jest.doMock('./config_loader', () => ({
11+
loadConfiguration: mockLoadConfiguration,
12+
}));
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import { mockLoadConfiguration } from './init_apm.test.mocks';
10+
11+
import { initApm } from './init_apm';
12+
import apm from 'elastic-apm-node';
13+
14+
describe('initApm', () => {
15+
let apmAddFilterSpy: jest.SpyInstance;
16+
let apmStartSpy: jest.SpyInstance;
17+
let getConfig: jest.Mock;
18+
19+
beforeEach(() => {
20+
apmAddFilterSpy = jest.spyOn(apm, 'addFilter').mockImplementation(() => undefined);
21+
apmStartSpy = jest.spyOn(apm, 'start').mockImplementation(() => undefined as any);
22+
getConfig = jest.fn();
23+
24+
mockLoadConfiguration.mockImplementation(() => ({
25+
getConfig,
26+
}));
27+
});
28+
29+
afterEach(() => {
30+
jest.restoreAllMocks();
31+
mockLoadConfiguration.mockReset();
32+
});
33+
34+
it('calls `loadConfiguration` with the correct options', () => {
35+
initApm(['foo', 'bar'], 'rootDir', true, 'service-name');
36+
37+
expect(mockLoadConfiguration).toHaveBeenCalledTimes(1);
38+
expect(mockLoadConfiguration).toHaveBeenCalledWith(['foo', 'bar'], 'rootDir', true);
39+
});
40+
41+
it('calls `apmConfigLoader.getConfig` with the correct options', () => {
42+
initApm(['foo', 'bar'], 'rootDir', true, 'service-name');
43+
44+
expect(getConfig).toHaveBeenCalledTimes(1);
45+
expect(getConfig).toHaveBeenCalledWith('service-name');
46+
});
47+
48+
it('registers a filter using `addFilter`', () => {
49+
initApm(['foo', 'bar'], 'rootDir', true, 'service-name');
50+
51+
expect(apmAddFilterSpy).toHaveBeenCalledTimes(1);
52+
expect(apmAddFilterSpy).toHaveBeenCalledWith(expect.any(Function));
53+
});
54+
55+
it('starts apm with the config returned from `getConfig`', () => {
56+
const config = {
57+
foo: 'bar',
58+
};
59+
getConfig.mockReturnValue(config);
60+
61+
initApm(['foo', 'bar'], 'rootDir', true, 'service-name');
62+
63+
expect(apmStartSpy).toHaveBeenCalledTimes(1);
64+
expect(apmStartSpy).toHaveBeenCalledWith(config);
65+
});
66+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import { loadConfiguration } from './config_loader';
10+
11+
export const initApm = (
12+
argv: string[],
13+
rootDir: string,
14+
isDistributable: boolean,
15+
serviceName: string
16+
) => {
17+
const apmConfigLoader = loadConfiguration(argv, rootDir, isDistributable);
18+
const apmConfig = apmConfigLoader.getConfig(serviceName);
19+
20+
// we want to only load the module when effectively used
21+
// eslint-disable-next-line @typescript-eslint/no-var-requires
22+
const apm = require('elastic-apm-node');
23+
24+
// Filter out all user PII
25+
apm.addFilter((payload: Record<string, any>) => {
26+
try {
27+
if (payload.context?.user && typeof payload.context.user === 'object') {
28+
Object.keys(payload.context.user).forEach((key) => {
29+
payload.context.user[key] = '[REDACTED]';
30+
});
31+
}
32+
} catch (e) {
33+
// just silently ignore the error
34+
}
35+
return payload;
36+
});
37+
38+
apm.start(apmConfig);
39+
};

src/apm.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

src/cli/apm.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
const { join } = require('path');
10+
const { name, build } = require('../../package.json');
11+
const { initApm } = require('@kbn/apm-config-loader');
12+
13+
const rootDir = join(__dirname, '../..');
14+
const isKibanaDistributable = Boolean(build && build.distributable === true);
15+
16+
module.exports = function (serviceName = name) {
17+
initApm(process.argv, rootDir, isKibanaDistributable, serviceName);
18+
};

src/cli/dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
require('../apm')(process.env.ELASTIC_APM_SERVICE_NAME || 'kibana-proxy');
9+
require('./apm')(process.env.ELASTIC_APM_SERVICE_NAME || 'kibana-proxy');
1010
require('../setup_node_env');
1111
require('../setup_node_env/root');
1212
require('./cli');

src/cli/dist.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Side Public License, v 1.
77
*/
88

9-
require('../apm')();
9+
require('./apm')();
1010
require('../setup_node_env/dist');
1111
require('../setup_node_env/root');
1212
require('./cli');

src/core/server/http_resources/get_apm_config.test.mocks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* Side Public License, v 1.
77
*/
88

9-
export const getConfigMock = jest.fn();
10-
jest.doMock('../../../apm', () => ({
11-
getConfig: getConfigMock,
9+
export const getConfigurationMock = jest.fn();
10+
jest.doMock('@kbn/apm-config-loader', () => ({
11+
getConfiguration: getConfigurationMock,
1212
}));
1313

1414
export const agentMock = {} as Record<string, any>;

0 commit comments

Comments
 (0)