Statsd.pm

filter: Statementstmt 100% 75 / 75 Branchbran 65.0% 13 / 20 Conditioncond 50.0% 8 / 16 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 9 totaltotal 77.0% 111 / 144 CC 15 SCAR 9.8
CC
15
Cov
86%
socket
5.5
_make
3.1
inc_gauge_by
3.0
CRAP 2.7
« DogStatsd.pm ↑ Summary SignalFx.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2020-2026 -- leonerd@leonerd.org.uk
5  
6 package Metrics::Any::Adapter::Statsd 0.04;
7  
8 4 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # We don't use Net::Statsd because it
14 # a) is hard to override sending for custom formats e.g. SignalFx or DogStatsd
15 # b) sends differently-named stats in different packets, losing atomicity of
16 # distribution updates
17 4 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # TODO: Keep the same config for now
20 4 ▶$Net::Statsd::HOST //= "127.0.0.1";
Condition50%1/2
$Net::Statsd::HOST //= "127.0.0.1"
l!l
04
MC/DC0%0/1
$Net::Statsd::HOST //= "127.0.0.1"
$Net::Statsd::HOST
Truth table50%1/2
A
A= $Net::Statsd::HOST
Aresult
11
00
21 4 ▶$Net::Statsd::PORT //= 8125;
Condition100%2/2
$Net::Statsd::PORT //= 8125
l!l
31
MC/DC100%1/1
$Net::Statsd::PORT //= 8125
$Net::Statsd::PORT
Truth table100%2/2
A
A= $Net::Statsd::PORT
Aresult
11
00
22  
23 =head1 NAME
24  
25 C<Metrics::Any::Adapter::Statsd> - a metrics reporting adapter for statsd
26  
27 =head1 SYNOPSIS
28  
29 =for highlighter language=perl
30  
31 use Metrics::Any::Adapter 'Statsd';
32  
33 =head1 DESCRIPTION
34  
35 This L<Metrics::Any> adapter type reports metrics to statsd via the local UDP
36 socket. Each metric value reported will result in a new UDP packet being sent.
37  
38 The default location of the statsd server is set by two package variables,
39 defaulting to
40  
41 $Net::Statsd::HOST = "127.0.0.1";
42 $Net::Statsd::PORT = 8125;
43  
44 The configuration can be changed by setting new values or by passing arguments
45 to the import line:
46  
47 use Metrics::Any::Adapter 'Statsd', port => 8200;
48  
49 =head1 METRIC HANDLING
50  
51 Unlabelled counter, gauge and timing metrics are handled natively as you would
52 expect for statsd; with multipart names being joined by periods (C<.>).
53  
54 Distribution metrics are emitted as two sub-named metrics by appending
55 C<count> and C<sum>. The C<count> metric in incremented by one for each
56 observation and the C<sum> by the observed amount.
57  
58 Labels are not handled by this adapter and are thrown away. This will result
59 in a single value being reported that accumulates the sum total across all of
60 the label values. In the case of labelled gauges using the C<set_gauge_to>
61 method this will not be a useful value.
62  
63 For better handling of labelled metrics for certain services which have
64 extended the basic statsd format to handle them, see:
65  
66 =over 2
67  
68 =item *
69  
70 L<Metrics::Any::Adapter::DogStatsd> - a metrics reporting adapter for DogStatsd
71  
72 =item *
73  
74 L<Metrics::Any::Adapter::SignalFx> - a metrics reporting adapter for SignalFx
75  
76 =back
77  
78 =head1 ARGUMENTS
79  
80 The following additional arguments are recognised
81  
82 =head2 host
83  
84 =head2 port
85  
86 Provides specific values for the statsd server location.
87  
88 =cut
89  
90 sub new
91 3 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
92 3 my $class = shift;
93 3 my ( %args ) = @_;
94  
95 return bless {
96 host => $args{host},
97 port => $args{port},
98 3 metrics => {},
99 gauge_initialised => {},
100 }, $class;
101 }
102  
103 sub mangle_name
104 17 ▶{
Subroutine mangle_name: called CC=2 SCAR=7.0
Pod undocumented
105 17 my $self = shift;
106 17 my ( $name ) = @_;
107  
108 17 ▶ return join ".", @$name if ref $name eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $name eq "ARRAY" 0 17 17
109  
110 # Convert _-separated components into .
111 17 $name =~ s/_/./g;
112 17 return $name;
113 }
114  
115 sub socket
116 16 ▶{
Subroutine socket: called CC=4 SCAR=17.1
Pod undocumented
117 16 my $self = shift;
118  
119 return $self->{socket} //= IO::Socket::INET->new(
120 Proto => "udp",
121 PeerHost => $self->{host} // $Net::Statsd::HOST,
122 16 ▶ PeerPort => $self->{port} // $Net::Statsd::PORT,
Condition44%4/9
$self->{"socket"} //= "IO::Socket::INET"->new("Proto", "udp", "PeerHost", $self->{"host"} // $Net::Statsd::HOST, "PeerPort", $self->{"port"} // $Net::Statsd::PORT)
l!l&&r!l&&!r
1330
$self->{"host"} // $Net::Statsd::HOST
l!l&&r!l&&!r
030
$self->{"port"} // $Net::Statsd::PORT
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->{"socket"} //= "IO::Socket::INET"->new("Proto", "udp", "PeerHost", $self->{"host"} // $Net::Statsd::HOST, "PeerPort", $self->{"port"} // $Net::Statsd::PORT)
$self->{"socket"} "IO::Socket::INET"->new("Proto", "udp", "PeerHost", $self->{"host"} // $Net::Statsd::HOST, "PeerPort", $self->{"port"} // $Net::Statsd::PORT)
MC/DC0%0/2
$self->{"host"} // $Net::Statsd::HOST
$self->{"host"} $Net::Statsd::HOST
MC/DC0%0/2
$self->{"port"} // $Net::Statsd::PORT
$self->{"port"} $Net::Statsd::PORT
Truth table66%2/3
A //= B
A= $self->{"socket"}B= "IO::Socket::INET"->new("Proto", "udp", "PeerHost", $self->{"host"} // $Net::Statsd::HOST, "PeerPort", $self->{"port"} // $Net::Statsd::PORT)
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $self->{"host"}B= $Net::Statsd::HOST
ABresult
1X1
011
000
Truth table33%1/3
A // B
A= $self->{"port"}B= $Net::Statsd::PORT
ABresult
1X1
011
000
123 );
124 }
125  
126 sub send
127 16 ▶{
Subroutine send: called CC=2 SCAR=6.9
Pod undocumented
128 16 my $self = shift;
129 16 my ( $stats, $labelnames, $labelvalues ) = @_;
130  
131 $self->socket->send(
132 join "\n", map {
133 16 my $name = $_;
134 18 my $value = $stats->{$name};
135 18 ▶ map { sprintf "%s:%s", $name, $_ } ref $value eq "ARRAY" ? @$value : $value
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 9 9 18
136 } sort keys %$stats
137 );
138 }
139  
140 sub _make
141 17 ▶{
Subroutine _make: called CC=3 SCAR=11.2
142 17 my $self = shift;
143 17 my ( $handle, %args ) = @_;
144  
145 17 ▶ my $name = $self->mangle_name( delete $args{name} // $handle );
Condition33%1/3
delete $args{"name"} // $handle
l!l&&r!l&&!r
1700
MC/DC0%0/2
delete $args{"name"} // $handle
delete $args{"name"} $handle
Truth table33%1/3
A // B
A= delete $args{"name"}B= $handle
ABresult
1X1
011
000
146 17 ▶ $name =~ m/[\x00-\x1f:\|]/ and
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /[\x00-\x1f:\|]/u and croak("Metric name '${name}' is not allowed by statsd") 3 14 17
147 croak "Metric name '$name' is not allowed by statsd";
148  
149 $self->{metrics}{$handle} = {
150 name => $name,
151 labels => $args{labels},
152 14 };
153 }
154  
155 4 *make_counter = \&_make;
156  
157 sub inc_counter_by
158 5 ▶{
Subroutine inc_counter_by: called CC=2 SCAR=7.0
Pod undocumented
159 5 my $self = shift;
160 5 my ( $handle, $amount, @labelvalues ) = @_;
161  
162 5 ▶ my $meta = $self->{metrics}{$handle} or croak "No metric '$handle'";
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $self->{"metrics"}{$handle} or croak("No metric '${handle}'") 5 0 5
163  
164 5 my $value = sprintf "%g|c", $amount;
165  
166 5 $self->send( { $meta->{name} => $value }, $meta->{labels}, \@labelvalues );
167 }
168  
169 4 *make_distribution = \&_make;
170  
171 sub report_distribution
172 2 ▶{
Subroutine report_distribution: called CC=2 SCAR=7.0
Pod undocumented
173 2 my $self = shift;
174 2 my ( $handle, $amount, @labelvalues ) = @_;
175  
176 # A distribution acts like two counters; `sum` a `count`.
177  
178 2 ▶ my $meta = $self->{metrics}{$handle} or croak "No metric '$handle'";
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $self->{"metrics"}{$handle} or croak("No metric '${handle}'") 2 0 2
179  
180 2 my $value = sprintf "%g|c", $amount;
181  
182 $self->send( {
183 "$meta->{name}.sum" => $value,
184 "$meta->{name}.count" => "1|c",
185 2 }, $meta->{labels}, \@labelvalues );
186 }
187  
188 4 *inc_distribution_by = \&report_distribution;
189  
190 4 *make_gauge = \&_make;
191  
192 sub inc_gauge_by
193 1 ▶{
Subroutine inc_gauge_by: called CC=3 SCAR=11.1
Pod undocumented
194 1 my $self = shift;
195 1 my ( $handle, $amount, @labelvalues ) = @_;
196  
197 1 ▶ my $meta = $self->{metrics}{$handle} or croak "No metric '$handle'";
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $self->{"metrics"}{$handle} or croak("No metric '${handle}'") 1 0 1
198 1 my $name = $meta->{name};
199  
200 1 my @value;
201 1 ▶ push @value, "0|g" unless $self->{gauge_initialised}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"gauge_initialised"}{$name} 0 1 1
202 1 push @value, sprintf( "%+g|g", $amount );
203  
204 1 $self->send( { $name => \@value }, $meta->{labels}, \@labelvalues );
205 1 $self->{gauge_initialised}{$name} = 1;
206 }
207  
208 sub set_gauge_to
209 4 ▶{
Subroutine set_gauge_to: called CC=3 SCAR=11.0
Pod undocumented
210 4 my $self = shift;
211 4 my ( $handle, $amount, @labelvalues ) = @_;
212  
213 4 ▶ my $meta = $self->{metrics}{$handle} or croak "No metric '$handle'";
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $self->{"metrics"}{$handle} or croak("No metric '${handle}'") 4 0 4
214 4 my $name = $meta->{name};
215  
216 4 my @value;
217 # wire format interprets a leading - as a decrement request; so negative
218 # absolute values must first set zero
219 4 ▶ push @value, "0|g" if $amount < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $amount < 0 1 3 4
220 4 push @value, sprintf( "%g|g", $amount );
221  
222 4 $self->send( { $name => \@value }, $meta->{labels}, \@labelvalues );
223 4 $self->{gauge_initialised}{$name} = 1;
224 }
225  
226 4 *make_timer = \&_make;
227  
228 sub report_timer
229 3 ▶{
Subroutine report_timer: called CC=2 SCAR=7.0
Pod undocumented
230 3 my $self = shift;
231 3 my ( $handle, $duration, @labelvalues ) = @_;
232  
233 3 ▶ my $meta = $self->{metrics}{$handle} or croak "No metric '$handle'";
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $self->{"metrics"}{$handle} or croak("No metric '${handle}'") 3 0 3
234  
235 3 my $value = sprintf "%d|ms", $duration * 1000; # msec
236  
237 3 $self->send( { $meta->{name} => $value }, $meta->{labels}, \@labelvalues );
238 }
239  
240 4 *inc_timer_by = \&report_timer;
241  
242 =head1 TODO
243  
244 =over 4
245  
246 =item *
247  
248 Support non-one samplerates; emit only one-in-N packets with the C<@rate>
249 notation in the packet.
250  
251 =item *
252  
253 Optionally support one dimension of labelling by appending the conventional
254 C<some.metric.by_$label.$value> notation to it.
255  
256 =back
257  
258 =cut
259  
260 =head1 AUTHOR
261  
262 Paul Evans <leonerd@leonerd.org.uk>
263  
264 =cut
265  
266 4 0x55AA;
« DogStatsd.pm ↑ Summary SignalFx.pm »