lib/Net/Statsite/Client.pm

filter: Statementstmt 58.0% 36 / 62 Branchbran 34.6% 9 / 26 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 71.4% 10 / 14 Podpod 100% 7 / 7 totaltotal 54.3% 62 / 114 SCAR 37.9
CC
15
Cov
49%
send
90.0
new
5.2
update
3.0
CRAP 44.1
« blib/lib/Net/Statsite/Client.pm ↑ Summary Build »
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.0.2';
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 =cut
45  
46 sub new {
47 2 ▶ my ($class, %options) = @_;
Subroutine new: called CC=5 SCAR=16.6
Pod documented
48 2 ▶ $options{host} = 'localhost' unless defined $options{host};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"host"} 2 0 2
49 2 ▶ $options{port} = 8125 unless defined $options{port};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"port"} 1 1 2
50 2 ▶ $options{prefix} = '' unless defined $options{prefix};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"prefix"} 2 0 2
51  
52 my $sock = new IO::Socket::INET(
53 PeerAddr => $options{host},
54 PeerPort => $options{port},
55 2 ▶ Proto => 'udp',
Branch: 1 branch
BranchTrueFalseTotal
my $sock = "IO::Socket::INET"->new("PeerAddr", $options{"host"}, "PeerPort", $options{"port"}, "Proto", "udp") or croak("Failed to initialize socket: $!") 0 2 2
56 ) or croak "Failed to initialize socket: $!";
57  
58 2 bless { socket => $sock, sample_rate => $options{sample_rate}, prefix => $options{prefix} }, $class;
59 }
60  
61 =head2 timing(STAT, TIME, SAMPLE_RATE)
62  
63 Log timing information (should be in miliseconds)
64  
65 =cut
66  
67 sub timing {
68 1 ▶ my ($self, $stat, $time, $sample_rate) = @_;
Subroutine timing: called CC=1 SCAR=0.0
Pod documented
69 1 $self->send({ $stat => "$time|ms" }, $sample_rate);
70 }
71  
72 =head2 increment(STATS, SAMPLE_RATE)
73  
74 Increment one of more stats counters.
75  
76 =cut
77  
78 sub increment {
79 1 ▶ my ($self, $stats, $sample_rate) = @_;
Subroutine increment: called CC=1 SCAR=0.0
Pod documented
80 1 $self->update($stats, 1, $sample_rate);
81 }
82  
83 =head2 decrement(STATS, SAMPLE_RATE)
84  
85 Decrement one of more stats counters.
86  
87 =cut
88  
89 sub decrement {
90 1 ▶ my ($self, $stats, $sample_rate) = @_;
Subroutine decrement: called CC=1 SCAR=0.0
Pod documented
91 1 $self->update($stats, -1, $sample_rate);
92 }
93  
94 =head2 update(STATS, DELTA, SAMPLE_RATE)
95  
96 Update one of more stats counters by arbitrary amounts.
97  
98 =cut
99  
100 sub update {
101 5 ▶ my ($self, $stats, $delta, $sample_rate) = @_;
Subroutine update: called CC=3 SCAR=11.0
Pod documented
102 5 ▶ $delta = 1 unless defined $delta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delta 2 3 5
103 5 my %data;
104 5 ▶ if (ref($stats) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $stats eq 'ARRAY') { } 1 4 5
105 1 %data = map { $_ => "$delta|c" } @$stats;
106 }
107 else {
108 4 %data = ($stats => "$delta|c");
109 }
110 5 $self->send(\%data, $sample_rate);
111 }
112  
113 =head2 unique(STATS, ITEM, SAMPLE_RATE)
114  
115 Unique Set
116  
117 For example if you need count of unique ip adresses (per flush interval)
118 $stats->unique('ip.unique', $ip);
119  
120 =cut
121  
122 sub unique {
123 0 ▶ my ($self, $stats, $item, $sample_rate) = @_;
Subroutine unique: not called CC=1 SCAR=6.9
Pod documented
124 0 my %data = ($stats => "$item|s");
125 0 $self->send(\%data, $sample_rate);
126 }
127  
128 =head2 gauge(STATS, VALUE, SAMPLE_RATE)
129  
130 Gauge Set (Gauge, similar to kv but only the last value per key is retained)
131  
132 =cut
133  
134 sub gauge {
135 0 ▶ my ($self, $stats, $value, $sample_rate) = @_;
Subroutine gauge: not called CC=1 SCAR=6.9
Pod documented
136 0 my %data = ($stats => "$value|g");
137 0 $self->send(\%data, $sample_rate);
138 }
139  
140 =head2 send(DATA, SAMPLE_RATE)
141  
142 Sending logging data; implicitly called by most of the other methods.
143  
144 =cut
145  
146 sub send {
147 0 ▶ my ($self, $data, $sample_rate) = @_;
Subroutine send: not called CC=9 SCAR=45.0
148 0 ▶ $sample_rate = $self->{sample_rate} unless defined $sample_rate;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sample_rate 0 0 0
149  
150 0 my $sampled_data;
151 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
152 0 ▶ while (my ($stat, $value) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 0 0 0
153 0 ▶ $sampled_data->{$stat} = "$value|\@$sample_rate" if rand() <= $sample_rate;
Branch: 1 branch
BranchTrueFalseTotal
if rand() <= $sample_rate 0 0 0
154 }
155 }
156 else {
157 0 $sampled_data = $data;
158 }
159  
160 0 ▶ return '0 but true' unless keys %$sampled_data;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$sampled_data 0 0 0
161  
162 #failures in any of this can be silently ignored
163 0 my $count = 0;
164 0 my $socket = $self->{socket};
165 0 ▶ while (my ($stat, $value) = each %$sampled_data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$sampled_data) 0 0 0
166  
167 0 my $key = $stat;
168 0 ▶ if ($$self{prefix}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"prefix"}) 0 0 0
169 0 $key = "$$self{ prefix }.$stat";
170 }
171  
172 #sanitize key (remove statsite separators)
173 #https://github.com/armon/statsite#protocol
174 0 $key =~ s/[:|]/_/g;
175  
176 0 _send_to_sock($socket, "$key:$value\n");
177 0 ++$count;
178 }
179 0 return $count;
180 }
181  
182 sub _send_to_sock( $$ ) {
183 0 ▶ my ($sock, $msg) = @_;
Subroutine _send_to_sock: not called CC=1 SCAR=6.9
184 0 CORE::send($sock, $msg, 0);
185 }
186  
187 =head1 LICENSE
188  
189 Copyright (C) Avast Software.
190  
191 This library is free software; you can redistribute it and/or modify
192 it under the same terms as Perl itself.
193  
194 =head1 AUTHOR
195  
196 Jan Seidl E<lt>seidl@avast.comE<gt>
197  
198 =cut
199  
200 1 1;
« blib/lib/Net/Statsite/Client.pm ↑ Summary Build »