Net/Statsd/Server/Backend/Graphite.pm

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