File Coverage

blib/lib/Net/Statsd/Client.pm
Criterion Covered Total %
statement 41 41 100.0
branch 8 14 57.1
condition n/a
subroutine 12 12 100.0
pod 7 8 87.5
total 68 75 90.6


line stmt bran cond sub pod time code
1             package Net::Statsd::Client;
2 3     3   3993 use Moo;
  3         34478  
  3         16  
3 3     3   5996 use Sub::Quote;
  3         14555  
  3         254  
4              
5             # ABSTRACT: Send data to StatsD / Graphite
6             our $VERSION = '0.34'; # VERSION
7             our $AUTHORITY = 'cpan:ARODLAND'; # AUTHORITY
8              
9 3     3   1426 use Etsy::StatsD 1.001;
  3         64981  
  3         107  
10 3     3   1589 use Net::Statsd::Client::Timer;
  3         12  
  3         2101  
11              
12             has 'prefix' => (
13             is => 'ro',
14             default => quote_sub q{''},
15             );
16              
17             has 'sample_rate' => (
18             is => 'ro',
19             default => quote_sub q{1},
20             );
21              
22             has 'host' => (
23             is => 'ro',
24             default => quote_sub q{'localhost'},
25             );
26              
27             has 'port' => (
28             is => 'ro',
29             default => quote_sub q{8125},
30             );
31              
32             has 'statsd' => (
33             is => 'rw',
34             );
35              
36             has 'warning_callback' => (
37             is => 'rw',
38             );
39              
40             sub BUILD {
41 4     4 0 13073 my ($self) = @_;
42 4         69 $self->statsd(
43             Etsy::StatsD->new($self->host, $self->port)
44             );
45             }
46              
47             sub increment {
48 2     2 1 1775 my ($self, $metric, $sample_rate) = @_;
49 2         7 $metric = "$self->{prefix}$metric";
50 2 50       12 $sample_rate = $self->{sample_rate} unless defined $sample_rate;
51 2         10 $self->{statsd}->increment($metric, $sample_rate);
52             }
53              
54             sub decrement {
55 2     2 1 1609 my ($self, $metric, $sample_rate) = @_;
56 2         8 $metric = "$self->{prefix}$metric";
57 2 50       10 $sample_rate = $self->{sample_rate} unless defined $sample_rate;
58 2         19 $self->{statsd}->decrement($metric, $sample_rate);
59             }
60              
61             sub update {
62 2     2 1 1335 my ($self, $metric, $value, $sample_rate) = @_;
63 2         6 $metric = "$self->{prefix}$metric";
64 2 50       10 $sample_rate = $self->{sample_rate} unless defined $sample_rate;
65 2         9 $self->{statsd}->update($metric, $value, $sample_rate);
66             }
67              
68             sub timing_ms {
69 7     7 1 1307 my ($self, $metric, $time, $sample_rate) = @_;
70 7         28 $metric = "$self->{prefix}$metric";
71 7 100       36 $sample_rate = $self->{sample_rate} unless defined $sample_rate;
72 7         52 $self->{statsd}->timing($metric, $time, $sample_rate);
73             }
74              
75             sub gauge {
76 1     1 1 1130 my ($self, $metric, $value, $sample_rate) = @_;
77 1         4 $metric = "$self->{prefix}$metric";
78 1 50       6 $sample_rate = $self->{sample_rate} unless defined $sample_rate;
79 1         7 $self->{statsd}->send({ $metric => "$value|g" }, $sample_rate);
80             }
81              
82             sub set_add {
83 1     1 1 641 my ($self, $metric, $value, $sample_rate) = @_;
84 1         4 $metric = "$self->{prefix}$metric";
85 1 50       5 $sample_rate = $self->{sample_rate} unless defined $sample_rate;
86 1         9 $self->{statsd}->send({ $metric => "$value|s" }, $sample_rate);
87             }
88              
89             sub timer {
90 6     6 1 4854 my ($self, $metric, $sample_rate) = @_;
91 6 50       30 $sample_rate = $self->{sample_rate} unless defined $sample_rate;
92              
93 6         108 return Net::Statsd::Client::Timer->new(
94             statsd => $self,
95             metric => $metric,
96             sample_rate => $sample_rate,
97             warning_callback => $self->warning_callback,
98             );
99             }
100              
101             1;
102              
103             __END__