DogStatsd.pm

filter: Statementstmt 98.0% 102 / 104 Branchbran 77.2% 17 / 22 Conditioncond 71.8% 23 / 32 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 100% 17 / 17 Podpod 76.9% 10 / 13 totaltotal 85.8% 176 / 205 CC 28 SCAR 13.5
CC
28
Cov
90%
new
7.1
send_stats
7.1
format_event
5.0
CRAP 3.9
↑ Summary DogStatsd/Helper.pm »
1 package DataDog::DogStatsd;
2  
3 # ABSTRACT: A Perl client for DogStatsd
4  
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 our $VERSION = '0.07';
9  
10 6 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 my %OPTS_KEYS = (
13 date_happened => 'd',
14 hostname => 'h',
15 aggregation_key => 'k',
16 priority => 'p',
17 source_type_name => 's',
18 alert_type => 't'
19 );
20  
21 4 ▶sub new { ## no critic (Subroutines::RequireArgUnpacking)
Subroutine new: called CC=6 SCAR=19.6
Pod documented
22 4 my $classname = shift;
23 4 ▶ my $class = ref($classname) || $classname;
Condition33%1/3
ref $classname || $classname
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $classname || $classname
ref $classname $classname
Truth table33%1/3
A || B
A= ref $classnameB= $classname
ABresult
1X1
011
000
24 0 ▶ my %p = @_ % 2 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 4 4
25  
26 4 ▶ $p{host} ||= '127.0.0.1';
Condition50%1/2
$p{"host"} ||= "127.0.0.1"
l!l
04
MC/DC0%0/1
$p{"host"} ||= "127.0.0.1"
$p{"host"}
Truth table50%1/2
A
A= $p{"host"}
Aresult
11
00
27 4 ▶ $p{port} ||= 8125;
Condition100%2/2
$p{"port"} ||= 8125
l!l
22
MC/DC100%1/1
$p{"port"} ||= 8125
$p{"port"}
Truth table100%2/2
A
A= $p{"port"}
Aresult
11
00
28 4 ▶ $p{namespace} ||= '';
Condition50%1/2
$p{"namespace"} ||= ""
l!l
04
MC/DC0%0/1
$p{"namespace"} ||= ""
$p{"namespace"}
Truth table50%1/2
A
A= $p{"namespace"}
Aresult
11
00
29  
30 4 return bless \%p, $class;
31 }
32  
33 57 ▶sub _socket {
Subroutine _socket: called CC=2 SCAR=6.9
34 57 my $self = shift;
35 57 ▶ return $self->{_socket} if $self->{_socket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_socket"} 53 4 57
36 $self->{_socket} = IO::Socket::INET->new(
37 PeerAddr => $self->{'host'},
38 4 PeerPort => $self->{'port'},
39 Proto => 'udp'
40 );
41 4 return $self->{_socket};
42 }
43  
44 2 ▶sub namespace {
Subroutine namespace: called CC=2 SCAR=7.1
Pod documented
45 2 my $self = shift;
46 2 ▶ return $self->{'namespace'} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
47 2 $self->{'namespace'} = shift;
48 }
49  
50 13 ▶sub increment {
Subroutine increment: called CC=2 SCAR=6.9
Pod documented
51 13 my $self = shift;
52 13 my $stat = shift;
53 13 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
76
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
54 13 $self->count($stat, 1, $opts);
55 }
56  
57 9 ▶sub decrement {
Subroutine decrement: called CC=2 SCAR=6.9
Pod documented
58 9 my $self = shift;
59 9 my $stat = shift;
60 9 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
36
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
61 9 $self->count($stat, -1, $opts);
62 }
63  
64 25 ▶sub count {
Subroutine count: called CC=2 SCAR=6.9
Pod documented
65 25 my $self = shift;
66 25 my $stat = shift;
67 25 my $count = shift;
68 25 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
223
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
69 25 $self->send_stats($stat, $count, 'c', $opts);
70 }
71  
72 9 ▶sub gauge {
Subroutine gauge: called CC=2 SCAR=6.9
Pod documented
73 9 my $self = shift;
74 9 my $stat = shift;
75 9 my $value = shift;
76 9 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
36
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
77 9 $self->send_stats($stat, $value, 'g', $opts);
78 }
79  
80 2 ▶sub histogram {
Subroutine histogram: called CC=2 SCAR=7.0
Pod documented
81 2 my $self = shift;
82 2 my $stat = shift;
83 2 my $value = shift;
84 2 ▶ my $opts = shift || {};
Condition50%1/2
shift() || {}
l!l
02
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
85 2 $self->send_stats($stat, $value, 'h', $opts);
86 }
87  
88 12 ▶sub timing {
Subroutine timing: called CC=2 SCAR=6.9
Pod documented
89 12 my $self = shift;
90 12 my $stat = shift;
91 12 my $ms = shift;
92 12 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
66
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
93 12 $self->send_stats($stat, sprintf("%d", $ms), 'ms', $opts);
94 }
95  
96 # Reports execution time of the provided block using {#timing}.
97 #
98 # @param [String] stat stat name
99 # @param [Hash] opts the options to create the metric with
100 # @option opts [Numeric] :sample_rate sample rate, 1 for always
101 # @option opts [Array<String>] :tags An array of tags
102 # @yield The operation to be timed
103 # @see #timing
104 # @example Report the time (in ms) taken to activate an account
105 # $statsd.time('account.activate') { @account.activate! }
106 ##def time(stat, opts={})
107 ## start = Time.now
108 ## result = yield
109 ## timing(stat, ((Time.now - start) * 1000).round, opts)
110 ## result
111 ##end
112  
113 1 ▶sub set {
Subroutine set: called CC=2 SCAR=7.0
Pod documented
114 1 my $self = shift;
115 1 my $stat = shift;
116 1 my $value = shift;
117 1 ▶ my $opts = shift || {};
Condition50%1/2
shift() || {}
l!l
01
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
118 1 $self->send_stats($stat, $value, 's', $opts);
119 }
120  
121 8 ▶sub event {
Subroutine event: called CC=2 SCAR=6.9
Pod documented
122 8 my $self = shift;
123 8 my $title = shift;
124 8 my $text = shift;
125 8 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
44
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
126  
127 8 my $event_string = format_event($title, $text, $opts);
128  
129 8 $self->send_to_socket($event_string);
130 }
131  
132 8 ▶sub format_event {
Subroutine format_event: called CC=5 SCAR=16.1
Pod undocumented
133 8 my $title = shift;
134 8 my $text = shift;
135 8 ▶ my $opts = shift || {};
Condition50%1/2
shift() || {}
l!l
80
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
136  
137 8 my $tags = delete $opts->{tags};
138  
139 8 $title =~ s/\n/\\n/g;
140 8 $text =~ s/\n/\\n/g;
141  
142 8 my $event_string_data = sprintf "_e{%d,%d}:%s|%s", length($title), length($text), $title, $text;
143  
144 8 for my $opt (keys %$opts) {
145 3 ▶ if (my $key = $OPTS_KEYS{$opt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key = $OPTS_KEYS{$opt}) 3 0 3
146 3 $opts->{$opt} =~ s/|//g;
147 3 $event_string_data .= "|$key:$opts->{$opt}";
148 }
149 }
150  
151 8 ▶ if ($tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags) 4 4 8
152 4 $event_string_data .= "|#" . join ",", map { s/|//g; $_ } @$tags; ## no critic (ControlStructures::ProhibitMutatingListFunctions)
153 }
154  
155 8 return $event_string_data;
156 }
157  
158 49 ▶sub send_stats {
Subroutine send_stats: called CC=7 SCAR=19.5
Pod undocumented
159 49 my $self = shift;
160 49 my $stat = shift;
161 49 my $delta = shift;
162 49 my $type = shift;
163 49 ▶ my $opts = shift || {};
Condition50%1/2
shift() || {}
l!l
490
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
164  
165 49 ▶ my $sample_rate = defined $opts->{'sample_rate'} ? $opts->{'sample_rate'} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'sample_rate'} ? : 2 47 49
166 49 ▶ if ($sample_rate == 1 || rand() <= $sample_rate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sample_rate == 1 or rand() <= $sample_rate) 49 0 49
Condition66%2/3
$sample_rate == 1 or rand() <= $sample_rate
l!l&&r!l&&!r
4720
MC/DC0%0/2
$sample_rate == 1 or rand() <= $sample_rate
$sample_rate == 1 rand() <= $sample_rate
Truth table66%2/3
A or B
A= $sample_rate == 1B= rand() <= $sample_rate
ABresult
1X1
011
000
167 49 $stat =~ s/::/./g;
168 49 $stat =~ s/[:|@]/_/g;
169 49 my $rate = '';
170 49 ▶ $rate = "|\@${sample_rate}" unless $sample_rate == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $sample_rate == 1 2 47 49
171 49 my $tags = '';
172 49 ▶ $tags = "|#" . join(',', @{$opts->{'tags'}}) if $opts->{'tags'};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"tags"} 17 32 49
173 49 my $message = $self->{'namespace'} . "${stat}:${delta}|${type}${rate}${tags}";
174 49 return $self->send_to_socket($message);
175 }
176 }
177  
178 57 ▶sub send_to_socket {
Subroutine send_to_socket: called CC=3 SCAR=11.2
Pod undocumented
179 57 my ($self, $message) = @_;
180  
181 57 my $r = send($self->_socket(), $message, 0);
182 57 ▶ if (!defined $r) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $r) { } 9 48 57
elsif ($r != length $message) { } 0 48 48
183 9 return 0;
184 } elsif ($r != length($message)) {
185 0 return 0;
186 }
187  
188 48 return 1;
189 }
190  
191 6 1;
192 __END__
↑ Summary DogStatsd/Helper.pm »