Skip to content

Maintenance: Add warning for overwriting dimensions in setDefaultDimensions method #4134

@dreamorosi

Description

@dreamorosi

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?

  1. Consistency: All dimension overwrite scenarios should behave consistently by providing warnings
  2. Developer Experience: Warnings help developers catch potential bugs where they might accidentally overwrite important dimensions
  3. Alignment with existing patterns: The codebase already has the infrastructure and patterns for dimension overwrite warnings
  4. Parity with other methods: addDimension() and addDimensions() 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

confirmedThe scope is clear, ready for implementationfeature-requestThis item refers to a feature request for an existing or new utilitymetricsThis item relates to the Metrics Utility

Type

No type

Projects

Status

Pending review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions