Last week I discovered a mysterious package in the Go standard library, expvar. A google search turned up little content on it. Undiscovered APIs for exploring? How exciting! I immediately dove in and what I found was neat yet unsurprising.
The expvar package allows a Go process to expose variables to the public via an HTTP endpoint that emits JSON. The simplest usage requires you to do two things in your custom code:
1. Import the package - this has the side effect of registering the
/debug/vars HTTP endpoint.
2. Start up an HTTP server for your process to handle HTTP requests:
If you hit http://localhost:8123/debug/vars, you should see something like this:
(Put that blob into JSONLint if you want to see a more readable but verbose version.)
By default, Go’s runtime exposes data about command line arguments, memory usage and garbage collection with very little effort, all built into the standard library. Simple and easy yet powerful, like most Go functionality.
Adding your own Metrics
As the expvar name implies though, you can expose your own variables into the mix for monitoring purposes. In fact Datadog recently announced that their monitoring agent can pull your custom expvar values into their system for monitoring purposes.
Here you can see how to declare a map of counters and then start to increment them as actions happen in your daemon. You can see in the JSON blob above how they appear when exported:
Wherein I Mix in some Awesome
Well, I’m even more excited because the next version of Inspeqtor Pro will have a Web UI for visualizing the memory and GC data which the Go runtime exposes. This type of functionality is always what I’ve wanted in production and with almost no effort. Sweet.
Here’s a prototype I’m working on right now. You modify your Go daemon to expose the expvar memory data and Inspeqtor Pro can give you this real-time memory visualization.
I’d love to see other runtimes expose similar data via HTTP/JSON. Can Ruby or Python expose similar data? What about the JVM? Rubinius recently discussed their VM metrics support, let’s see other runtimes do the same! Make it easy to expose and tooling will appear to support it.