Skip to content

Commit

Permalink
fix(apm): flatten globalLabels object (elastic#186579)
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo authored Jun 21, 2024
1 parent 9ed2ad9 commit aaf8914
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/kbn-apm-config-loader/src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,35 @@ describe('ApmConfiguration', () => {
);
});

it('flattens the `globalLabels` object', () => {
const kibanaConfig = {
elastic: {
apm: {
globalLabels: {
keyOne: 'k1',
objectOne: {
objectOneKeyOne: 'o1k1',
objectOneKeyTwo: {
objectOneKeyTwoSubkeyOne: 'o1k2s1',
},
},
},
},
},
};
const config = new ApmConfiguration(mockedRootDir, kibanaConfig, true);
expect(config.getConfig('serviceName')).toEqual(
expect.objectContaining({
globalLabels: {
git_rev: 'sha',
keyOne: 'k1',
'objectOne.objectOneKeyOne': 'o1k1',
'objectOne.objectOneKeyTwo.objectOneKeyTwoSubkeyOne': 'o1k2s1',
},
})
);
});

describe('env vars', () => {
beforeEach(() => {
delete process.env.ELASTIC_APM_ENVIRONMENT;
Expand Down
10 changes: 10 additions & 0 deletions packages/kbn-apm-config-loader/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getDataPath } from '@kbn/utils';
import { readFileSync } from 'fs';
import type { AgentConfigOptions } from 'elastic-apm-node';
import type { AgentConfigOptions as RUMAgentConfigOptions } from '@elastic/apm-rum';
import { getFlattenedObject } from '@kbn/std';
import type { ApmConfigSchema } from './apm_config';

// https://www.elastic.co/guide/en/apm/agent/nodejs/current/configuration.html
Expand Down Expand Up @@ -129,6 +130,15 @@ export class ApmConfiguration {
) {
this.baseConfig = merge(this.baseConfig, centralizedConfig);
}

if (this.baseConfig?.globalLabels) {
// Global Labels need to be a key/value pair...
// Dotted names will be renamed to underscored ones by the agent, but we need to provide key/value pairs
// https://github.com/elastic/apm-agent-nodejs/issues/4096#issuecomment-2181621221
this.baseConfig.globalLabels = getFlattenedObject(
this.baseConfig.globalLabels as Record<string, unknown>
);
}
}

return this.baseConfig;
Expand Down

0 comments on commit aaf8914

Please sign in to comment.