DEV Community

Cover image for Decorate the Python function - Metrics
Paweł Piwosz for AWS Community Builders

Posted on • Updated on


Decorate the Python function - Metrics


In last episode we instrumented our function with traces. Now we will do the same thing with metrics.

Decorate the handler


Like with traces we need to import proper functionality from the powertools library.

First, let's remember, in the SAM template we need to have two obligatory variables:

POWERTOOLS_SERVICE_NAME: simpleFunctionService
Enter fullscreen mode Exit fullscreen mode

POWERTOOLS_SERVICE_NAME we set previously. POWERTOOLS_METRICS_NAMESPACE gives us the possibility to collect all related metrics under one container. We can add custom dimensions later.

Currently we have this part of the code

from aws_lambda_powertools import Tracer

tracer = Tracer()
Enter fullscreen mode Exit fullscreen mode

Let's update it with metrics.

from aws_lambda_powertools import Tracer, Metrics

tracer = Tracer()
metrics = Metrics()
Enter fullscreen mode Exit fullscreen mode

At this moment we are ready to decorate the handler with metrics decorator:

def handler(event, context):
    """ A very simple Lambda function """
Enter fullscreen mode Exit fullscreen mode

All right, let's deploy it and let's take a look on the CloudWatch Metrics console!

As we can easily see, the new Custom Metrics appeared.

Custom metrics

Click the simpleFunctionMetrics, then click function_name, service (we should see that we have 1 metrics there). We can select ColdStart metrics now. We should see at least one coldstart is registered.

Custom metrics, coldstart

Looks perfect!

Custom metrics

Now it is the time to add the custom metrics. But before we start, please remember, if we add metrics outside the handler, in the global scope, it will be used during the cold start only! Anyway, we will test it to see how it works.

Import MetricUnit

According to documentation: MetricUnit enum facilitate finding a supported metric unit by CloudWatch. Alternatively, you can pass the value as a string if you already know them e.g. "Count".

We use this approach, therefore we need to import one more element from library.

from aws_lambda_powertools.metrics import MetricUnit
Enter fullscreen mode Exit fullscreen mode

Custom metrics for coldstart

Now, before we define any function, let's put this part of the code:

""" This is the coldstart metrics only"""
metrics.add_dimension(name="Execution", value="ColdStart")
metrics.add_metric(name="ColdStarted", unit=MetricUnit.Count, value=1)
Enter fullscreen mode Exit fullscreen mode

This will create a custom metrics which should be executed only during cold start.

Ok, after deployment I executed the function multiple times.

Let's take a look on CloudWatch Logs.

CloudWatch Logs for instrumented Lambda

As we can clearly see the idea of instrumenting the custom metrics for coldstart only is successsfull. We see multiple executions but only one is instrumented in logs.

Let's see the metrics then. When we go to CloudWatch Metrics, we can see 2 metrics there.

Custom metrics, coldstart

Let's click it. Now we have two dimensions. The default one, and the one we created - Executions.

Custom metrics, coldstart

Let's finally go inside and graph the metrics. I switched the display method to numbers as it shows the effect better.

Custom metrics, coldstart

Execution metrics

Now it is the time to instrument the functionality with custom metrics. What we want to achieve is to see how many times the specific function was executed.

Let's add this part

metrics.add_dimension(name="Execution", value="Functionality")
metrics.add_metric(name="Handler", unit=MetricUnit.Count, value=1)
Enter fullscreen mode Exit fullscreen mode

Into the handler, and this

metrics.add_metric(name="sumNumbers", unit=MetricUnit.Count, value=1)
Enter fullscreen mode Exit fullscreen mode

Into each function. Please remember to change the name for each function.

After deployment and a few executions we should see something like this:

Custom metrics

When we click...

Custom metrics

And click again...

Custom metrics

So, we have more metrics now!

Let's take a look on the CloudWatch Metrics dashboard now. The display is very similar like it was before, the only one thing which I changed was the aggreggation - from average to sum.

Custom metrics

Ok, after many executions this is what you can get through CloudWatch. It is not perfect. But we will work on it... later :)

Custom metrics

Cover image by Hebi B. from Pixabay

Top comments (0)

Super Useful CSS Resources

A collection of 70 hand-picked, web-based tools which are actually useful.
Each will generate pure CSS without the need for JS or any external libraries.