NAME

Net::Statsd - Perl client for Etsy's statsd daemon

VERSION

version 0.13

SYNOPSIS

# Configure where to send events
# That's where your statsd daemon is listening.
$Net::Statsd::HOST = 'localhost';    # Default
$Net::Statsd::PORT = 8125;           # Default

#
# Keep track of events as counters
#
Net::Statsd::increment('site.logins');
Net::Statsd::increment('database.connects');

#
# Log timing of events, ex. db queries
#
use Time::HiRes;
my $start_time = [ Time::HiRes::gettimeofday ];

# do the complex database query
# note: time value sent to timing should
# be in milliseconds.
Net::Statsd::timing(
    'database.complexquery',
    Time::HiRes::tv_interval($start_time) * 1000
);

#
# Log metric values
#
Net::Statsd::gauge('core.temperature' => 55);

DESCRIPTION

This module implement a UDP client for the statsd statistics collector daemon in use at Etsy.com.

You want to use this module to track statistics in your Perl application, such as how many times a certain event occurs (user logins in a web application, or database queries issued), or you want to time and then graph how long certain events take, like database queries execution time or time to download a certain file, etc...

If you're uncertain whether you'd want to use this module or statsd, then you can read some background information here:

http://codeascraft.etsy.com/2011/02/15/measure-anything-measure-everything/

The github repository for statsd is:

http://github.com/etsy/statsd

By default the client will try to send statistic metrics to localhost:8125, but you can change the default hostname and port with:

$Net::Statsd::HOST = 'your.statsd.hostname.net';
$Net::Statsd::PORT = 9999;

just after including the Net::Statsd module.

ABOUT SAMPLING

A note about sample rate: A sample rate of < 1 instructs this library to send only the specified percentage of the samples to the server. As such, the application code should call this module for every occurence of each metric and allow this library to determine which specific measurements to deliver, based on the sample_rate value. (e.g. a sample rate of 0.5 would indicate that approximately only half of the metrics given to this module would actually be sent to statsd).

SECURITY

To prevent metric injection (CVE-2026-46739), metric names and values are validated before being sent. The statsd wire format is name:value|type, and several metrics can be packed into one UDP datagram separated by newlines, so a name or value containing a newline (or any control character below ASCII 32), a colon (:) or a pipe (|) could be used to forge extra metric lines.

Any such name or value causes the offending call (timing, increment, decrement, update_stats, gauge or send) to throw via Carp::croak. Note that values passed to send() already contain a formatted |type suffix, so send() only re-validates the metric names; raw values are validated by the higher level functions before formatting.

FUNCTIONS

timing($name, $time, $sample_rate = 1)

Log timing information. Time is assumed to be in milliseconds (ms).

Net::Statsd::timing('some.timer', 500);

get_timer($name, $sample_rate = 1)

Start timer for the metric. Function returns timer object. When you call this object, it sends timing data to statsd.

my $timer = Net::Statsd::get_timer('my.func.time');
...; # here comes your code
$timer->(); # send timing to server

increment($counter, $sample_rate=1)

increment(\@counter, $sample_rate=1)

Increments one or more stats counters

# +1 on 'some.int'
Net::Statsd::increment('some.int');

# 0.5 = 50% sampling
Net::Statsd::increment('some.int', 0.5);

To increment more than one counter at a time, you can pass an array reference:

Net::Statsd::increment(['grue.dinners', 'room.lamps'], 1);

You can also use "inc()" instead of "increment()" to type less.

decrement($counter, $sample_rate=1)

Same as increment, but decrements. Yay.

Net::Statsd::decrement('some.int')

You can also use "dec()" instead of "decrement()" to type less.

update_stats($stats, $delta=1, $sample_rate=1)

Updates one or more stats counters by arbitrary amounts

Net::Statsd::update_stats('some.int', 10)

equivalent to:

Net::Statsd::update_stats('some.int', 10, 1)

A sampling rate less than 1 means only update the stats every x number of times (0.1 = 10% of the times).

gauge($name, $value)

Log arbitrary values, as a temperature, or server load.

Net::Statsd::gauge('core.temperature', 55);

Statsd interprets gauge values with + or - sign as increment/decrement. Therefore, to explicitly set a gauge to a negative number it has to be set to zero first.

However, if either the zero or the actual negative value is lost in UDP transport to statsd server because of e.g. network congestion or packet loss, your gauge will become skewed.

To ensure network problems will not skew your data, Net::Statsd::gauge() supports packing multiple values in single UDP packet sent to statsd:

Net::Statsd::gauge(
    'core.temperature' => 55,
    'freezer.temperature' => -18
);

Make sure you don't supply too many values, or you might risk exceeding the MTU of the network interface and cause the resulting UDP packet to be dropped.

In general, a safe limit should be 512 bytes. Related to the example above, core.temperature of 55 will be likely packed as a string:

core.temperature:55|g

which is 21 characters, plus a newline used as delimiter (22). Using this example, you can pack at least 20 distinct gauge values without problems. That will result in a UDP message of 440 bytes (22 times 20), which is well below the safe threshold of 512.

In reality, if the communication happens on a local interface, or over a 10G link, you are allowed much more than that.

send(\%data, $sample_rate = 1)

Squirt the metrics over UDP.

Net::Statsd::send({ 'some.int' => 1 });

_sample_data(\%data, $sample_rate = 1)

This method is used internally, it's not part of the public interface.

Takes care of transforming a hash of metrics data into a sampled hash of metrics data, according to the given $sample_rate.

If $sample_rate == 1, then sampled data is exactly the incoming data.

If $sample_rate = 0.2, then every metric value will be marked with the given sample rate, so the Statsd server will automatically scale it. For example, with a sample rate of 0.2, the metric values will be multiplied by 5.

_validate_metric_name($name)

_validate_metric_value($value)

These methods are used internally, they're not part of the public interface.

Guard against metric injection (CVE-2026-46739). The statsd wire format is name:value|type, and multiple metrics are packed in a single UDP datagram separated by newlines. A metric name or value that contains a newline (or any other control character below ASCII 32), a colon (:) or a pipe (|) could therefore forge additional, attacker-controlled metric lines.

Both functions Carp::croak when the input contains any of those characters. Names are validated centrally in send(); raw values are validated at each recording entry point (timing, update_stats, gauge) before the |type suffix is appended.

AUTHOR

Cosimo Streppone <cosimo@streppone.it>

COPYRIGHT AND LICENSE

This software is copyright (c) 2026 by Cosimo Streppone.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.