-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move src/apm.js to @kbn/apm-config-loader (#94315)
* Move src/apm.js to @kbn/apm-config-loader * add unit tests for `initApm` * return undefined instead of empty config * use ?.
- Loading branch information
1 parent
0c3514c
commit ed5bbda
Showing
12 changed files
with
169 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export const mockLoadConfiguration = jest.fn(); | ||
jest.doMock('./config_loader', () => ({ | ||
loadConfiguration: mockLoadConfiguration, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* 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 { mockLoadConfiguration } from './init_apm.test.mocks'; | ||
|
||
import { initApm } from './init_apm'; | ||
import apm from 'elastic-apm-node'; | ||
|
||
describe('initApm', () => { | ||
let apmAddFilterSpy: jest.SpyInstance; | ||
let apmStartSpy: jest.SpyInstance; | ||
let getConfig: jest.Mock; | ||
|
||
beforeEach(() => { | ||
apmAddFilterSpy = jest.spyOn(apm, 'addFilter').mockImplementation(() => undefined); | ||
apmStartSpy = jest.spyOn(apm, 'start').mockImplementation(() => undefined as any); | ||
getConfig = jest.fn(); | ||
|
||
mockLoadConfiguration.mockImplementation(() => ({ | ||
getConfig, | ||
})); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
mockLoadConfiguration.mockReset(); | ||
}); | ||
|
||
it('calls `loadConfiguration` with the correct options', () => { | ||
initApm(['foo', 'bar'], 'rootDir', true, 'service-name'); | ||
|
||
expect(mockLoadConfiguration).toHaveBeenCalledTimes(1); | ||
expect(mockLoadConfiguration).toHaveBeenCalledWith(['foo', 'bar'], 'rootDir', true); | ||
}); | ||
|
||
it('calls `apmConfigLoader.getConfig` with the correct options', () => { | ||
initApm(['foo', 'bar'], 'rootDir', true, 'service-name'); | ||
|
||
expect(getConfig).toHaveBeenCalledTimes(1); | ||
expect(getConfig).toHaveBeenCalledWith('service-name'); | ||
}); | ||
|
||
it('registers a filter using `addFilter`', () => { | ||
initApm(['foo', 'bar'], 'rootDir', true, 'service-name'); | ||
|
||
expect(apmAddFilterSpy).toHaveBeenCalledTimes(1); | ||
expect(apmAddFilterSpy).toHaveBeenCalledWith(expect.any(Function)); | ||
}); | ||
|
||
it('starts apm with the config returned from `getConfig`', () => { | ||
const config = { | ||
foo: 'bar', | ||
}; | ||
getConfig.mockReturnValue(config); | ||
|
||
initApm(['foo', 'bar'], 'rootDir', true, 'service-name'); | ||
|
||
expect(apmStartSpy).toHaveBeenCalledTimes(1); | ||
expect(apmStartSpy).toHaveBeenCalledWith(config); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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 { loadConfiguration } from './config_loader'; | ||
|
||
export const initApm = ( | ||
argv: string[], | ||
rootDir: string, | ||
isDistributable: boolean, | ||
serviceName: string | ||
) => { | ||
const apmConfigLoader = loadConfiguration(argv, rootDir, isDistributable); | ||
const apmConfig = apmConfigLoader.getConfig(serviceName); | ||
|
||
// we want to only load the module when effectively used | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const apm = require('elastic-apm-node'); | ||
|
||
// Filter out all user PII | ||
apm.addFilter((payload: Record<string, any>) => { | ||
try { | ||
if (payload.context?.user && typeof payload.context.user === 'object') { | ||
Object.keys(payload.context.user).forEach((key) => { | ||
payload.context.user[key] = '[REDACTED]'; | ||
}); | ||
} | ||
} catch (e) { | ||
// just silently ignore the error | ||
} | ||
return payload; | ||
}); | ||
|
||
apm.start(apmConfig); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
const { join } = require('path'); | ||
const { name, build } = require('../../package.json'); | ||
const { initApm } = require('@kbn/apm-config-loader'); | ||
|
||
const rootDir = join(__dirname, '../..'); | ||
const isKibanaDistributable = Boolean(build && build.distributable === true); | ||
|
||
module.exports = function (serviceName = name) { | ||
initApm(process.argv, rootDir, isKibanaDistributable, serviceName); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters