fix(metrics): detect key collisions between dimensions, metrics, and metadata#5240
fix(metrics): detect key collisions between dimensions, metrics, and metadata#5240Zelys-DFKH wants to merge 1 commit into
Conversation
|
|
Thanks for tackling this! Two pieces of feedback: 1. Only throw on type changes Same-type collisions (dimension ↔ metadata, both strings) overwrite silently but don't corrupt the EMF schema — CloudWatch only rejects when a key flips between string and number. Throwing on same-type clashes is stricter than the spec requires and inconsistent with 2. Centralise the check in The current guards in
Reverse the call order in either of the existing tests and the corruption comes back. Rather than adding guards to every mutator, // inside serializeMetrics(), just before the return
type Source = 'dimension' | 'default dimension' | 'dimension set' | 'metadata';
const stringKeys = new Map<string, Source>();
for (const k of Object.keys(defaultDimensions)) stringKeys.set(k, 'default dimension');
for (const k of Object.keys(dimensions)) stringKeys.set(k, 'dimension');
for (const set of dimensionSets) for (const k of Object.keys(set)) stringKeys.set(k, 'dimension set');
for (const k of Object.keys(this.#metadataStore.getAll())) stringKeys.set(k, 'metadata');
for (const name of Object.keys(metricValues)) {
const source = stringKeys.get(name);
if (source) {
throw new Error(
`EMF key collision on "${name}": registered as both a metric (number) and a ${source} (string)`
);
}
} |



Summary
Changes
Closes #5208. In
serializeMetrics(), EMF output is assembled by spreading default dimensions, dimensions, dimension sets, metric values, and metadata in that order. When keys match across those layers, later values win silently. Two of those collisions break the EMF schema:What changed
Metrics.ts: Added a guard instoreMetric()that throws when the metric name matches any current dimension key (flat dimensions, default dimensions, or dimension sets), and a guard inaddMetadata()that throws when the metadata key matches any current metric name. Both error messages include the key name.dimensions.test.ts: Four new tests covering a regular dimension conflict, a default dimension conflict, a dimension-set conflict (viaaddDimensions), and the built-inservicedimension.metadata.test.ts: One new test covering a metadata-metric name conflict.Issue number: Closes #5208
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.