migrate from disableSend to contextPropagationOnly

This commit is contained in:
restrry 2021-11-05 13:09:58 +01:00
parent 73272b5b20
commit c25a31bba1
5 changed files with 77 additions and 36 deletions

View file

@ -38,10 +38,10 @@ export ELASTIC_APM_TRANSACTION_SAMPLE_RATE=0.1
if is_pr; then
if [[ "${GITHUB_PR_LABELS:-}" == *"ci:collect-apm"* ]]; then
export ELASTIC_APM_ACTIVE=true
export ELASTIC_APM_DISABLE_SEND=false
export ELASTIC_APM_CONTEXT_PROPAGATION_ONLY=false
else
export ELASTIC_APM_ACTIVE=true
export ELASTIC_APM_DISABLE_SEND=true
export ELASTIC_APM_CONTEXT_PROPAGATION_ONLY=true
fi
export CHECKS_REPORTER_ACTIVE=true
@ -59,7 +59,7 @@ if is_pr; then
export PR_TARGET_BRANCH="$GITHUB_PR_TARGET_BRANCH"
else
export ELASTIC_APM_ACTIVE=true
export ELASTIC_APM_DISABLE_SEND=false
export ELASTIC_APM_CONTEXT_PROPAGATION_ONLY=false
export CHECKS_REPORTER_ACTIVE=false
fi

View file

