Welcome, Guest Login

Support Center

Using statsd to send aggregated metrics to Stackdriver

Last Updated: May 01, 2014 02:10PM EDT
Statsd is a network daemon that runs as an independent lightweight service, accepts values over UDP, and sends aggregates out to one or more configurable output backends. You can find the project page and releases on GitHub. Getting up and running is as simple as extracting their package and setting up a short configuration file for your output.

Stackdriver has released an output backend for statsd, which you can find it on Github as well. It can be added to a statsd install by going into the statsd directory and doing an npm install to pull in the module.

Our friends at segment.io have released an output adapter for statsite, which is hosted in Github also.  Statsite is a C-language implementation of statsd.

Below is a sample statsd configuration for the Stackdriver output backend. See the project page for examples on tying the metrics from an install to a single instance or turning on verbose logging.
flushInterval: 60000,
backends: [ "stackdriver-statsd-backend"],
stackdriver: {


Once you have statsd up and running, it’s just a matter of setting up your app to send metrics. The sample below is in Python, and uses the Python statsd library.

import random
import time
import statsd
import math
sc = statsd.StatsClient('localhost', 8125)
while True:
sc.timing('test_statsd_metrics.test_timer', math.floor(random.random() * 100))
sc.gauge('test_statsd_metrics.test_gauge', math.floor(random.random() * 100))

This short program illustrates the different types of metrics that can be sent from statsd to Stackdriver: counters, timers and gauges. Connecting to statsd is simply done over a local connection without the need to open up any inbound ports.


The .incr() function adds +1 or more to a counter variable. With the configuration above a counter will send two values a minute to Stackdriver: a count for the minute and a rate per second of how quickly the count was incremented. The name you choose will be suffixed with .count and .rate and be available for charts in custom metrics.



Timers can be used to track how long something takes, and send more values as a result. There are counts and rates like a counter, as well as min/max/average/sum values for the timings so you can see how whatever you are timing is performing. You can use this for tracking completion times for requests, method calls, queries. The suffixes are .count, .rate, .avg, .sum, .min, .max and can be combined in a chart. Using .min/.max./.avg for a series in the same custom chart is an example of a potential combination.



The .gauge() function is a simple way to set a value that will be overwritten each time. Every minute the latest value will be sent as a custom metric to Stackdriver with .value suffixed to the name you give in the method call.

seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
Invalid characters found