Skip to content

Commit c3076a8

Browse files
authored
Prepare APM agent configuration for production use (#78697)
1 parent 6a173ba commit c3076a8

File tree

9 files changed

+60
-24
lines changed

9 files changed

+60
-24
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ describe('ApmConfiguration', () => {
4242
resetAllMocks();
4343
});
4444

45-
it('sets the correct service name', () => {
45+
it('sets the correct service name and version', () => {
4646
packageMock.raw = {
4747
version: '9.2.1',
4848
};
4949
const config = new ApmConfiguration(mockedRootDir, {}, false);
50-
expect(config.getConfig('myservice').serviceName).toBe('myservice-9_2_1');
50+
expect(config.getConfig('myservice').serviceName).toBe('myservice');
51+
expect(config.getConfig('myservice').serviceVersion).toBe('9.2.1');
5152
});
5253

5354
it('sets the git revision from `git rev-parse` command in non distribution mode', () => {

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@ const getDefaultConfig = (isDistributable: boolean): ApmAgentConfig => {
3030
return {
3131
active: false,
3232
globalLabels: {},
33+
// Do not use a centralized controlled config
34+
centralConfig: false,
35+
// Capture all exceptions that are not caught
36+
logUncaughtExceptions: true,
37+
// Can be performance intensive, disabling by default
38+
breakdownMetrics: false,
3339
};
3440
}
41+
3542
return {
3643
active: false,
3744
serverUrl: 'https://f1542b814f674090afd914960583265f.apm.us-central1.gcp.cloud.es.io:443',
@@ -60,14 +67,14 @@ export class ApmConfiguration {
6067
) {
6168
// eslint-disable-next-line @typescript-eslint/no-var-requires
6269
const { version, build } = require(join(this.rootDir, 'package.json'));
63-
this.kibanaVersion = version.replace(/\./g, '_');
70+
this.kibanaVersion = version;
6471
this.pkgBuild = build;
6572
}
6673

6774
public getConfig(serviceName: string): ApmAgentConfig {
6875
return {
6976
...this.getBaseConfig(),
70-
serviceName: `${serviceName}-${this.kibanaVersion}`,
77+
serviceName,
7178
};
7279
}
7380

@@ -76,7 +83,8 @@ export class ApmConfiguration {
7683
const apmConfig = merge(
7784
getDefaultConfig(this.isDistributable),
7885
this.getConfigFromKibanaConfig(),
79-
this.getDevConfig()
86+
this.getDevConfig(),
87+
this.getDistConfig()
8088
);
8189

8290
const rev = this.getGitRev();
@@ -88,6 +96,8 @@ export class ApmConfiguration {
8896
if (uuid) {
8997
apmConfig.globalLabels.kibana_uuid = uuid;
9098
}
99+
100+
apmConfig.serviceVersion = this.kibanaVersion;
91101
this.baseConfig = apmConfig;
92102
}
93103

@@ -123,6 +133,19 @@ export class ApmConfiguration {
123133
}
124134
}
125135

136+
/** Config keys that cannot be overridden in production builds */
137+
private getDistConfig(): ApmAgentConfig {
138+
if (!this.isDistributable) {
139+
return {};
140+
}
141+
142+
return {
143+
// Headers & body may contain sensitive info
144+
captureHeaders: false,
145+
captureBody: 'off',
146+
};
147+
}
148+
126149
private getGitRev() {
127150
if (this.isDistributable) {
128151
return this.pkgBuild.sha;

src/apm.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,22 @@ module.exports = function (serviceName = name) {
3636

3737
apmConfig = loadConfiguration(process.argv, ROOT_DIR, isKibanaDistributable);
3838
const conf = apmConfig.getConfig(serviceName);
39-
require('elastic-apm-node').start(conf);
39+
const apm = require('elastic-apm-node');
40+
41+
// Filter out all user PII
42+
apm.addFilter((payload) => {
43+
try {
44+
if (payload.context && payload.context.user && typeof payload.context.user === 'object') {
45+
Object.keys(payload.context.user).forEach((key) => {
46+
payload.context.user[key] = '[REDACTED]';
47+
});
48+
}
49+
} finally {
50+
return payload;
51+
}
52+
});
53+
54+
apm.start(conf);
4055
};
4156

4257
module.exports.getConfig = (serviceName) => {
@@ -50,4 +65,3 @@ module.exports.getConfig = (serviceName) => {
5065
}
5166
return {};
5267
};
53-
module.exports.isKibanaDistributable = isKibanaDistributable;

src/cli/cluster/cluster_manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export class ClusterManager {
110110
type: 'server',
111111
log: this.log,
112112
argv: serverArgv,
113+
apmServiceName: 'kibana',
113114
})),
114115
];
115116

src/cli/cluster/worker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ interface WorkerOptions {
4949
title?: string;
5050
watch?: boolean;
5151
baseArgv?: string[];
52+
apmServiceName?: string;
5253
}
5354

5455
export class Worker extends EventEmitter {
@@ -89,6 +90,7 @@ export class Worker extends EventEmitter {
8990
NODE_OPTIONS: process.env.NODE_OPTIONS || '',
9091
kbnWorkerType: this.type,
9192
kbnWorkerArgv: JSON.stringify([...(opts.baseArgv || baseArgv), ...(opts.argv || [])]),
93+
ELASTIC_APM_SERVICE_NAME: opts.apmServiceName || '',
9294
};
9395
}
9496

src/cli/dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
* under the License.
1818
*/
1919

20-
require('../apm')(process.env.ELASTIC_APM_PROXY_SERVICE_NAME || 'kibana-proxy');
20+
require('../apm')(process.env.ELASTIC_APM_SERVICE_NAME || 'kibana-proxy');
2121
require('../setup_node_env');
2222
require('./cli');

src/core/public/apm_system.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import type { InternalApplicationStart } from './application';
2828

2929
interface ApmConfig {
3030
// AgentConfigOptions is not exported from @elastic/apm-rum
31+
active?: boolean;
3132
globalLabels?: Record<string, string>;
3233
}
3334

@@ -39,10 +40,10 @@ export class ApmSystem {
3940
private readonly enabled: boolean;
4041
/**
4142
* `apmConfig` would be populated with relevant APM RUM agent
42-
* configuration if server is started with `ELASTIC_APM_ACTIVE=true`
43+
* configuration if server is started with elastic.apm.* config.
4344
*/
4445
constructor(private readonly apmConfig?: ApmConfig) {
45-
this.enabled = process.env.IS_KIBANA_DISTRIBUTABLE !== 'true' && apmConfig != null;
46+
this.enabled = apmConfig != null && !!apmConfig.active;
4647
}
4748

4849
async setup() {

src/legacy/server/config/schema.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const HANDLED_IN_NEW_PLATFORM = Joi.any().description(
2525
);
2626
export default () =>
2727
Joi.object({
28+
elastic: Joi.object({
29+
apm: HANDLED_IN_NEW_PLATFORM,
30+
}).default(),
31+
2832
pkg: Joi.object({
2933
version: Joi.string().default(Joi.ref('$version')),
3034
branch: Joi.string().default(Joi.ref('$branch')),

src/legacy/ui/apm/index.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,20 @@
1717
* under the License.
1818
*/
1919

20-
import { getConfig, isKibanaDistributable } from '../../../apm';
20+
import { getConfig } from '../../../apm';
2121
import agent from 'elastic-apm-node';
2222

23-
const apmEnabled = !isKibanaDistributable && process.env.ELASTIC_APM_ACTIVE === 'true';
24-
25-
export function apmImport() {
26-
return apmEnabled ? 'import { init } from "@elastic/apm-rum"' : '';
27-
}
28-
29-
export function apmInit(config) {
30-
return apmEnabled ? `init(${config})` : '';
31-
}
23+
const apmEnabled = getConfig()?.active;
3224

3325
export function getApmConfig(requestPath) {
3426
if (!apmEnabled) {
3527
return null;
3628
}
3729
const config = {
3830
...getConfig('kibana-frontend'),
39-
...{
40-
active: true,
41-
pageLoadTransactionName: requestPath,
42-
},
31+
pageLoadTransactionName: requestPath,
4332
};
33+
4434
/**
4535
* Get current active backend transaction to make distrubuted tracing
4636
* work for rendering the app

0 commit comments

Comments
 (0)