Instrument Your Go Apps With Expvar and Datadog | Datadog

Instrument your Go apps with Expvar and Datadog

Author Matt Williams

Published: 2月 9, 2015

Here at Datadog, we have created myriad integrations with your favorite applications. We also provide ways to instrument your own custom apps using various DogStatsD libraries as well as the RESTful API. For those of you writing your apps in Go, the Datadog Agent 5.1.0 introduces the ability to use expvar as well.

Instrument your Go apps with Expvar and Datadog

Expvar is a Go package which allows you to define variables to export and publish over http. To take advantage of this and expose an expvar as a Datadog metric, follow these simple steps:

  1. Define an expvar variable and set it
  2. Create the Datadog configuration file for expvar

That’s really all there is to it. Let’s take a look at a specific example using a trivial application: a simple webserver. For this example we want to have a Datadog dashboard that shows the number of times a particular page has been visited in the last minute. The first step is to import the expvar package and define the expvar:

var (
  counter        *ratecounter.RateCounter
  hitsperminute = expvar.NewInt("hits_per_minute")

In my code I created the hitsperminute expvar as well as a counter using Paul Bellamy’s RateCounter package to determine hits per amount of time.

By simply including the expvar package and then creating this variable, a special debug page is created with the current value of hits_per_minute as well a collection of memory stats.

Then, in the portion of code that handles the page view, I set the value of the hitsperminute variable.

func increment(w http.ResponseWriter, r *http.Request) {
  io.WriteString(w, strconv.FormatInt(counter.Rate(), 10))

As you can see I am using a http handler function to display a simple page showing the current rate. However, before that, I increment the ratecounter and then set the value of my expvar to the current ratecounter rate.

The main() function is where you get to see just how trivial this app is.

func main() {
  counter = ratecounter.NewRateCounter(1 * time.Minute)
  http.HandleFunc("/increment", increment)
  http.ListenAndServe(":8000", nil)

I instantiate the ratecounter and then listen and serve the increment function at http://localhost:8000/increment. You can see the full source code for this app here.

The expvar package takes care of the rest and publishes the variable along with some performance metrics to a standard location which will be http://localhost:8000/debug/vars.

The final step is to enable the Datadog expvar configuration. Within the Datadog installation directory, go to conf.d and edit the go_expvar.yaml file (or copy the go_expvar.yaml.example file). Add the following to the end of the file, preserving the current tab level:

- path: hits_per_minute
  alias: go_expvar.hits_per_minute
  type: gauge

You can find the complete yaml file here.

When you next visit the Metrics Explorer, you will see the go_expvar.hits_per_minute metric available for all of your dashboards.

Instrument your Go apps with Expvar and Datadog

Now visit the site served by the Go app and hit refresh or use a simple script like this to randomly hit the page a thousand times.

If you’re not currently a Datadog customer, sign up for a and expose expvars as a Datadog metric to monitor your Go application performance.