Statsd Module

Eloy Coto Pereiro

Edited by

Eloy Coto Pereiro


Table of Contents

1. Admin Guide
1. Overview
2. Parameters
2.1. ip(string)
2.2. port(string)
3. Functions
3.1. statsd_set(key, value[, labels])
3.2. statsd_gauge(key, value[, labels])
3.3. statsd_histogram(key, value[, labels])
3.4. statsd_start(key)
3.5. statsd_stop(key[, labels])
3.6. statsd_incr(key[, labels])
3.7. statsd_decr(key[, labels])
4. KEMI bindings

List of Examples

1.1. Set ip parameter
1.2. Set port parameter
1.3. statsd_set usage
1.4. statsd_set usage with labels
1.5. statsd_gauge usage
1.6. statsd_gauge usage with labels
1.7. statsd_histogram usage
1.8. statsd_histogram usage with labels
1.9. statsd_start usage
1.10. statsd_stop usage
1.11. statsd_stop usage with labels
1.12. statsd_incr usage
1.13. statsd_incr usage with labels
1.14. statsd_decr usage
1.15. statsd_decr usage with labels

Chapter 1. Admin Guide

1. Overview

The module implements the statsd text protocol, allowing Kamailio to emit counters, gauges, histograms and timers to any server that understands statsd semantics. Typical destinations include the reference Etsy/Graphite daemon, InfluxDB via Telegraf listeners, or any other statsd-compatible collector.

The implementation has no special external dependencies; it opens a UDP socket to the configured host and sends the raw statsd datagrams, so any standards-compliant server can receive them.

2. Parameters

2.1. ip(string)

Statsd server IP address.

Defaults to 127.0.0.1

Example 1.1. Set ip parameter

...
modparam("statsd", "ip", "127.0.0.1")
...

2.2. port(string)

Statsd server port number

Defaults to 8125

Example 1.2. Set port parameter

...
modparam("statsd", "port", "8125")
...

3. Functions

3.1.  statsd_set(key, value[, labels])

Sets count the number of unique values passed to a key.

If this method is called multiple times with the same userid in the same sample period, that userid will only be counted once.

Optional labels supply a comma-separated list of key:value tags; omit the argument to emit the metric untagged.

This function can be used in ALL ROUTES.

Example 1.3. statsd_set usage

...
failure_route[tryagain] {
...
    statsd_set("customerFailure", 1);
...
}
...

Example 1.4. statsd_set usage with labels

...
statsd_set("active_users", $var(count), "cluster:a,region:$var(region)");
...

3.2.  statsd_gauge(key, value[, labels])

Gauges are a constant data type. They are not subject to averaging and they don't change unless you change them. That is, once you set a gauge value, it will be a flat line on the graph until you change it again.

Gauges are useful for things that are already averaged, or don't need to reset periodically

This function can be used in ALL ROUTES.

The statsd server collects gauges under the stats.gauges prefix. Optional labels supply a comma-separated list of key:value tags; omit the argument to emit the metric untagged.

Example 1.5. statsd_gauge usage

...
route [gauge_method]{
    statsd_gauge("method"+$rm, "+1");
    statsd_gauge("customer_credit"+$var(customer),"$var(customer_credit)");
}
...

Example 1.6. statsd_gauge usage with labels

...
statsd_gauge("method"+$rm, "+1", "node:$si");
...

3.3.  statsd_histogram(key, value[, labels])

The histograms are a measure of time, but they are calculated at the server side. As the data exported by the client is the same, this is just an alias for the Timer type.

This function can be used in ALL ROUTES.

The statsd server collects histograms under the stats.histograms prefix. Optional labels supply a comma-separated list of key:value tags; omit the argument to emit the metric untagged.

Example 1.7. statsd_histogram usage

...
    statsd_histogram("latency", 1000);
...

Example 1.8. statsd_histogram usage with labels

...
statsd_histogram("latency", 1000, "method:"+$rm+",peer:"+$si);
...

3.4.  statsd_start(key)

statsd_start set an avp with the key name, and when statsd_stop(key) is used, the module will send statsd the difference in milliseconds. This is useful to know the time of a SQL query, or how much time your replies take.

This function can be used in all routes.

The statsd server collects all timers under the stats.timers prefix and will calculate the lower bound, mean, 90th percentile, upper bound, and count of each timer for each period (by the time it can be seen in graphite, that's usually per minute).

Example 1.9. statsd_start usage

...
statsd_start("long_mysql_query");
sql_query("ca", "select sleep(0.2)", "ra");
statsd_stop("long_mysql_query");
...

3.5.  statsd_stop(key[, labels])

statsd_stop(key) get the avp string with the key and calculate the difference from the start time. When finished the milliseconds used will be sent to statsd.

Optional labels supply a comma-separated list of key:value tags; omit the argument to emit the metric untagged.

This function can be used in all routes.

Example 1.10. statsd_stop usage

...
statsd_start("long_mysql_query");
sql_query("ca", "select sleep(0.2)", "ra");
statsd_stop("long_mysql_query");
...

Example 1.11. statsd_stop usage with labels

...
statsd_start("long_mysql_query");
sql_query("ca", "select sleep(0.2)", "ra");
statsd_stop("long_mysql_query", "db:replica,shard:$var(shard)");
...

3.6.  statsd_incr(key[, labels])

Increment a statsd counter

This function can be used in all routes.

Optional labels supply a comma-separated list of key:value tags; omit the argument to emit the metric untagged.

Example 1.12. statsd_incr usage

...
if(geoip_match("$si", "src")){
    statsd_incr("country."+$(gip(src=>cc)));
}
...

Example 1.13. statsd_incr usage with labels

...
if(geoip_match("$si", "src")){
	statsd_incr("country."+$(gip(src=>cc)), "asn:"+$(gip(src=>asn)));
}
...

3.7.  statsd_decr(key[, labels])

Decrement a counter

This function can be used in all routes.

Example 1.14. statsd_decr usage

...
if (t_check_status("408")) {
    statsd_decr("kamailio.successfulCalls");
    statsd_incr("kamailio.reply.timeout");
}
...

Example 1.15. statsd_decr usage with labels

...
if (t_check_status("408")) {
	statsd_decr("kamailio.successfulCalls", "response:timeout");
	statsd_incr("kamailio.reply.timeout", "response:timeout");
}
...

4. KEMI bindings

All of the functionality above is also exported to Kamailio's scripting interfaces (Lua, Python, JS, etc.) via the statsd KEMI module. For every function that accepts labels there is a companion name suffixed with _labels. For example, statsd_gauge and statsd_labels_gauge, statsd_histogram and statsd_labels_histogram, statsd_set and statsd_labels_set, statsd_incr and statsd_labels_incr, and so on. Timers are exposed as statsd_start, statsd_stop and statsd_labels_stop.