Skip to content

Feature request: Add multiple dimensets to the same Metrics instance #845

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
2 tasks done
hjgraca opened this issue Apr 8, 2025 · 0 comments · May be fixed by #884
Open
2 tasks done

Feature request: Add multiple dimensets to the same Metrics instance #845

hjgraca opened this issue Apr 8, 2025 · 0 comments · May be fixed by #884
Assignees
Labels
area/metrics Core metrics utility feature-request New or enhancements to existing features good first issue Good for newcomers

Comments

@hjgraca
Copy link
Contributor

hjgraca commented Apr 8, 2025

Use case

Add Support for multiple dimension sets for the same Metrics instance
This feature would allow users to gain more granular insights and comprehensive views of their applications by creating aggregating metrics across various dimensions.

Reference: aws-powertools/powertools-lambda-python#6198

Solution/User Experience

Add a new AddDimensions() method to the Metrics class that accepts a List<DimensionSet>

    /// <summary>
    ///     Adds multiple dimensions to memory.
    /// </summary>
    /// <param name="dimensions">Variable number of key-value pairs for dimensions.</param>
    public static void AddDimensions(params (string key, string value)[] dimensions)
    {
        if (dimensions == null || dimensions.Length == 0)
            return;
            
        foreach (var (key, value) in dimensions)
        {
            Instance.AddDimension(key, value);
        }
    }

Example:

Metrics.AddDimensions(("environment", "prod"), ("region", "us-west-2"));
Metrics.AddDimensions(("environment", "prod"));
Metrics.AddDimensions(("region", "us-west-2"));

Metrics.AddMetric("ExecutionTime", DateTime.Now, Unit.MILLISECONDS);

This will output

{
  "_aws": {
    "Timestamp": 1742467748165,
    "CloudWatchMetrics": [
      {
        "Namespace": "HelloWorldFunction",
        "Metrics": [
          {
            "Name": "ExecutionTime",
            "Unit": "Milliseconds"
          }
        ],
        "Dimensions": [
          [
            "Service"
          ],
          [
            "environment",
            "region"
          ],
          [
            "environment"
          ],
          [
            "region"
          ]
        ]
      }
    ]
  },
  "function_request_id": "c0e5afc1-c033-4aa6-9b13-26bfc324874b",
  "ExecutionTime": 0.0,
  "environment": "prod",
  "functionVersion": "$LATEST",
  "Service": "Powertools",
  "logStreamId": "$LATEST",
  "region": "us-west-2",
  "executionEnvironment": "AWS_Lambda_java17"
}

Notes:

  • 'AddDimension': will add a single dimension to the position to when it was called and will not update other dimensions. This is to keep the current behaviour as is.
  • 'Overriding values': override the latest set value for the same dimension key
    • Duplicate dimension sets are removed before being added to the end of the collection.
    • This ensures only latest dimension value is used as a target member on the root EMF node.
    • This operation is O(n^2), but acceptable given sets are capped at 30 dimensions

Alternative solutions

Acknowledgment

@hjgraca hjgraca added area/metrics Core metrics utility feature-request New or enhancements to existing features good first issue Good for newcomers labels Apr 8, 2025
@hjgraca hjgraca moved this to 📋 Backlog in Powertools for AWS Lambda (.NET) May 12, 2025
@hjgraca hjgraca moved this from 📋 Backlog to 🏗 In progress in Powertools for AWS Lambda (.NET) May 14, 2025
@hjgraca hjgraca self-assigned this May 14, 2025
@hjgraca hjgraca linked a pull request May 15, 2025 that will close this issue
7 tasks
@hjgraca hjgraca moved this from 🏗 In progress to 👀 In review in Powertools for AWS Lambda (.NET) May 15, 2025
@hjgraca hjgraca linked a pull request May 15, 2025 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/metrics Core metrics utility feature-request New or enhancements to existing features good first issue Good for newcomers
Projects
Status: 👀 In review
Development

Successfully merging a pull request may close this issue.

1 participant