How to monitor your system dependencies
Tags: go, devops, aws, tutorial
Published: 2021-01-03 18:03:46

---
title: How to monitor your system dependencies
published: true
description: A simple trick to publish metrics for all your dependencies
tags: go, devops, aws, tutorial
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/pwg2jy2p7p12cp7pltyr.jpg
canonical_url: https://napicella.medium.com/how-to-monitor-your-dependencies-94b224ddd585
---
In the past few years, the [DevOps reports](https://services.google.com/fh/files/misc/state-of-devops-2019.pdfhttps://services.google.com/fh/files/misc/state-of-devops-2019.pdf) have shown how high-performance teams use monitoring to quickly detect errors, reduce downtime and costs. 
Monitoring a complex application is a significant engineering endeavor in and of itself, but thanks to effort of cloud providers, developers can instrument their system with few clicks or configurations.

When it comes to your system health, monitoring your dependencies is just as important as monitoring your system. If your system is experiencing issues, your system dashboards and alarms should highlight if one of your dependency is having a bad time. 
In the rest of the article I show a simple pattern to monitor your dependencies. The examples use [CloudWatch Embedded Metric Format ](https://aws.amazon.com/about-aws/whats-new/2019/11/amazon-cloudwatch-launches-embedded-metric-format/)and golang but the underlying idea can be applied to other cloud platforms or languages.

## What’s EMF?

[CloudWatch Embedded Metric Format](https://aws.amazon.com/about-aws/whats-new/2019/11/amazon-cloudwatch-launches-embedded-metric-format/) allows to automatically generate metrics from logs. As a developer you need to log the metrics as JSON to stdout and Cloud Watch takes care of publishing the metric on your behalf.
You can use one of the [AWS client libraries](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Libraries.html) for NodeJS, Python or Java to build and log an embedded metric structure. Sadly, at the moment AWS has not published a client library for Golang which means you need to create the JSON object yourself following the schema described in the [specification](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html). It’s relatively straightforward and not surprisingly a few community maintained projects do exactly that, for example [aws-embedded-metrics-golang](https://github.com/prozz/aws-embedded-metrics-golang).

I find EMF very convenient but it’s usage is not strictly necessary to monitor your dependencies. You can publish the metric yourself if that’s what you want to do.

## Instrumenting the client

The main idea is to instrument the client you use to call your service. Regardless of the language or the library you use, it’s almost certain that the client you use to call your dependencies allows to plug in custom logic. You can leverage it to log the EMF metric. 
For example, assuming you use a golang http client, you can implement the RoundTripper interface:

{% gist https://gist.github.com/napicella/b38db97b50da2c93230d0322c89629a2 %}

The code implements the RoundTripper interface with the logic to log details about the http calls. In the example, we log the Duration and the result of the call (client error or server error ). Once I deployed the code (in my case to Lambda) and triggered it a few times, we can see the metrics published to CloudWatch:

![Cloud Watch Metrics](https://dev-to-uploads.s3.amazonaws.com/i/cka8b3e2b3hl0p541imb.png)

Instrumenting a different client is very similar. For example, here we instrument the golang AWS SDK to log calls made to other AWS services:
{% gist https://gist.github.com/napicella/da9cf37288e75c81ee6c126ec4a3be83 %}

## Conclusions

With [AWS CloudWatch](https://aws.amazon.com/cloudwatch/?&trk=ta_a134p000003yEACAA2&trkCampaign=pac-edm-2020_M&GAdoption_CloudWatchProductLandingPage&sc_channel=ta&sc_campaign=pac-edm-2020_M&G_Website_UseCase_MonitoringObservability_CloudWatchPDP&sc_outcome=Enterprise_Digital_Marketing&sc_geo=NAMER&sc_country=mult) you can easily set up monitoring for your AWS resources and applications. EMF is a very convenient way to plug custom metrics in your system. As I mentioned, the pattern is quite generic and you can easily port it in other languages or even other monitoring systems.

If you develop on AWS take also a look at [AWS X-Ray](https://aws.amazon.com/xray/?&trk=ta_a134p000003yEA7AAM&trkCampaign=pac-edm-2020_M&GAdoption_XRayPDP&sc_channel=ta&sc_campaign=pac-edm-2020_M&G_Website_UseCase_MonitoringObservability_XRayPDP&sc_outcome=Enterprise_Digital_Marketing&sc_geo=NAMER&sc_country=mult). With AWS X-Ray you can visualize and analyze the health, performance, and availability of your applications in one location, and see an end-to-end view of requests as they travel through your application.

-----------------------------------

Follow me on [Twitter](https://twitter.com/napicellatwit) to get new posts in your feed.
Credit for the cover image to [Luke Chesser](https://unsplash.com/@lukechesser?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/analytics?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText).