-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move src/apm.js to @kbn/apm-config-loader #94315
Merged
pgayvallet
merged 6 commits into
elastic:master
from
pgayvallet:kbn-93823-move-apm-script
Mar 14, 2021
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c89030
Move src/apm.js to @kbn/apm-config-loader
pgayvallet 16bcc2b
add unit tests for `initApm`
pgayvallet 331c6f5
Merge remote-tracking branch 'upstream/master' into kbn-93823-move-ap…
pgayvallet a6ae126
Merge remote-tracking branch 'upstream/master' into kbn-93823-move-ap…
pgayvallet 850bb49
return undefined instead of empty config
pgayvallet 79cbb13
use ?.
pgayvallet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '../..'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. optional: we have a package for everything: import { REPO_ROOT, kibanaPackageJson } from '@kbn/utils'; |
||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understood correctly, we use this same agent for the UI. However, this is the
elastic-apm-node
package, which is different from the@elastic/apm-rum
used on the UI.Are we sure that we can reuse the same agent initialization for both: server and public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is only executed on the server-side.
We're not using the same agent on server and browser (apm agent vs rum agent), we're just sharing the same configuration and factorizing the configuration loading logic (the RUM config is then sent to the client in the injected metadatas).
Note that nothing changed in this PR in term of design/architecture, we just moved the script to a package to resolve the cyclic dependency between core and cli.