@ -90,7 +90,7 @@ describe('ApmConfiguration', () => {
"breakdownMetrics": true,
"captureSpanStackTraces": false,
"centralConfig": false,
"disableSend": true,
"contextPropagationOnly": true,
"environment": "development",
"globalLabels": Object {},
"logUncaughtExceptions": true,
@ -112,7 +112,7 @@ describe('ApmConfiguration', () => {
"captureHeaders": false,
"captureSpanStackTraces": false,
"centralConfig": false,
"disableSend": true,
"contextPropagationOnly": true,
"environment": "development",
"globalLabels": Object {
"git_rev": "sha",
@ -226,19 +226,19 @@ describe('ApmConfiguration', () => {
);
});
describe('disableSend', () => {
it('sets "active: true" and "disableSend: true" by default', () => {
describe('contextPropagationOnly', () => {
it('sets "active: true" and "contextPropagationOnly: true" by default', () => {
expect(new ApmConfiguration(mockedRootDir, {}, false).getConfig('serviceName')).toEqual(
expect.objectContaining({
active: true,
disableSend: true,
contextPropagationOnly: true,
})
);
expect(new ApmConfiguration(mockedRootDir, {}, true).getConfig('serviceName')).toEqual(
expect.objectContaining({
active: true,
disableSend: true,
contextPropagationOnly: true,
})
);
});
@ -248,7 +248,7 @@ describe('ApmConfiguration', () => {
elastic: {
apm: {
active: false,
disableSend: false,
contextPropagationOnly: false,
},
},
};
@ -258,7 +258,7 @@ describe('ApmConfiguration', () => {
).toEqual(
expect.objectContaining({
active: false,
disableSend: false,
contextPropagationOnly: false,
})
);
@ -267,12 +267,12 @@ describe('ApmConfiguration', () => {
).toEqual(
expect.objectContaining({
active: false,
disableSend: false,
contextPropagationOnly: false,
})
);
});
it('is "false" if "active:true" and "disableSend" is not specified', () => {
it('is "false" if "active: true" configured and "contextPropagationOnly" is not specified', () => {
const kibanaConfig = {
elastic: {
apm: {
@ -286,7 +286,7 @@ describe('ApmConfiguration', () => {
).toEqual(
expect.objectContaining({
active: true,
disableSend: false,
contextPropagationOnly: false,
})
);
@ -295,12 +295,12 @@ describe('ApmConfiguration', () => {
).toEqual(
expect.objectContaining({
active: true,
disableSend: false,
contextPropagationOnly: false,
})
);
});
it('is "true" if "active:false" and "disableSend" is not specified', () => {
it('throws if "active: false" set without configuring "contextPropagationOnly: false"', () => {
const kibanaConfig = {
elastic: {
apm: {
@ -309,12 +309,35 @@ describe('ApmConfiguration', () => {
},
};
expect(() =>
new ApmConfiguration(mockedRootDir, kibanaConfig, false).getConfig('serviceName')
).toThrowErrorMatchingInlineSnapshot(
`"APM is disabled, but context propagation is enabled. Please disable context propagation with contextPropagationOnly:false"`
);
expect(() =>
new ApmConfiguration(mockedRootDir, kibanaConfig, true).getConfig('serviceName')
).toThrowErrorMatchingInlineSnapshot(
`"APM is disabled, but context propagation is enabled. Please disable context propagation with contextPropagationOnly:false"`
);
});
it('does not throw if "active: false" and "contextPropagationOnly: false" configured', () => {
const kibanaConfig = {
elastic: {
apm: {
active: false,
contextPropagationOnly: false,
},
},
};
expect(
new ApmConfiguration(mockedRootDir, kibanaConfig, false).getConfig('serviceName')
).toEqual(
expect.objectContaining({
active: false,
disableSend: true,
contextPropagationOnly: false,
})
);
@ -323,7 +346,7 @@ describe('ApmConfiguration', () => {
).toEqual(
expect.objectContaining({
active: false,
disableSend: true,
contextPropagationOnly: false,
})
);
});

View file

@ -17,7 +17,7 @@ import type { AgentConfigOptions } from 'elastic-apm-node';
// https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html
const DEFAULT_CONFIG: AgentConfigOptions = {
active: true,
disableSend: true,
contextPropagationOnly: true,
environment: 'development',
logUncaughtExceptions: true,
globalLabels: {},
@ -72,6 +72,8 @@ export class ApmConfiguration {
private getBaseConfig() {
if (!this.baseConfig) {
const configFromSources = this.getConfigFromAllSources();
this.baseConfig = merge(
{
serviceVersion: this.kibanaVersion,
@ -80,9 +82,7 @@ export class ApmConfiguration {
this.getUuidConfig(),
this.getGitConfig(),
this.getCiConfig(),
this.getConfigFromKibanaConfig(),
this.getDevConfig(),
this.getConfigFromEnv()
configFromSources
);
/**
@ -113,13 +113,12 @@ export class ApmConfiguration {
if (process.env.ELASTIC_APM_ACTIVE === 'true') {
config.active = true;
config.disableSend = false;
}
if (process.env.ELASTIC_APM_DISABLE_SEND === 'true') {
config.disableSend = true;
} else if (process.env.ELASTIC_APM_DISABLE_SEND === 'false') {
config.disableSend = false;
if (process.env.ELASTIC_APM_CONTEXT_PROPAGATION_ONLY === 'true') {
config.contextPropagationOnly = true;
} else if (process.env.ELASTIC_APM_CONTEXT_PROPAGATION_ONLY === 'false') {
config.contextPropagationOnly = false;
}
if (process.env.ELASTIC_APM_ENVIRONMENT || process.env.NODE_ENV) {
@ -151,12 +150,7 @@ export class ApmConfiguration {
* default config.
*/
private getConfigFromKibanaConfig(): AgentConfigOptions {
const config: AgentConfigOptions = this.rawKibanaConfig?.elastic?.apm ?? {};
return {
...config,
disableSend: config.disableSend ?? (config.active === true ? false : true),
};
return this.rawKibanaConfig?.elastic?.apm ?? {};
}
/**
@ -262,4 +256,28 @@ export class ApmConfiguration {
return {};
}
}
/**
* Reads APM configuration from different sources and merges them together.
*/
private getConfigFromAllSources(): AgentConfigOptions {
const config = merge(
{},
this.getConfigFromKibanaConfig(),
this.getDevConfig(),
this.getConfigFromEnv()
);
if (config.active === false && config.contextPropagationOnly !== false) {
throw new Error(
'APM is disabled, but context propagation is enabled. Please disable context propagation with contextPropagationOnly:false'
);
}
if (config.active === true) {
config.contextPropagationOnly = config.contextPropagationOnly ?? false;
}
return config;
}
}

View file

@ -93,7 +93,7 @@ def withFunctionalTestEnv(List additionalEnvs = [], Closure closure) {
def corsTestServerPort = "64${parallelId}3"
// needed for https://github.com/elastic/kibana/issues/107246
def proxyTestServerPort = "64${parallelId}4"
def apmDisableSend = githubPr.isPr() ? "true" : "false"
def contextPropagationOnly = githubPr.isPr() ? "true" : "false"
withEnv([
"CI_GROUP=${parallelId}",
@ -110,7 +110,7 @@ def withFunctionalTestEnv(List additionalEnvs = [], Closure closure) {
"FLEET_PACKAGE_REGISTRY_PORT=${fleetPackageRegistryPort}",
"ALERTING_PROXY_PORT=${alertingProxyPort}",
"ELASTIC_APM_ACTIVE=true",
"ELASTIC_APM_DISABLE_SEND=${apmDisableSend}",
"ELASTIC_APM_CONTEXT_PROPAGATION_ONLY=${contextPropagationOnly}",
"ELASTIC_APM_TRANSACTION_SAMPLE_RATE=0.1",
] + additionalEnvs) {
closure()

View file

@ -32,7 +32,7 @@ export default async function ({ readConfigFile }: FtrConfigProviderContext) {
...functionalConfig.get('kbnTestServer'),
env: {
ELASTIC_APM_ACTIVE: 'true',
ELASTIC_APM_DISABLE_SEND: 'false',
ELASTIC_APM_CONTEXT_PROPAGATION_ONLY: 'false',
ELASTIC_APM_ENVIRONMENT: process.env.CI ? 'ci' : 'development',
ELASTIC_APM_TRANSACTION_SAMPLE_RATE: '1.0',
ELASTIC_APM_SERVER_URL: APM_SERVER_URL,