DogStatsd.pm

filter: Statementstmt 100% 35 / 35 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 2 totaltotal 92.0% 46 / 50 CC 8 SCAR 10.5
CC
8
Cov
95%
_tags
4.0
send
4.0
report_distribution
2.0
CRAP 2.9
↑ Summary Statsd.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::DogStatsd 0.04;
7  
8 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use base qw( Metrics::Any::Adapter::Statsd );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # See also
15 # https://metacpan.org/release/DataDog-DogStatsd/source/lib/DataDog/DogStatsd.pm
16  
17 =head1 NAME
18  
19 C<Metrics::Any::Adapter::DogStatsd> - a metrics reporting adapter for DogStatsd
20  
21 =head1 SYNOPSIS
22  
23 =for highlighter language=perl
24  
25 use Metrics::Any::Adapter 'DogStatsd';
26  
27 =head1 DESCRIPTION
28  
29 This extension of L<Metrics::Any::Adapter::Statsd> supports the additional tag
30 reporting syntax defined by F<DogStatsd> to report labelled metrics.
31  
32 Additionally, distribution metrics are reported as native DogStatsd histograms
33 rather than the two-part count-and-sum implementation of plain statsd. Due to
34 limitations of the line-based protocol, certain characters are not allowed in
35 label names or values. Any C<:>, C<|>, C<,> or linefeeds are replaced by C<_>.
36  
37 =cut
38  
39 sub _tags
40 5 ▶{
Subroutine _tags: called CC=4 SCAR=13.9
41 5 my ( $labels, $labelvalues ) = @_;
42  
43 5 my @tags;
44 5 foreach ( 0 .. $#$labels ) {
45 5 my $name = $labels->[$_];
46 5 my $value = $labelvalues->[$_];
47 # Replace disallowed characters with '_'
48 5 $_ =~ s/[:|,\n]/_/g for $name, $value;
49  
50 5 push @tags, "$name:$value";
51 }
52  
53 5 ▶ return "" unless @tags;
Branch: 1 branch
BranchTrueFalseTotal
unless @tags 0 5 5
54 5 return "|#" . join( ",", @tags );
55 }
56  
57 sub send
58 5 ▶{
Subroutine send: called CC=4 SCAR=13.9
Pod undocumented
59 5 my $self = shift;
60 5 my ( $stats, $labelnames, $labelvalues ) = @_;
61  
62 5 foreach my $name ( keys %$stats ) {
63 5 my $value = $stats->{$name};
64 5 ▶ my @values = ( ref $value ) ? @$value : ( $value );
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 1 4 5
65 5 $_ .= _tags( $labelnames, $labelvalues ) for @values;
66 5 $stats->{$name} = \@values
67 }
68  
69 5 $self->SUPER::send( $stats );
70 }
71  
72 # DogStatsd has a native "histogram" format; we'll use that
73 sub report_distribution
74 1 ▶{
Subroutine report_distribution: called CC=2 SCAR=7.0
Pod undocumented
75 1 my $self = shift;
76 1 my ( $handle, $amount, @labelvalues ) = @_;
77  
78 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
79  
80 1 my $value = sprintf "%g|h", $amount;
81  
82 1 $self->send( { $meta->{name} => $value }, $meta->{labels}, \@labelvalues );
83 }
84  
85 2 *inc_distribution_by = \&report_distribution;
86  
87 =head1 AUTHOR
88  
89 Paul Evans <leonerd@leonerd.org.uk>
90  
91 =cut
92  
93 2 0x55AA;
↑ Summary Statsd.pm »