Skip to content

Commit 180362b

Browse files
author
Tyler Smalley
committed
[CI] Preparation for APM tracking on CI
Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co>
1 parent 82ba41c commit 180362b

File tree

3 files changed

+70
-22
lines changed

3 files changed

+70
-22
lines changed

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import {
2828

2929
import { ApmConfiguration } from './config';
3030

31+
const initialEnv = { ...process.env };
32+
3133
describe('ApmConfiguration', () => {
3234
beforeEach(() => {
3335
packageMock.raw = {
@@ -39,6 +41,7 @@ describe('ApmConfiguration', () => {
3941
});
4042

4143
afterEach(() => {
44+
process.env = { ...initialEnv };
4245
resetAllMocks();
4346
});
4447

@@ -90,13 +93,16 @@ describe('ApmConfiguration', () => {
9093
let config = new ApmConfiguration(mockedRootDir, {}, false);
9194
expect(config.getConfig('serviceName')).toEqual(
9295
expect.objectContaining({
93-
serverUrl: expect.any(String),
94-
secretToken: expect.any(String),
96+
breakdownMetrics: true,
9597
})
9698
);
9799

98100
config = new ApmConfiguration(mockedRootDir, {}, true);
99-
expect(Object.keys(config.getConfig('serviceName'))).not.toContain('serverUrl');
101+
expect(config.getConfig('serviceName')).toEqual(
102+
expect.objectContaining({
103+
breakdownMetrics: false,
104+
})
105+
);
100106
});
101107

102108
it('loads the configuration from the kibana config file', () => {
@@ -156,4 +162,32 @@ describe('ApmConfiguration', () => {
156162
})
157163
);
158164
});
165+
166+
it('sets correctly sets environment', () => {
167+
delete process.env.ELASTIC_APM_ENVIRONMENT;
168+
delete process.env.NODE_ENV;
169+
170+
let config = new ApmConfiguration(mockedRootDir, {}, false);
171+
expect(config.getConfig('serviceName')).toEqual(
172+
expect.objectContaining({
173+
environment: 'development',
174+
})
175+
);
176+
177+
process.env.NODE_ENV = 'production';
178+
config = new ApmConfiguration(mockedRootDir, {}, false);
179+
expect(config.getConfig('serviceName')).toEqual(
180+
expect.objectContaining({
181+
environment: 'production',
182+
})
183+
);
184+
185+
process.env.ELASTIC_APM_ENVIRONMENT = 'ci';
186+
config = new ApmConfiguration(mockedRootDir, {}, false);
187+
expect(config.getConfig('serviceName')).toEqual(
188+
expect.objectContaining({
189+
environment: 'ci',
190+
})
191+
);
192+
});
159193
});

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,26 @@ import { readFileSync } from 'fs';
2626
import { ApmAgentConfig } from './types';
2727

2828
const getDefaultConfig = (isDistributable: boolean): ApmAgentConfig => {
29-
if (isDistributable) {
30-
return {
31-
active: false,
32-
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,
39-
};
40-
}
41-
29+
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html
4230
return {
43-
active: false,
44-
serverUrl: 'https://f1542b814f674090afd914960583265f.apm.us-central1.gcp.cloud.es.io:443',
31+
active: process.env.ELASTIC_APM_ACTIVE || false,
32+
environment: process.env.ELASTIC_APM_ENVIRONMENT || process.env.NODE_ENV || 'development',
33+
34+
serverUrl: 'https://b1e3b4b4233e44cdad468c127d0af8d8.apm.europe-west1.gcp.cloud.es.io:443',
35+
4536
// The secretToken below is intended to be hardcoded in this file even though
4637
// it makes it public. This is not a security/privacy issue. Normally we'd
4738
// instead disable the need for a secretToken in the APM Server config where
4839
// the data is transmitted to, but due to how it's being hosted, it's easier,
4940
// for now, to simply leave it in.
50-
secretToken: 'R0Gjg46pE9K9wGestd',
41+
secretToken: '2OyjjaI6RVkzx2O5CV',
42+
43+
logUncaughtExceptions: true,
5144
globalLabels: {},
52-
breakdownMetrics: true,
5345
centralConfig: false,
54-
logUncaughtExceptions: true,
46+
47+
// Can be performance intensive, disabling by default
48+
breakdownMetrics: isDistributable ? false : true,
5549
};
5650
};
5751

@@ -84,7 +78,8 @@ export class ApmConfiguration {
8478
getDefaultConfig(this.isDistributable),
8579
this.getConfigFromKibanaConfig(),
8680
this.getDevConfig(),
87-
this.getDistConfig()
81+
this.getDistConfig(),
82+
this.getCIConfig()
8883
);
8984

9085
const rev = this.getGitRev();
@@ -146,6 +141,21 @@ export class ApmConfiguration {
146141
};
147142
}
148143

144+
private getCIConfig(): ApmAgentConfig {
145+
if (process.env.ELASTIC_APM_ENVIRONMENT !== 'ci') {
146+
return {};
147+
}
148+
149+
return {
150+
globalLabels: {
151+
branch: process.env.ghprbSourceBranch || '',
152+
targetBranch: process.env.ghprbTargetBranch || '',
153+
ciJobName: process.env.JOB_NAME || '',
154+
ciBuildNumber: process.env.BUILD_NUMBER || '',
155+
},
156+
};
157+
}
158+
149159
private getGitRev() {
150160
if (this.isDistributable) {
151161
return this.pkgBuild.sha;

src/dev/ci_setup/setup_env.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export NODE_OPTIONS="$NODE_OPTIONS --max-old-space-size=4096"
2424
###
2525
export FORCE_COLOR=1
2626

27+
### APM tracking
28+
###
29+
export ELASTIC_APM_ENVIRONMENT=ci
30+
2731
###
2832
### check that we seem to be in a kibana project
2933
###

0 commit comments

Comments
 (0)