Notify New Item Function

Modify the application

Go back you your Cloud9 environment and open your app workspace at serverless-observability-workshop/code/sample-app-tracing.

Modify the Notify New Item Function

  1. Lambda doesn’t allow us to add custom annotations and metadata to its root segment, so we first need to create our custom subsegment by updating our handler.

  2. Edit the serverless-observability-workshop/code/sample-app-tracing/src/handlers/notify-item.js file to add an initial subsegment called ## Handler using the AWSXRay.captureAsyncFunc() method on the entire handler method and closing the subsegment inside a new finally clause in our try/catch.

    
    exports.notifyNewItemHandler = async (event, context) => {
      return AWSXRay.captureAsyncFunc('## Handler', async (subsegment) => {
          // Initialization
          try{
            // Happy Path
          } catch(err) {
            // Exception Handling
          } finally {
              subsegment.close()
          }
          return response
      }, AWSXRay.getSegment());
    }
    
  3. Next, we are ready to add our annotations in case of successful and failed executions to our given Item ID. Inside your handler, find and add in the end of your try and beginning of your catch statements the annotations for ItemID and Status:

        // Initialization
        try{
            // Happy Path
            //Tracing
            subsegment.addAnnotation('Status', 'SUCCESS')
        } catch(err) {
            // Exception Handling
            //Tracing
            subsegment.addAnnotation('Status', 'FAILED')
        }
    
  4. Next, let’s modify the getItem() method to receive the subsegment as a parameter and create an additional subsegment to capture any business logic inside this method.

    const getItem = async (record, segment) => {
        return AWSXRay.captureAsyncFunc('## subscribeSNSNewItem', async (subsegment) => {
            // Initialization
            try {
                // Happy Path
            } catch (err) {
                // Exception Handling
            } finally {
                subsegment.close()
            }
            return response
        }, segment);
    }
    
  5. Finally, modify the handler method to pass the subsegment to the getItem() method.

    response = await getItem(record, subsegment)
    
  6. Save your changes to the serverless-observability-workshop/code/sample-app-tracing/src/handlers/notify-item.js file.

Your entire file should look like the code below:

Fully modified file (expand for code)