Skip to content

fix(metrics): addDimensions() documentation and tests #3964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

matteofigus
Copy link
Member

@matteofigus matteofigus commented May 22, 2025

Fix: addDimensions() should create a set

Summary

Changes

This PR fixes issue #3777 where addDimensions() was incorrectly adding dimensions to the existing set instead of creating a new dimension set.

Changes made:

  1. Fixed the addDimensions() method in Metrics.ts to create a new dimension set rather than adding to existing dimensions
  2. Added a dimensionSets array to store multiple dimension sets
  3. Updated the metrics serialization logic to handle multiple dimension sets
  4. Added unit tests to verify the correct behavior of dimension sets
  5. Updated documentation to explain the correct behavior of addDimensions()

Documentation updates:

  • Added a new section in the metrics documentation explaining how to use dimension sets
  • Created a new code example showing how to use addDimensions() to create multiple dimension sets
  • Clarified that addDimensions() creates a new set rather than modifying existing dimensions

Issue number: #3777


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation metrics This item relates to the Metrics Utility tests PRs that add or change tests labels May 22, 2025
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label May 22, 2025
Copy link

boring-cyborg bot commented May 22, 2025

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

This comment was marked as outdated.

@github-actions github-actions bot added do-not-merge This item should not be merged need-issue This PR needs an issue before it can be reviewed/worked on further labels May 22, 2025

This comment was marked as outdated.

@github-actions github-actions bot added the bug Something isn't working label May 22, 2025
@dreamorosi dreamorosi removed do-not-merge This item should not be merged need-issue This PR needs an issue before it can be reviewed/worked on further labels May 22, 2025
@matteofigus
Copy link
Member Author

Thanks for catching this! I've reverted the types/index.ts file to its original state. This was an unintended change that happened during the implementation. The PR now only contains documentation updates and test fixes.

@dreamorosi dreamorosi linked an issue May 22, 2025 that may be closed by this pull request
@dreamorosi dreamorosi changed the title fix: addDimensions() documentation and tests fix(metrics): addDimensions() documentation and tests May 22, 2025
@matteofigus matteofigus marked this pull request as draft May 23, 2025 14:01
@dreamorosi
Copy link
Contributor

Hi @matteofigus, I'll pick up the review for this right after the release of Bedrock Agent Functions on Tue 06/03.

@matteofigus matteofigus force-pushed the fix/issue-3777-metrics-dimension-sets branch from 8f8454b to 6278aa8 Compare May 30, 2025 01:22
@matteofigus matteofigus marked this pull request as ready for review May 30, 2025 01:33
Copy link

Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes, and apologies for the delayed review. Got caught between PTO and other launches.

I've left a few comments, I think we're getting closer but we need to tweak the logic to align with the addDimension (singular) method.

I've also answered to your questions in the linked issue, I hope it'll clarify the doubts - apologies for the confusion there.

@@ -267,9 +273,19 @@ class Metrics extends Utility implements MetricsInterface {
* @param dimensions - An object with key-value pairs of dimensions
*/
public addDimensions(dimensions: Dimensions): void {
for (const [name, value] of Object.entries(dimensions)) {
this.addDimension(name, value);
const newDimensionKeys = Object.keys(dimensions).filter(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation doesn't match the addDimension method (nor the docstring above).

We are not checking if the dimension value is valid like we do in the other method, and when overwriting a dimension we don't let customers know.

Other than that I think it could work. Here's a suggestion:

const newDimensionSet: Dimensions = {};
for (const [key, value] of Object.entries(dimensions)) {
  if (!value) {
    this.#logger.warn(
      `The dimension ${key} doesn't meet the requirements and won't be added. Ensure the dimension name and value are non empty strings`
    );
    continue;
  }
  if (
    Object.hasOwn(this.dimensions, key) ||
    Object.hasOwn(this.defaultDimensions, key) ||
    Object.hasOwn(newDimensionSet, key)
  ) {
    this.#logger.warn(
      `Dimension "${key}" has already been added. The previous value will be overwritten.`
    );
  }
  newDimensionSet[key] = value;
  if (MAX_DIMENSION_COUNT <= this.getCurrentDimensionsCount()) {
    throw new RangeError(
      `The number of metric dimensions must be lower than ${MAX_DIMENSION_COUNT}`
    );
  }
}
this.dimensionSets.push(dimensions);

);

if (
newDimensionKeys.length + this.getCurrentDimensionsCount() >=
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also update the this.getCurrentDimensionsCount() method so that it factors in the this.dimensionSets - even if in a set, these dimensions still count against the total 30 allowed.


return {
_aws: {
Timestamp: this.#timestamp ?? new Date().getTime(),
CloudWatchMetrics: [
{
Namespace: this.namespace || DEFAULT_NAMESPACE,
Dimensions: [dimensionNames],
Dimensions: dimensionNames as [string[]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure about this change, was it intentional?

...this.dimensions,
...metricValues,
...this.metadata,
...Object.assign(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put back the spread operator (...)? With current implementation we're instantiating a new object (with Object.assing()) only to immediately spread it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation metrics This item relates to the Metrics Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: addDimensions() should create a set
2 participants