lib/Net/Statsite/Client.pm

filter: Statementstmt 59.3% 38 / 64 Branchbran 43.3% 13 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 71.4% 10 / 14 Podpod 100% 7 / 7 totaltotal 56.6% 68 / 120 CC 17 SCAR 33.8
CC
17
Cov
53%
send
90.0
new
7.2
update
3.0
CRAP 29.4
↑ Summary
1 package Net::Statsite::Client;
2 1 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.1.0';
7  
8 1 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Net::Statsite::Client - Object-Oriented Client for L<statsite|http://armon.github.io/statsite> server
14  
15 =head1 SYNOPSIS
16  
17 use Net::Statsite::Client;
18 my $statsite = Net::Statsite::Client->new(
19 host => 'localhost',
20 prefix => 'test',
21 );
22  
23 $statsite->increment('item'); #increment key test.item
24  
25 =head1 DESCRIPTION
26  
27 Net::Statsite::Client is based on L<Etsy::StatsD> but with new - C<new> interface and C<unique> method.
28  
29  
30 =head1 METHODS
31  
32 =head2 new (host => $host, port => $port, sample_rate => $sample_rate, prefix => $prefix)
33  
34 Create a new instance.
35  
36 I<host> - hostname of statsite server (default: localhost)
37  
38 I<port> - port of statsite server (port: 8125)
39  
40 I<sample_rate> - rate of sends metrics (default: 1)
41  
42 I<prefix> - prefix metric name (default: '')
43  
44 I<proto> - protocol (default: 'udp')
45  
46 =cut
47  
48 4 ▶sub new {
Subroutine new: called CC=7 SCAR=19.7
Pod documented
49 4 my ($class, %options) = @_;
50 4 ▶ $options{host} = 'localhost' unless defined $options{host};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"host"} 4 0 4
51 4 ▶ $options{port} = 8125 unless defined $options{port};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"port"} 2 2 4
52 4 ▶ $options{prefix} = '' unless defined $options{prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"prefix"} 4 0 4
53 4 ▶ $options{proto} = 'udp' unless defined $options{proto};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"proto"} 2 2 4
54  
55 4 ▶ die "Invalid protocol '$options{proto}' (tcp|udp)" if $options{proto} !~ /^(?:tcp|udp)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"proto"} =~ /^(?:tcp|udp)$/ 1 3 4
56  
57 my $sock = IO::Socket::INET->new(
58 PeerAddr => $options{host},
59 PeerPort => $options{port},
60 Proto => $options{proto},
61 3 ▶ ) or croak "Failed to initialize socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerAddr", $options{"host"}, "PeerPort", $options{"port"}, "Proto", $options{"proto"}) or croak("Failed to initialize socket: $!") 3 0 3
62  
63 3 bless { socket => $sock, sample_rate => $options{sample_rate}, prefix => $options{prefix} }, $class;
64 }
65  
66 =head2 timing(STAT, TIME, SAMPLE_RATE)
67  
68 Log timing information (should be in miliseconds)
69  
70 =cut
71  
72 1 ▶sub timing {
Subroutine timing: called CC=1 SCAR=0.0
Pod documented
73 1 my ($self, $stat, $time, $sample_rate) = @_;
74 1 $self->send({ $stat => "$time|ms" }, $sample_rate);
75 }
76  
77 =head2 increment(STATS, SAMPLE_RATE)
78  
79 Increment one of more stats counters.
80  
81 =cut
82  
83 1 ▶sub increment {
Subroutine increment: called CC=1 SCAR=0.0
Pod documented
84 1 my ($self, $stats, $sample_rate) = @_;
85 1 $self->update($stats, 1, $sample_rate);
86 }
87  
88 =head2 decrement(STATS, SAMPLE_RATE)
89  
90 Decrement one of more stats counters.
91  
92 =cut
93  
94 1 ▶sub decrement {
Subroutine decrement: called CC=1 SCAR=0.0
Pod documented
95 1 my ($self, $stats, $sample_rate) = @_;
96 1 $self->update($stats, -1, $sample_rate);
97 }
98  
99 =head2 update(STATS, DELTA, SAMPLE_RATE)
100  
101 Update one of more stats counters by arbitrary amounts.
102  
103 =cut
104  
105 5 ▶sub update {
Subroutine update: called CC=3 SCAR=11.0
Pod documented
106 5 my ($self, $stats, $delta, $sample_rate) = @_;
107 5 ▶ $delta = 1 unless defined $delta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delta 2 3 5
108 5 my %data;
109 5 ▶ if (ref($stats) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stats eq 'ARRAY') { } 1 4 5
110 1 %data = map { $_ => "$delta|c" } @$stats;
111 }
112 else {
113 4 %data = ($stats => "$delta|c");
114 }
115 5 $self->send(\%data, $sample_rate);
116 }
117  
118 =head2 unique(STATS, ITEM, SAMPLE_RATE)
119  
120 Unique Set
121  
122 For example if you need count of unique ip adresses (per flush interval)
123 $stats->unique('ip.unique', $ip);
124  
125 =cut
126  
127 0 ▶sub unique {
Subroutine unique: not called CC=1 SCAR=6.9
Pod documented
128 0 my ($self, $stats, $item, $sample_rate) = @_;
129 0 my %data = ($stats => "$item|s");
130 0 $self->send(\%data, $sample_rate);
131 }
132  
133 =head2 gauge(STATS, VALUE, SAMPLE_RATE)
134  
135 Gauge Set (Gauge, similar to kv but only the last value per key is retained)
136  
137 =cut
138  
139 0 ▶sub gauge {
Subroutine gauge: not called CC=1 SCAR=6.9
Pod documented
140 0 my ($self, $stats, $value, $sample_rate) = @_;
141 0 my %data = ($stats => "$value|g");
142 0 $self->send(\%data, $sample_rate);
143 }
144  
145 =head2 send(DATA, SAMPLE_RATE)
146  
147 Sending logging data; implicitly called by most of the other methods.
148  
149 =cut
150  
151 0 ▶sub send {
Subroutine send: not called CC=9 SCAR=45.0
152 0 my ($self, $data, $sample_rate) = @_;
153 0 ▶ $sample_rate = $self->{sample_rate} unless defined $sample_rate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sample_rate 0 0 0
154  
155 0 my $sampled_data;
156 0 ▶ if (defined($sample_rate) and $sample_rate < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sample_rate and $sample_rate < 1) { } 0 0 0
Condition0%0/3
defined $sample_rate and $sample_rate < 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sample_rate and $sample_rate < 1
defined $sample_rate $sample_rate < 1
Truth table0%0/3
A and B
A= defined $sample_rateB= $sample_rate < 1
ABresult
0X0
100
111
157 0 ▶ while (my ($stat, $value) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 0 0 0
158 0 ▶ $sampled_data->{$stat} = "$value|\@$sample_rate" if rand() <= $sample_rate;
Branch: 1 branch
BranchTrueFalseTotal
if rand() <= $sample_rate 0 0 0
159 }
160 }
161 else {
162 0 $sampled_data = $data;
163 }
164  
165 0 ▶ return '0 but true' unless keys %$sampled_data;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$sampled_data 0 0 0
166  
167 #failures in any of this can be silently ignored
168 0 my $count = 0;
169 0 my $socket = $self->{socket};
170 0 ▶ while (my ($stat, $value) = each %$sampled_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$sampled_data) 0 0 0
171  
172 0 my $key = $stat;
173 0 ▶ if ($$self{prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"prefix"}) 0 0 0
174 0 $key = "$$self{ prefix }.$stat";
175 }
176  
177 #sanitize key (remove statsite separators)
178 #https://github.com/armon/statsite#protocol
179 0 $key =~ s/[:|\/]/_/g;
180  
181 0 _send_to_sock($socket, "$key:$value\n");
182 0 ++$count;
183 }
184 0 return $count;
185 }
186  
187 0 ▶sub _send_to_sock( $$ ) {
Subroutine _send_to_sock: not called CC=1 SCAR=6.9
188 0 my ($sock, $msg) = @_;
189 0 CORE::send($sock, $msg, 0);
190 }
191  
192 =head1 CONTRIBUTING
193  
194 the easiest way is use docker (L<avastsoftware/perl-extended|https://hub.docker.com/r/avastsoftware/perl-extended/> - with L<Carton> and L<Minilla>)
195  
196 or L<Carton> and C<Minilla> itself (commands after C<../perl-extended>)
197  
198 carton (aka ruby bundle) for fetch dependency
199  
200 docker run -v $PWD:/tmp/app -w /tmp/app avastsoftware/perl-extended carton
201  
202 and minil test for tests and regenerate meta and readme
203  
204 docker run -v $PWD:/tmp/app -w /tmp/app avastsoftware/perl-extended carton exec minil test
205  
206  
207 =head1 LICENSE
208  
209 Copyright (C) Avast Software.
210  
211 This library is free software; you can redistribute it and/or modify
212 it under the same terms as Perl itself.
213  
214 =head1 AUTHOR
215  
216 Jan Seidl E<lt>seidl@avast.comE<gt>
217  
218 =cut
219  
220 1 1;
↑ Summary