By Dan Bader — Get free updates of new posts here.
This is a tutorial about how to instrument a Node.js app with realtime metrics. We’ll use Datadog as a backend to do the monitoring together with the datadog-metrics library.
At Mobify we ran a couple of web services that we monitored in production using Datadog. It’s a great tool to whip up dashboards quickly and gain insights into the key performance metrics of your application. Datadog also lets you to set up alerts on these metrics so you can use it to monitor application health, too.
Datadog has good Python client libraries but it’s Node.js support was somewhat lacking. I was unhappy with having to set up a statsd instance to report metrics from Node.js apps and decided to write a library called datadog-metrics to solve that problem.
Datadog-metrics let’s you report metrics directly from your Node.js app to Datadog’s HTTP API without having to setup and maintain a statsd instance. It handles the details like aggregation and buffering (we don’t want to send one HTTP requests for every single metric we report), and provides utilities for things like computing histograms. They help answer questions like “What’s the average and 95th percentile request time for this app?”.
Let’s put together a simple example application that reports some memory statistics into a Datadog dashboard using datadog-metrics.
After you’ve signed up we need to grab the Datadog API key. You can find the API key under Integrations » APIs. You only need the API key to set up datadog-metrics, not the App key.
Step 2: Integrate datadog-metrics in your app
Next up, we’re going to install the datadog-metrics library and integrate it with our application. To demonstrate how that works we’ll create a simple demo app that reports some memory stats to Datadog.
Let’s start with installing datadog-metrics via NPM:
$ npm install --save datadog-metrics
That’s all it takes to report some basic metrics with datadog-metrics. Now let’s spin up the app with the following shell command. Make sure that DATADOG_API_KEY is set to the API key we’ve copied from the Datadog admin console in step 1:
If you let this run for a little while your console output should look similar to this. That means we’re now sending metrics to Datadog:
After a short delay you should start seeing metrics show up in Datadog’s Metrics Explorer. Just type the key prefix we’ve used in the metrics.init() call (myapp.) into the Graph input box and you should see a list of metrics reported by our app. Click any metric and Datadog will show you a line plot for that metric by default.
Once you see metrics are reported correctly to Datadog we can go ahead and set up a nice little dashboard.
Debugging memory usage in a live Python web app – I worked on a Python web app a while ago that was struggling with using too much memory in production. A helpful technique for debugging this issue was adding a simple API endpoint that exposed memory stats while the app was running.
How to write a great README for your GitHub project – A great README file helps your project to stand out from the sea of open-source software on GitHub. In this article I go over the key elements every README for an open-source project should contain. It also includes a README.md template for use in your own projects.
Working With File I/O in Python – Learn the basics of working with files in Python. How to read from files, how to write data to them, what file seeks are, and why files should be closed.
How to Reverse a String in Python – An overview of the three main ways to reverse a Python string: “slicing”, reverse iteration, and the classic in-place reversal algorithm. Also includes performance benchmarks.
📦🐍 Which frameworks and libraries are the most important to know to be considered an effective Python dev? Here’s how you go from “writing scripts” to “building applications” with Python, master the tools of the trade for Dependency Management, and become an expert at picking quality Python libraries: » Click here to learn more