Net/Statsd/Server/Backend/Graphite.pm

filter: Statementstmt 69.8% 148 / 212 Branchbran 33.3% 12 / 36 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 80.9% 17 / 21 Podpod 0.0% 0 / 11 totaltotal 61.2% 180 / 294 SCAR 44.1
CC
32
Cov
63%
post_stats
30.0
flush_stats
22.3
stats_to_string
20.0
CRAP 82.1
« Net/Statsd/Server/Backend/File.pm ↑ Summary Net/Statsd/Server/Metrics.pm »
1 #
2 # ABSTRACT: Flushes stats to graphite (http://graphite.wikidot.com/).
3 #
4 # To enable this backend, include 'graphite' in the backends
5 # configuration array:
6 #
7 # backends: ['graphite']
8 #
9 # This backend supports the following config options:
10 #
11 # graphiteHost: Hostname of graphite server.
12 # graphitePort: Port to contact graphite server at.
13 #
14  
15 package Net::Statsd::Server::Backend::Graphite;
16 {
17 2 $Net::Statsd::Server::Backend::Graphite::VERSION = '0.19';
18 }
19  
20 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use base qw(Net::Statsd::Server::Backend);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 2 ▶use AnyEvent::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use IO::Socket::INET ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 use constant {
31 2 fmt_FLOAT => '%.6f',
32 fmt_INT => '%d',
33 fmt_STR => '%s',
34 fmt_TIME => '%d',
35 2 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 6 ▶sub _dor { defined $_[0] ? $_[0] : $_[1] }
Subroutine _dor: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 5 6
38  
39 sub init {
40 1 ▶ my ($self, $startup_time, $config) = @_;
Subroutine init: called CC=10 SCAR=23.7
Pod undocumented
41  
42 1 for (qw(debug graphiteHost graphitePort)) {
43 3 $self->{$_} = $config->{$_};
44 }
45  
46 1 ▶ $config->{graphite} ||= {};
Condition50%1/2
$config->{"graphite"} ||= {}
l!l
10
MC/DC0%0/1
$config->{"graphite"} ||= {}
$config->{"graphite"}
Truth table50%1/2
A
A= $config->{"graphite"}
Aresult
11
00
47  
48 1 my $globalPrefix = _dor($config->{graphite}->{globalPrefix} , "stats");
49 1 my $prefixCounter = _dor($config->{graphite}->{prefixCounter}, "counters");
50 1 my $prefixTimer = _dor($config->{graphite}->{prefixTimer} , "timers");
51 1 my $prefixGauge = _dor($config->{graphite}->{prefixGauge} , "gauges");
52 1 my $prefixSet = _dor($config->{graphite}->{prefixSet} , "sets");
53 1 my $legacyNamespace = _dor($config->{graphite}->{legacyNamespace} , 1);
54  
55 1 my $globalNamespace = [];
56 1 my $counterNamespace = [];
57 1 my $timerNamespace = [];
58 1 my $gaugesNamespace = [];
59 1 my $setsNamespace = [];
60  
61 1 ▶ if ($legacyNamespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($legacyNamespace) { } 0 1 1
62  
63 0 $globalNamespace = ["stats"];
64 0 $counterNamespace = ["stats"];
65 0 $timerNamespace = ["stats", "timers"];
66 0 $gaugesNamespace = ["stats", "gauges"];
67 0 $setsNamespace = ["stats", "sets"];
68  
69 }
70 else {
71  
72 1 ▶ if ($globalPrefix ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($globalPrefix ne "") 1 0 1
73 1 push @{ $globalNamespace }, $globalPrefix;
74 1 push @{ $counterNamespace }, $globalPrefix;
75 1 push @{ $timerNamespace }, $globalPrefix;
76 1 push @{ $gaugesNamespace }, $globalPrefix;
77 1 push @{ $setsNamespace }, $globalPrefix;
78 }
79  
80 1 ▶ if ($prefixCounter ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefixCounter ne "") 1 0 1
81 1 push @{ $counterNamespace }, $prefixCounter;
82 }
83  
84 1 ▶ if ($prefixTimer ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefixTimer ne "") 1 0 1
85 1 push @{ $timerNamespace }, $prefixTimer;
86 }
87  
88 1 ▶ if ($prefixGauge ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefixGauge ne "") 1 0 1
89 1 push @{ $gaugesNamespace }, $prefixGauge;
90 }
91  
92 1 ▶ if ($prefixSet ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefixSet ne "") 1 0 1
93 1 push @{ $setsNamespace }, $prefixSet;
94 }
95  
96 }
97  
98 1 $self->{globalPrefix} = $globalPrefix;
99 1 $self->{prefixCounter} = $prefixCounter;
100 1 $self->{prefixTimer} = $prefixTimer;
101 1 $self->{prefixGauge} = $prefixGauge;
102 1 $self->{prefixSet} = $prefixSet;
103 1 $self->{legacyNamespace} = $legacyNamespace;
104  
105 1 $self->{globalNamespace} = $globalNamespace;
106 1 $self->{counterNamespace} = $counterNamespace;
107 1 $self->{timerNamespace} = $timerNamespace;
108 1 $self->{gaugesNamespace} = $gaugesNamespace;
109 1 $self->{setsNamespace} = $setsNamespace;
110  
111 #$self->{graphiteStats}->{last_flush} =
112 #$self->{graphiteStats}->{last_exception} = $startup_time;
113  
114 1 $self->{flushInterval} = $config->{flushInterval};
115 1 $self->{prefixStats} = $config->{prefixStats};
116  
117 1 ▶ if (! $self->{prefixStats}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"prefixStats"}) 0 1 1
118 0 Carp::croak("config.prefixStats should not be blank/empty!");
119 }
120  
121 }
122  
123 sub flush {
124 0 ▶ my ($self, $timestamp, $metrics) = @_;
Subroutine flush: not called CC=1 SCAR=6.9
Pod undocumented
125 0 my $flush_stats = $self->flush_stats($timestamp, $metrics);
126 0 $self->post_stats($flush_stats);
127 }
128  
129 sub flush_stats {
130 1 ▶ my ($self, $ts, $metrics) = @_;
Subroutine flush_stats: called CC=12 SCAR=31.0
Pod undocumented
131  
132 1 my $startTime = [ Time::HiRes::gettimeofday ];
133 1 my $statString = "";
134 1 my $num_stats = 0;
135 1 my $timer_data_key;
136  
137 1 my $counters = $metrics->{counters};
138 1 my $gauges = $metrics->{gauges};
139 1 my $timers = $metrics->{timers};
140 1 my $sets = $metrics->{sets};
141 1 my $counter_rates = $metrics->{counter_rates};
142 1 my $timer_data = $metrics->{timer_data};
143 1 my $statsd_metrics = $metrics->{statsd_metrics};
144  
145 # Accumulate flush statistics into a list
146 1 my @fstats;
147  
148 1 for my $key (keys %{ $counters }) {
149  
150 2 my @namespace = (@{ $self->{counterNamespace} }, $key);
151 2 my $namespace = join(".", @namespace);
152  
153 2 my $value = $counters->{$key};
154 2 my $valuePerSecond = $counter_rates->{$key}; # pre-calculated "per second" rate
155  
156 2 ▶ if ($self->{legacyNamespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'legacyNamespace'}) { } 0 2 2
157 0 push @fstats, stat_float($namespace, $valuePerSecond, $ts);
158 0 push @fstats, stat_int("stats_counts.$key", $value, $ts);
159 } else {
160 2 push @fstats, stat_float("$namespace.rate", $valuePerSecond, $ts);
161 2 push @fstats, stat_int("$namespace.count", $value, $ts);
162 }
163  
164 2 $num_stats++;
165 }
166  
167 1 for my $key (keys %{ $timer_data }) {
168 0 ▶ if ($timer_data->{$key} && keys %{ $timer_data->{$key} } > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timer_data->{$key} and keys %{$$timer_data{$key};} > 0) 0 0 0
Condition0%0/3
$timer_data->{$key} and keys %{$$timer_data{$key};} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$timer_data->{$key} and keys %{$$timer_data{$key};} > 0
$timer_data->{$key} keys %{$$timer_data{$key};} > 0
Truth table0%0/3
A and B
A= $timer_data->{$key}B= keys %{$$timer_data{$key};} > 0
ABresult
0X0
100
111
169 0 for my $timer_data_key (keys %{ $timer_data->{$key} }) {
170 0 my @namespace = (@{ $self->{timerNamespace} }, $key);
171 0 my $the_key = join(".", @namespace);
172 push @fstats, stat_float(
173 "$the_key.$timer_data_key",
174 0 $timer_data->{$key}->{$timer_data_key}, $ts
175 );
176 }
177 0 $num_stats++;
178 }
179 }
180  
181 1 for my $key (keys %{ $gauges }) {
182 0 my @namespace = (@{ $self->{gaugesNamespace} }, $key);
183 0 push @fstats, stat_float(join(".", @namespace), $gauges->{$key}, $ts);
184 0 $num_stats++;
185 }
186  
187 1 for my $key (keys %{ $sets }) {
188 0 my @namespace = (@{ $self->{setsNamespace} }, $key);
189 0 my $set_count = join(".", @namespace, "count");
190 0 my $set_len = scalar keys %{ $sets->{$key} };
191 0 push @fstats, stat_int($set_count, $set_len, $ts);
192 0 $num_stats++;
193 }
194  
195 1 my $g_pref = $self->{prefixStats};
196 1 my @namespace = (@{ $self->{globalNamespace} }, $g_pref);
197  
198 # Convert Time::HiRes format (µs) to ms
199 1 my $calcTime = 1000 * Time::HiRes::tv_interval($startTime);
200  
201 1 ▶ if ($self->{legacyNamespace}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'legacyNamespace'}) { } 0 1 1
202 0 push @fstats, stat_int("${g_pref}.numStats", $num_stats, $ts);
203 0 push @fstats, stat_float("stats.${g_pref}.graphiteStats.calculationtime",
204 $calcTime, $ts);
205 0 for my $key (keys %{ $statsd_metrics }) {
206 0 push @fstats, stat_int("stats.${g_pref}.${key}", $statsd_metrics->{$key}, $ts);
207 }
208 }
209 else {
210 1 my $namespace = join(".", @namespace);
211 1 push @fstats, stat_int("${namespace}.numStats", $num_stats, $ts);
212 1 push @fstats, stat_float(
213 "${namespace}.graphiteStats.calculationtime", $calcTime, $ts);
214 1 for my $key (keys %{ $statsd_metrics }) {
215 1 my $value = $statsd_metrics->{$key};
216 1 push @fstats, stat_str("${namespace}.${key}", $value, $ts);
217 }
218 }
219  
220 1 my $global_stats = $self->global_stats();
221 1 push @fstats, @{ $global_stats };
222  
223 1 return \@fstats;
224 }
225  
226 sub global_stats {
227 1 ▶ my ($self) = @_;
Subroutine global_stats: called CC=4 SCAR=14.3
Pod undocumented
228  
229 1 my $g_pref = $self->{prefixStats}; # "statsd" by default
230 1 ▶ if (! $g_pref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($g_pref) 0 1 1
231 0 Carp::croak("config.prefixStats is empty or invalid! (global_stats)");
232 }
233  
234 1 ▶ my $last_flush = $self->{lastFlush} || 0;
Condition50%1/2
$self->{'lastFlush'} || 0
l!l
10
MC/DC0%0/1
$self->{'lastFlush'} || 0
$self->{'lastFlush'}
Truth table50%1/2
A
A= $self->{'lastFlush'}
Aresult
11
00
235 1 ▶ my $last_exception = $self->{lastException} || 0;
Condition50%1/2
$self->{'lastException'} || 0
l!l
10
MC/DC0%0/1
$self->{'lastException'} || 0
$self->{'lastException'}
Truth table50%1/2
A
A= $self->{'lastException'}
Aresult
11
00
236 1 my $ts = time();
237  
238 1 my @namespace = (@{ $self->{globalNamespace} }, $g_pref, 'graphiteStats');
239 1 my $namespace = join(".", @namespace);
240  
241 1 my $global_stats = [
242 stat_time("${namespace}.last_exception", $last_exception, $ts),
243 stat_time("${namespace}.last_flush", $last_flush, $ts),
244 ];
245  
246 1 return $global_stats;
247 }
248  
249 sub post_stats {
250 0 ▶ my ($self, $stat_list) = @_;
Subroutine post_stats: not called CC=5 SCAR=34.0
Pod undocumented
251  
252 0 ▶ return if ! $self->{graphiteHost};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"graphiteHost"} 0 0 0
253  
254 eval {
255 0 my $host = $self->{graphiteHost};
256 0 my $port = $self->{graphitePort};
257 0 ▶ my $graphite = IO::Socket::INET->new(
Branch: 1 branch
BranchTrueFalseTotal
my $graphite = "IO::Socket::INET"->new("PeerHost", $host, "PeerPort", $port) or die "Can't connect to Graphite on ${host}:${port}: $!" 0 0 0
258 PeerHost => $host,
259 PeerPort => $port,
260 ) or die "Can't connect to Graphite on ${host}:${port}: $!";
261  
262 0 my $stat_string = $self->stats_to_string($stat_list);
263 0 $graphite->send($stat_string);
264 0 $graphite->close();
265  
266 0 $self->{lastFlush} = [Time::HiRes::gettimeofday];
267 }
268 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $host = $self->{"graphiteHost"}; my $port = $self->{"graphitePort"}; my $graphite = "IO::Socket::INET"->new("PeerHost", $host, "PeerPort", $port) or die "Can't connect to Graphite on ${host}:${port}: $!"; my $stat_string = $self->stats_to_string($stat_list); $graphite->send($stat_string); $graphite->close; $self->{"lastFlush"} = [&Time::HiRes::gettimeofday()] } } or do { if ($self->{"debug"}) { warn "Exception while posting stats to Graphite: $@"; } ; $self->{"lastException"} = [&Time::HiRes::gettimeofday()] } 0 0 0
269 0 ▶ if ($self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
270 # TODO use logger!
271 0 warn("Exception while posting stats to Graphite: $@");
272 }
273 0 $self->{lastException} = [Time::HiRes::gettimeofday];
274 };
275  
276 }
277  
278 sub stat_float {
279 3 ▶ my ($stat, $val, $ts) = @_;
Subroutine stat_float: called CC=1 SCAR=0.0
Pod undocumented
280 return {
281 3 stat => $stat,
282 value => $val,
283 time => $ts,
284 fmt => fmt_FLOAT,
285 };
286 }
287  
288 sub stat_int {
289 3 ▶ my ($stat, $val, $ts) = @_;
Subroutine stat_int: called CC=1 SCAR=0.0
Pod undocumented
290 return {
291 3 stat => $stat,
292 value => $val,
293 time => $ts,
294 fmt => fmt_INT,
295 };
296 }
297  
298 sub stat_str {
299 1 ▶ my ($stat, $val, $ts) = @_;
Subroutine stat_str: called CC=1 SCAR=0.0
Pod undocumented
300 return {
301 1 stat => $stat,
302 value => $val,
303 time => $ts,
304 fmt => fmt_STR,
305 };
306 }
307  
308 sub stat_time {
309 2 ▶ my ($stat, $val, $ts) = @_;
Subroutine stat_time: called CC=1 SCAR=0.0
Pod undocumented
310 return {
311 2 stat => $stat,
312 value => $val,
313 time => $ts,
314 fmt => fmt_TIME,
315 };
316 }
317  
318 sub stats_to_string {
319 0 ▶ my ($self, $stat_list) = @_;
Subroutine stats_to_string: not called CC=4 SCAR=30.0
Pod undocumented
320 0 my $stat_string = "";
321 0 for (@{ $stat_list }) {
322 0 my $attr = $_;
323 0 my $stat = $attr->{stat};
324 0 my $val = $attr->{value};
325 0 ▶ next if ! defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
326 0 my $ts = $attr->{time};
327 0 ▶ my $fmt = exists $attr->{fmt} ? $attr->{fmt} : '%d';
Branch: 1 branch
BranchTrueFalseTotal
exists $attr->{'fmt'} ? : 0 0 0
328 #warn "fmt=$fmt stat=$stat val=$val ts=$ts\n";
329 0 $stat_string .= sprintf("%s $fmt %d\n", $stat, $val, $ts);
330 }
331 0 return $stat_string;
332 }
333  
334 sub status {
335 0 ▶ my ($self) = @_;
Subroutine status: not called CC=1 SCAR=6.9
Pod undocumented
336 return {
337 last_flush => $self->since($self->{lastFlush}),
338 0 last_exception => $self->since($self->{lastException}),
339 };
340 }
341  
342 2 1;
« Net/Statsd/Server/Backend/File.pm ↑ Summary Net/Statsd/Server/Metrics.pm »