Skip to content

Bug: Metrics removing service name after flushing #1145

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

Closed
dreamorosi opened this issue Nov 8, 2022 · 2 comments · Fixed by #1146
Closed

Bug: Metrics removing service name after flushing #1145

dreamorosi opened this issue Nov 8, 2022 · 2 comments · Fixed by #1146
Assignees
Labels
bug Something isn't working completed This item is complete and has been merged/shipped metrics This item relates to the Metrics Utility

Comments

@dreamorosi
Copy link
Contributor

Expected Behaviour

As a user, when using the Metrics utility I want dimensions and metadata added during a function execution to be cleared when flushing. At the same time, I also want default dimensions and service name to be preserved.

Take the function code shown in the "code snippet" section below, then run it twice. The EMF logs emitted should look like this:

First invocation:

{
    "_aws": {
        "Timestamp": 1667915882250,
        "CloudWatchMetrics": [
            {
                "Namespace": "someNS",
                "Dimensions": [
                    [
                        "service", // <-- Service dimension is here
                        "foo"
                    ]
                ],
                "Metrics": []
            }
        ]
    },
    "service": "someName", // <-- Service value is here
    "foo": "bar"
}

Subsequent invocation:

{
    "_aws": {
        "Timestamp": 1667915928090,
        "CloudWatchMetrics": [
            {
                "Namespace": "someNS",
                "Dimensions": [
                    [
                        "service", // <-- Service dimension is still here
                        "foo"
                    ]
                ],
                "Metrics": []
            }
        ]
    },
    "service": "someName", // <-- Service is still here
    "foo": "bar"
}

Current Behaviour

After the changes made in #1129 (which correspond to v1.4.0), however the service name is cleared together with the non-default dimensions and metadata.

The function below, executed twice, emits these logs:

First invocation:

{
    "_aws": {
        "Timestamp": 1667915882250,
        "CloudWatchMetrics": [
            {
                "Namespace": "someNS",
                "Dimensions": [
                    [
                        "service", // <-- Service dimension is here
                        "foo"
                    ]
                ],
                "Metrics": []
            }
        ]
    },
    "service": "someName", // <-- Service value is here
    "foo": "bar"
}

Subsequent invocation:

{
    "_aws": {
        "Timestamp": 1667915928090,
        "CloudWatchMetrics": [
            {
                "Namespace": "someNS",
                "Dimensions": [
                    [
                        "foo"
                    ] <-- Service dimension is gone
                ],
                "Metrics": []
            }
        ]
    },
    "foo": "bar"
    // <-- Service value is also gone
}

Code snippet

import { Metrics, logMetrics } from "@aws-lambda-powertools/metrics";
import middy from "@middy/core";

const metrics = new Metrics({ serviceName: "someName", namespace: 'someNS' });

export const handler = middy(async () => {
  return true;
}).use(logMetrics(metrics, { captureColdStartMetric: true }));


Finally, run the function twice.

Possible Solution

The current Metrics implementation (in v1.4.0) looks like this:

public publishStoredMetrics(): void {
   const target = this.serializeMetrics();
   console.log(JSON.stringify(target));
   this.storedMetrics = {};
   this.clearMetrics();
   this.clearDimensions(); // <- this is causing the bug
   this.clearMetadata();
}

public clearDimensions(): void {
  this.dimensions = {};
}

The above means that every time metrics are flushed, stored metrics, dimensions, and metadata are cleared.

This is fine, however default dimensions and service name should be preserved.

Currently, dimensions and defaultDimensions are stored in two separate objects in a Metrics instance. The clearDimensions empties the former but leaves the latter untouched.

However, the service name is currently being stored in dimensions:

private setService(service: string | undefined): void {
  const targetService = (service ||
    this.getCustomConfigService()?.getServiceName() ||
    this.getEnvVarsService().getServiceName()) as string;
  if (targetService.length > 0) {
    this.addDimension('service', targetService);
  }
}

A potential fix would be to instead store the service name in the default dimensions, which would have the side effect of keeping it around when clearDimensions is called.

private setService(service: string | undefined): void {
  const targetService = (service ||
    this.getCustomConfigService()?.getServiceName() ||
    this.getEnvVarsService().getServiceName()) as string;
  if (targetService.length > 0) {
-    this.addDimension('service', targetService);
+   this.setDefaultDimensions({ service: targetService });
  }
}

An alternative solution that would allow to keep storing the service in dimensions would involve saving the value of serviceName passed to the utility constructor & resolving its value using the setService() method after any publishStoredMetrics() call.

Steps to Reproduce

Create a Lambda function using any Node.js runtime and use Powertools v1.4.0, use the code shown in the "Code snippet" section, then run twice.

AWS Lambda Powertools for TypeScript version

v1.4.0 (latest)

AWS Lambda function runtime

16.x

Packaging format used

Lambda Layers, Npm

Debugging logs

No response

@dreamorosi dreamorosi added bug Something isn't working metrics This item relates to the Metrics Utility triage This item has not been triaged by a maintainer, please wait labels Nov 8, 2022
@dreamorosi dreamorosi self-assigned this Nov 8, 2022
@dreamorosi dreamorosi removed the triage This item has not been triaged by a maintainer, please wait label Nov 8, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Nov 9, 2022

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release This item has been merged and will be released soon label Nov 9, 2022
@dreamorosi dreamorosi removed the pending-release This item has been merged and will be released soon label Nov 9, 2022
@dreamorosi
Copy link
Contributor Author

Fix released in v1.4.1.

@dreamorosi dreamorosi added the completed This item is complete and has been merged/shipped label Nov 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working completed This item is complete and has been merged/shipped metrics This item relates to the Metrics Utility
Projects
None yet
1 participant