.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Net::Statsd 3pm" .TH Net::Statsd 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Net::Statsd \- Perl client for Etsy's statsd daemon .SH VERSION .IX Header "VERSION" version 0.12 .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 4 \& # Configure where to send events \& # That\*(Aqs where your statsd daemon is listening. \& $Net::Statsd::HOST = \*(Aqlocalhost\*(Aq; # Default \& $Net::Statsd::PORT = 8125; # Default \& \& # \& # Keep track of events as counters \& # \& Net::Statsd::increment(\*(Aqsite.logins\*(Aq); \& Net::Statsd::increment(\*(Aqdatabase.connects\*(Aq); \& \& # \& # 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( \& \*(Aqdatabase.complexquery\*(Aq, \& Time::HiRes::tv_interval($start_time) * 1000 \& ); \& \& # \& # Log metric values \& # \& Net::Statsd::gauge(\*(Aqcore.temperature\*(Aq => 55); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This module implement a UDP client for the \fBstatsd\fR statistics collector daemon in use at Etsy.com. .PP 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... .PP If you're uncertain whether you'd want to use this module or statsd, then you can read some background information here: .PP .Vb 1 \& http://codeascraft.etsy.com/2011/02/15/measure\-anything\-measure\-everything/ .Ve .PP The github repository for statsd is: .PP .Vb 1 \& http://github.com/etsy/statsd .Ve .PP By default the client will try to send statistic metrics to \&\f(CW\*(C`localhost:8125\*(C'\fR, but you can change the default hostname and port with: .PP .Vb 2 \& $Net::Statsd::HOST = \*(Aqyour.statsd.hostname.net\*(Aq; \& $Net::Statsd::PORT = 9999; .Ve .PP just after including the \f(CW\*(C`Net::Statsd\*(C'\fR module. .SH "ABOUT SAMPLING" .IX Header "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). .SH FUNCTIONS .IX Header "FUNCTIONS" .ie n .SS """timing($name, $time, $sample_rate = 1)""" .el .SS "\f(CWtiming($name, $time, $sample_rate = 1)\fP" .IX Subsection "timing($name, $time, $sample_rate = 1)" Log timing information. \&\fBTime is assumed to be in milliseconds (ms)\fR. .PP .Vb 1 \& Net::Statsd::timing(\*(Aqsome.timer\*(Aq, 500); .Ve .ie n .SS """increment($counter, $sample_rate=1)""" .el .SS "\f(CWincrement($counter, $sample_rate=1)\fP" .IX Subsection "increment($counter, $sample_rate=1)" .ie n .SS """increment(\e@counter, $sample_rate=1)""" .el .SS "\f(CWincrement(\e@counter, $sample_rate=1)\fP" .IX Subsection "increment(@counter, $sample_rate=1)" Increments one or more stats counters .PP .Vb 2 \& # +1 on \*(Aqsome.int\*(Aq \& Net::Statsd::increment(\*(Aqsome.int\*(Aq); \& \& # 0.5 = 50% sampling \& Net::Statsd::increment(\*(Aqsome.int\*(Aq, 0.5); .Ve .PP To increment more than one counter at a time, you can \fBpass an array reference\fR: .PP .Vb 1 \& Net::Statsd::increment([\*(Aqgrue.dinners\*(Aq, \*(Aqroom.lamps\*(Aq], 1); .Ve .PP \&\fBYou can also use "inc()" instead of "increment()" to type less\fR. .ie n .SS """decrement($counter, $sample_rate=1)""" .el .SS "\f(CWdecrement($counter, $sample_rate=1)\fP" .IX Subsection "decrement($counter, $sample_rate=1)" Same as increment, but decrements. Yay. .PP .Vb 1 \& Net::Statsd::decrement(\*(Aqsome.int\*(Aq) .Ve .PP \&\fBYou can also use "dec()" instead of "decrement()" to type less\fR. .ie n .SS """update_stats($stats, $delta=1, $sample_rate=1)""" .el .SS "\f(CWupdate_stats($stats, $delta=1, $sample_rate=1)\fP" .IX Subsection "update_stats($stats, $delta=1, $sample_rate=1)" Updates one or more stats counters by arbitrary amounts .PP .Vb 1 \& Net::Statsd::update_stats(\*(Aqsome.int\*(Aq, 10) .Ve .PP equivalent to: .PP .Vb 1 \& Net::Statsd::update_stats(\*(Aqsome.int\*(Aq, 10, 1) .Ve .PP A sampling rate less than 1 means only update the stats every x number of times (0.1 = 10% of the times). .ie n .SS """gauge($name, $value)""" .el .SS "\f(CWgauge($name, $value)\fP" .IX Subsection "gauge($name, $value)" Log arbitrary values, as a temperature, or server load. .PP .Vb 1 \& Net::Statsd::gauge(\*(Aqcore.temperature\*(Aq, 55); .Ve .PP Statsd interprets gauge values with \f(CW\*(C`+\*(C'\fR or \f(CW\*(C`\-\*(C'\fR sign as increment/decrement. Therefore, to explicitly set a gauge to a negative number it has to be set to zero first. .PP 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. .PP To ensure network problems will not skew your data, \f(CWNet::Statsd::gauge()\fR supports packing multiple values in single UDP packet sent to statsd: .PP .Vb 4 \& Net::Statsd::gauge( \& \*(Aqcore.temperature\*(Aq => 55, \& \*(Aqfreezer.temperature\*(Aq => \-18 \& ); .Ve .PP 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. .PP In general, a safe limit should be 512 bytes. Related to the example above, \f(CW\*(C`core.temperature\*(C'\fR of 55 will be likely packed as a string: .PP .Vb 1 \& core.temperature:55|g .Ve .PP 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 \fIsafe\fR threshold of 512. .PP In reality, if the communication happens on a local interface, or over a 10G link, you are allowed much more than that. .ie n .SS """send(\e%data, $sample_rate = 1)""" .el .SS "\f(CWsend(\e%data, $sample_rate = 1)\fP" .IX Subsection "send(%data, $sample_rate = 1)" Squirt the metrics over UDP. .PP .Vb 1 \& Net::Statsd::send({ \*(Aqsome.int\*(Aq => 1 }); .Ve .ie n .SS """_sample_data(\e%data, $sample_rate = 1)""" .el .SS "\f(CW_sample_data(\e%data, $sample_rate = 1)\fP" .IX Subsection "_sample_data(%data, $sample_rate = 1)" \&\fBThis method is used internally, it's not part of the public interface.\fR .PP Takes care of transforming a hash of metrics data into a \fBsampled\fR hash of metrics data, according to the given \&\f(CW$sample_rate\fR. .PP If \f(CW\*(C`$sample_rate == 1\*(C'\fR, then sampled data is exactly the incoming data. .PP If \f(CW\*(C`$sample_rate = 0.2\*(C'\fR, then every metric value will be \fImarked\fR 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. .SH AUTHOR .IX Header "AUTHOR" Cosimo Streppone .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2016 by Cosimo Streppone. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.