File Coverage

blib/lib/Metrics/Any/Adapter/DogStatsd.pm
Criterion Covered Total %
statement 31 31 100.0
branch 4 6 66.6
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 42 46 91.3


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