Open
Description
Summary
The Metrics utility currently has inconsistent behavior when overwriting dimensions. The addDimension()
and addDimensions()
methods properly emit warnings when overwriting existing dimensions, but the setDefaultDimensions()
method silently overwrites existing default dimensions without any warning.
This creates an inconsistent user experience where some dimension overwrite scenarios provide helpful warnings while others do not.
Why is this needed?
- Consistency: All dimension overwrite scenarios should behave consistently by providing warnings
- Developer Experience: Warnings help developers catch potential bugs where they might accidentally overwrite important dimensions
- Alignment with existing patterns: The codebase already has the infrastructure and patterns for dimension overwrite warnings
- Parity with other methods:
addDimension()
andaddDimensions()
already implement this behavior successfully
Which area does this relate to?
Metrics
Solution
Add warning logic to the setDefaultDimensions()
method to detect when existing default dimensions are being overwritten and emit appropriate warning messages using the existing logger infrastructure.
The implementation should:
- Check if any keys in the new dimensions object already exist in
this.defaultDimensions
- Emit a warning message similar to the existing ones:
"Dimension \"${key}\" has already been added. The previous value will be overwritten."
- Use the existing
this.#logger.warn()
pattern for consistency
Test case that should pass after implementation:
it('warns when setDefaultDimensions overwrites existing dimensions', () => {
// Prepare
const metrics = new Metrics({
namespace: DEFAULT_NAMESPACE,
defaultDimensions: { environment: 'prod' },
});
// Act
metrics.setDefaultDimensions({ region: 'us-east-1' });
metrics.setDefaultDimensions({
environment: 'staging', // overwrites default dimension
});
// Assess
expect(console.warn).toHaveBeenCalledOnce();
expect(console.log).toHaveBeenCalledWith(
'Dimension "environment" has already been added. The previous value will be overwritten.'
);
});
Acknowledgment
- This request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Backlog