TO DISCUSS. what if we enforce contextPropagationOnly to be configured when active is defined

This commit is contained in:
restrry 2021-11-05 13:22:28 +01:00
parent c25a31bba1
commit 62dda4fb27
3 changed files with 33 additions and 67 deletions

View file

@ -53,7 +53,7 @@ APM
https://www.elastic.co/guide/en/apm/agent/rum-js/current/index.html[Real
User Monitoring agent] is not available in the {kib} distributables,
however the agent can be enabled by setting `ELASTIC_APM_ACTIVE` to
`true`. flags
`true` and `ELASTIC_APM_CONTEXT_PROPAGATION_ONLY` to `false`. flags
....
ELASTIC_APM_ACTIVE=true yarn start

View file

@ -133,6 +133,7 @@ describe('ApmConfiguration', () => {
elastic: {
apm: {
active: true,
contextPropagationOnly: false,
serverUrl: 'https://url',
secretToken: 'secret',
},
@ -151,6 +152,7 @@ describe('ApmConfiguration', () => {
it('loads the configuration from the dev config is present', () => {
devConfigMock.raw = {
active: true,
contextPropagationOnly: false,
serverUrl: 'https://dev-url.co',
};
const config = new ApmConfiguration(mockedRootDir, {}, false);
@ -179,6 +181,7 @@ describe('ApmConfiguration', () => {
elastic: {
apm: {
active: true,
contextPropagationOnly: false,
serverUrl: 'https://url',
secretToken: 'secret',
},
@ -272,7 +275,29 @@ describe('ApmConfiguration', () => {
);
});
it('is "false" if "active: true" configured and "contextPropagationOnly" is not specified', () => {
it('throws if "active: false" set without configuring "contextPropagationOnly"', () => {
const kibanaConfig = {
elastic: {
apm: {
active: false,
},
},
};
expect(() =>
new ApmConfiguration(mockedRootDir, kibanaConfig, false).getConfig('serviceName')
).toThrowErrorMatchingInlineSnapshot(
`"[apm.active] is configured but contextPropagationOnly is not. Please, set [apm.contextPropagationOnly] value explicitly."`
);
expect(() =>
new ApmConfiguration(mockedRootDir, kibanaConfig, true).getConfig('serviceName')
).toThrowErrorMatchingInlineSnapshot(
`"[apm.active] is configured but contextPropagationOnly is not. Please, set [apm.contextPropagationOnly] value explicitly."`
);
});
it('throws if "active: true" set without configuring "contextPropagationOnly"', () => {
const kibanaConfig = {
elastic: {
apm: {
@ -281,73 +306,16 @@ describe('ApmConfiguration', () => {
},
};
expect(
new ApmConfiguration(mockedRootDir, kibanaConfig, false).getConfig('serviceName')
).toEqual(
expect.objectContaining({
active: true,
contextPropagationOnly: false,
})
);
expect(
new ApmConfiguration(mockedRootDir, kibanaConfig, true).getConfig('serviceName')
).toEqual(
expect.objectContaining({
active: true,
contextPropagationOnly: false,
})
);
});
it('throws if "active: false" set without configuring "contextPropagationOnly: false"', () => {
const kibanaConfig = {
elastic: {
apm: {
active: false,
},
},
};
expect(() =>
new ApmConfiguration(mockedRootDir, kibanaConfig, false).getConfig('serviceName')
).toThrowErrorMatchingInlineSnapshot(
`"APM is disabled, but context propagation is enabled. Please disable context propagation with contextPropagationOnly:false"`
`"[apm.active] is configured but contextPropagationOnly is not. Please, set [apm.contextPropagationOnly] value explicitly."`
);
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,
contextPropagationOnly: false,
})
);
expect(
new ApmConfiguration(mockedRootDir, kibanaConfig, true).getConfig('serviceName')
).toEqual(
expect.objectContaining({
active: false,
contextPropagationOnly: false,
})
`"[apm.active] is configured but contextPropagationOnly is not. Please, set [apm.contextPropagationOnly] value explicitly."`
);
});
});

View file

@ -113,6 +113,8 @@ export class ApmConfiguration {
if (process.env.ELASTIC_APM_ACTIVE === 'true') {
config.active = true;
} else if (process.env.ELASTIC_APM_ACTIVE === 'false') {
config.active = false;
}
if (process.env.ELASTIC_APM_CONTEXT_PROPAGATION_ONLY === 'true') {
@ -268,16 +270,12 @@ export class ApmConfiguration {
this.getConfigFromEnv()
);
if (config.active === false && config.contextPropagationOnly !== false) {
if (config.active !== undefined && config.contextPropagationOnly === undefined) {
throw new Error(
'APM is disabled, but context propagation is enabled. Please disable context propagation with contextPropagationOnly:false'
'[apm.active] is configured but contextPropagationOnly is not. Please, set [apm.contextPropagationOnly] value explicitly.'
);
}
if (config.active === true) {
config.contextPropagationOnly = config.contextPropagationOnly ?? false;
}
return config;
}
}