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   2316 use strict;
  2         4  
  2         60  
9 2     2   10 use warnings;
  2         4  
  2         59  
10 2     2   9 use base qw( Metrics::Any::Adapter::Statsd );
  2         4  
  2         597  
11              
12             our $VERSION = '0.01';
13              
14 2     2   51 use Carp;
  2         5  
  2         752  
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   7 my ( $labels, $labelvalues ) = @_;
38              
39 4         6 my @tags;
40 4         11 foreach ( 0 .. $#$labels ) {
41 4         13 push @tags, "$labels->[$_]:$labelvalues->[$_]";
42             }
43              
44 4 50       10 return "" unless @tags;
45 4         16 return "#" . join( ",", @tags );
46             }
47              
48             sub send
49             {
50 4     4 0 8 my $self = shift;
51 4         7 my ( $stats, $labelnames, $labelvalues ) = @_;
52              
53 4         12 foreach my $name ( keys %$stats ) {
54 4         8 my $value = $stats->{$name};
55 4 100       13 my @values = ( ref $value ) ? @$value : ( $value );
56 4         10 $_ .= _tags( $labelnames, $labelvalues ) for @values;
57 4         11 $stats->{$name} = \@values
58             }
59              
60 4         14 $self->SUPER::send( $stats );
61             }
62              
63             # DogStatsd has a native "histogram" format; we'll use that
64             sub inc_distribution_by
65             {
66 1     1 0 16 my $self = shift;
67 1         3 my ( $handle, $amount, @labelvalues ) = @_;
68              
69 1 50       4 my $meta = $self->{metrics}{$handle} or croak "No metric '$handle'";
70              
71 1         8 my $value = sprintf "%g|h", $amount;
72              
73 1         4 $self->send( { $meta->{name} => $value }, $meta->{labels}, \@labelvalues );
74             }
75              
76             =head1 AUTHOR
77              
78             Paul Evans
79              
80             =cut
81              
82             0x55AA;