File Coverage

blib/lib/Metrics/Any/Adapter/DogStatsd.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 6 66.6
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 41 45 91.1


line stmt bran cond sub pod time code
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-2021 -- leonerd@leonerd.org.uk
5              
6             package Metrics::Any::Adapter::DogStatsd 0.03;
7              
8 2     2   2922 use v5.14;
  2         7  
9 2     2   13 use warnings;
  2         3  
  2         81  
10 2     2   13 use base qw( Metrics::Any::Adapter::Statsd );
  2         5  
  2         704  
11              
12 2     2   14 use Carp;
  2         5  
  2         781  
13              
14             # See also
15             # https://metacpan.org/release/DataDog-DogStatsd/source/lib/DataDog/DogStatsd.pm
16              
17             =head1 NAME
18              
19             C - a metrics reporting adapter for DogStatsd
20              
21             =head1 SYNOPSIS
22              
23             use Metrics::Any::Adapter 'DogStatsd';
24              
25             This extension of L supports the additional tag
26             reporting syntax defined by F to report labelled metrics.
27              
28             Additionally, distribution metrics are reported as native DogStatsd histograms
29             rather than the two-part count-and-sum implementation of plain statsd.
30              
31             =cut
32              
33             sub _tags
34             {
35 4     4   7 my ( $labels, $labelvalues ) = @_;
36              
37 4         7 my @tags;
38 4         10 foreach ( 0 .. $#$labels ) {
39 4         15 push @tags, "$labels->[$_]:$labelvalues->[$_]";
40             }
41              
42 4 50       11 return "" unless @tags;
43 4         16 return "|#" . join( ",", @tags );
44             }
45              
46             sub send
47             {
48 4     4 0 7 my $self = shift;
49 4         8 my ( $stats, $labelnames, $labelvalues ) = @_;
50              
51 4         11 foreach my $name ( keys %$stats ) {
52 4         8 my $value = $stats->{$name};
53 4 100       14 my @values = ( ref $value ) ? @$value : ( $value );
54 4         11 $_ .= _tags( $labelnames, $labelvalues ) for @values;
55 4         12 $stats->{$name} = \@values
56             }
57              
58 4         16 $self->SUPER::send( $stats );
59             }
60              
61             # DogStatsd has a native "histogram" format; we'll use that
62             sub report_distribution
63             {
64 1     1 0 43 my $self = shift;
65 1         3 my ( $handle, $amount, @labelvalues ) = @_;
66              
67 1 50       3 my $meta = $self->{metrics}{$handle} or croak "No metric '$handle'";
68              
69 1         9 my $value = sprintf "%g|h", $amount;
70              
71 1         4 $self->send( { $meta->{name} => $value }, $meta->{labels}, \@labelvalues );
72             }
73              
74             *inc_distribution_by = \&report_distribution;
75              
76             =head1 AUTHOR
77              
78             Paul Evans
79              
80             =cut
81              
82             0x55AA;