aws-powertools / powertools-lambda-java

Powertools is a developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/java/
MIT No Attribution
285 stars 88 forks source link

Powertools-Metrics not working with Lambda Handlder #1128

Closed mahedi99 closed 1 year ago

mahedi99 commented 1 year ago

I have custom dashboard setup with a few widget. Now, I am trying to push CloudWatch metrics with the same namespace name, metrics name and dimension name inside a lambda handler function. However, even though it does not raise any error, it fails to record any value for the putMetric() call. Example code below.

MetricsLogger metricsLogger = MetricsUtils.metricsLogger();

@Metrics(namespace = "mynamespace")
public Void handleRequest(final SQSEvent event, final Context context) {
        metricsLogger.putDimensions(DimensionSet.of("mydimension", "success"));
        metricsLogger.putMetric("testmetric", 1, Unit.COUNT);
}

Expected Behavior

One of the widgets is recording and visualizing all the putMetric() calls. For all the putMetric() calls it should record and reflect on the custom dashboard widget.

Current Behavior

If I call metricsLogger.putMetric(), it executes successfully without raising any error. However, there's no metric value recorded in the dashboard.

Steps to Reproduce (for bugs)

  1. Write a lambda handler function with @Metrics annotation.
  2. Add a custom namespace. eg. @Metrics(namespace = "mynamespace")
  3. Declare metriclogger instance. eg. MetricsLogger metricsLogger = MetricsUtils.metricsLogger();
  4. Execute below api calls: metricsLogger.putDimensions(DimensionSet.of("mydimension", "success")); metricsLogger.putMetric("testmetric", 1, Unit.COUNT);
  5. In response, it won't record anything on the dashboard.

Environment

jeromevdl commented 1 year ago

Hi @mahedi99,

Thanks for reporting this.

Quick question: are you able to find the metrics somewhere in Cloudwatch Metrics (outside of your dashboard). If you go to CloudWatch Metrics / All metrics (in the left menu), are you able to see your custom namespace and navigate to the underlying metrics ?

I want to understand if the metric is pushed to CloudWatch properly or no, and if that could be a misconfiguration in the dashboard, because of the namespace or something not properly documented on our side.

jeromevdl commented 1 year ago

@mahedi99, any update on this ? We'll soon close the ticket if we don't hear from you.

GabrielAndreiPreda commented 1 year ago

Currently facing the same problem. My guess is that the metrics don't get flushed automatically as the docs for the Metrics annotation say, if I flush the metricsLogger manually I can see them in the logs. Not sure if it's supposed to automatically upload the metrics too, that doesn't happen for me at all, I've checked all metrics in Cloudwatch. The lambda function i'm running powertools on has full access to Cloudwatch actions. Same environment but the Powertools version I'm using is 1.16

jeromevdl commented 1 year ago

Thanks for the feedback. Can you share your pom / gradle config ? I suspect a misconfiguration on aspectJ...

GabrielAndreiPreda commented 1 year ago

That was absolutely on point, thanks for the speedy response. Didn't do the logging setup myself so I assumed I just have to add the package for metrics, newbie blunder. I guess it's safe to assume the original issue was also caused by this. Here's how the plugin from the build section should look like in case anyone stumbles upon this ticket:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.14.0</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <complianceLevel>1.8</complianceLevel>
        <forceAjcCompile>true</forceAjcCompile>
        <sources/>
        <weaveDirectories>
            <weaveDirectory>${project.build.directory}/classes</weaveDirectory>
        </weaveDirectories>
        <aspectLibraries>
            <aspectLibrary>
                <groupId>software.amazon.lambda</groupId>
                <artifactId>powertools-logging</artifactId>
            </aspectLibrary>
            <aspectLibrary>
                <groupId>software.amazon.lambda</groupId>
                <artifactId>powertools-metrics</artifactId>
            </aspectLibrary>
        </aspectLibraries>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

This is also considering that I use Lombok in the project

jeromevdl commented 1 year ago

Yes, you need to add each module that provides annotations in this <aspectLibraries> bloc (logging, metrics, tracing, idempotency, validation, sqs, ...), the one you use obviously. In this case, this is metrics. Thanks for confirming and here is the reference documentation.