Chapter 12. Writing Exporters
Sometimes you will not be able to either add direct instrumentation to an application, nor find an existing exporter that covers it. This leaves you with having to write an exporter yourself. The good news is that exporters are relatively easy to write. The hard part is figuring out what the metrics exposed by applications mean. Units are often unknown, and documentation, if it exists at all, can be vague. In this chapter you will learn how to write exporters.
Consul Telemetry
We are going to write a small exporter for Consul to demonstrate the process. We already saw Consul and the Consul Exporter in “Consul”, so let’s create a simple exporter with metrics from the telemetry API.1
While you can write exporters in any programming language, the majority are written in Go, and that is the language we will use here. However, you will find a small number of exporters written in Python, and an even smaller number in Java.
If your Consul is not running, start it again following the instructions in Example 8-8. If you visit http://localhost:8500/v1/agent/metrics, you will see the JSON output that you will be working with, which is similar to Example 12-1. Conveniently, Consul provides a Go library that you can use, so you don’t have to worry about parsing the JSON yourself.
Example 12-1. An abbreviated example output from a Consul agent’s metrics output
{
"Timestamp"
:
"2018-01-31 14:42:10 +0000 UTC"
,
"Gauges"
:
[
{
"Name"
:
"consul.autopilot.failure_tolerance" ...
Get Prometheus: Up & Running, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.