Modifying the Application Code

In this step, we are going to define a couple of metrics that we want to capture amongst our three core services, and will instrument the putMetric() method to push them synchronously to CloudWatch Metrics.

Defining Metrics

Let’s define the following Business & Operational metrics:

  • ColdStart
  • UnsupportedHTTPMethod
  • SuccessfulPutItem
  • FailedPutItem

Modifying the Put Item Function

Importing Dependencies

  1. Open the Lambda function located on /serverless-observability-workshop/code/sample-app/src/handlers/put-item.js and start by importing the required MetricUnit and putMetric dependencies in the beginning of the file and initializing a _cold_start variable to capture cold starts in our lambda executions:

    const AWS = require('aws-sdk')
    const docClient = new AWS.DynamoDB.DocumentClient()
    const { MetricUnit } = require('../lib/helper/models')
    const { putMetric } = require('../lib/logging/logger')
    
    let _cold_start = true
    

    Adding PutMetric

  2. Now, inside the putItemHandler() we are going to test whether it’s the first execution of a given Lambda container and label it as Cold Start, also pushing this information as a CloudWatch Metric using our putMetric() method. Add and if statement checking whether the _cold_start variable is true or false right after the beginning of the try block.

    exports.putItemHandler = async (event, context) => {
        let response
        try {
            if (_cold_start) {
                //Metrics
                await putMetric(name = 'ColdStart', unit = MetricUnit.Count, value = 1, { service: 'item_service', function_name: context.functionName })
                _cold_start = false
            }
    
  3. Now, we are going to capture metrics for UnsupportedHTTPMethod, instrumenting the putMetric() method call if our if (event.httpMethod !== 'POST') evaluates true.

        if (event.httpMethod !== 'POST') {
            await putMetric(name = 'UnsupportedHTTPMethod', unit = MetricUnit.Count, value = 1, { service: 'item_service', operation: 'put-item' })
            throw new Error(`PutItem only accept POST method, you tried: ${event.httpMethod}`)
        }
    
    
  4. Next, we are ready to add our business metrics for successful and failed item insertions. Still inside your putItemHandler(), find and add in the end of your try and beginning of your catch blocks the metrics for SuccessfulPutItem and FailedPutItem:

        try{
            //After Sucessful Response Composition
            //Metrics
            await putMetric(name = 'SuccessfulPutItem', unit = MetricUnit.Count, value = 1, { service: 'item_service', operation: 'put-item' })
        } catch (err) {
            //After Exception Handling
            //Metrics
            await putMetric(name = 'FailedPutItem', unit = MetricUnit.Count, value = 1, { service: 'item_service', operation: 'put-item' })
        }
    
  5. Save your changes to the serverless-observability-workshop/code/sample-app/src/handlers/put-item.js file.

    Your putItemHandler method should look like the one below

    Full putItemHandler method (expand for code)

Deploy the application

cd ~/environment/serverless-observability-workshop/code/sample-app
sam build && sam deploy