File Coverage

blib/lib/Net/Statsd/Server/Metrics.pm
Criterion Covered Total %
statement 105 110 95.4
branch 10 16 62.5
condition 2 6 33.3
subroutine 7 7 100.0
pod 0 3 0.0
total 124 142 87.3


line stmt bran cond sub pod time code
1             # ABSTRACT: Provides metrics abstraction to a running statsd server
2              
3             package Net::Statsd::Server::Metrics;
4             {
5             $Net::Statsd::Server::Metrics::VERSION = '0.19';
6             }
7              
8 5     5   49111 use 5.008;
  5         16  
  5         210  
9 5     5   28 use strict;
  5         8  
  5         183  
10 5     5   33 use Carp ();
  5         7  
  5         177  
11 5     5   1537 use Time::HiRes ();
  5         3716  
  5         4585  
12              
13             sub new {
14 12     12 0 21021 my ($class, $config) = @_;
15 12   33     67 $class = ref $class || $class;
16 12         21 my $g_pref = $config->{prefixStats};
17 12 50       27 if (! $g_pref) {
18 0         0 Carp::croak("prefixStats is empty or invalid! (Metrics.new)");
19             }
20              
21 12         134 my $self = {
22             keyCounter => {},
23             counters => {
24             "${g_pref}.packets_received" => 0,
25             "${g_pref}.bad_lines_seen" => 0,
26             },
27             timers => {},
28             gauges => {},
29             sets => {},
30             counter_rates => {},
31             timer_data => {},
32             pctThreshold => [ 90 ],
33             };
34              
35 12 50 33     50 if (exists $config->{percentThreshold}
36             && ref $config->{percentThreshold} eq "ARRAY") {
37 0         0 $self->{pctThreshold} = $config->{percentThreshold};
38             }
39              
40 12         57 bless $self, $class;
41             }
42              
43             sub process {
44 12     12 0 115 my ($self, $flush_interval) = @_;
45              
46 12         68 my $starttime = [Time::HiRes::gettimeofday];
47 12         18 my $key;
48              
49 12         29 my $metrics = $self->as_hash;
50 12         16 my $counters = $metrics->{counters};
51 12         18 my $timers = $metrics->{timers};
52 12         14 my $pctThreshold = $metrics->{pctThreshold};
53              
54             # Meta metrics added by statsd
55 12         18 my $counter_rates = {};
56 12         13 my $timer_data = {};
57 12         19 my $statsd_metrics = {};
58              
59             # Calculate "per second" rate
60 12         20 $flush_interval /= 1000;
61              
62 12         14 for my $key (keys %{ $counters }) {
  12         44  
63 26         30 my $value = $counters->{$key};
64 26         65 $counter_rates->{$key} = $value / $flush_interval;
65             }
66              
67             # Calculate all requested
68             # percentile values (90%, 95%, ...)
69 12         22 for my $key (keys %{ $timers }) {
  12         31  
70              
71 7         11 my $current_timer_data = {};
72              
73 7 100       11 if (@{ $timers->{$key} } > 0) {
  7         25  
74              
75             # Sort timer samples by value
76 6         8 my @values = @{ $timers->{$key} };
  6         15  
77 6         23 @values = sort { $a <=> $b } @values;
  9         15  
78              
79 6         9 my $count = @values;
80 6         6 my $min = $values[0];
81 6         9 my $max = $values[$#values];
82              
83             # We don't want to iterate at all if there's just 1 value
84 6         29 my $cumulativeValues = [ $min ];
85 6         14 my $cumulSumSquaresValues = [ $min * $min ];
86              
87 6         50 for (my $i = 1; $i < $count; $i++) {
88 6         12 my $cmlVal = $values[$i] + $cumulativeValues->[$i - 1];
89 6         9 push @{ $cumulativeValues }, $values[$i] + $cumulativeValues->[$i - 1];
  6         13  
90 6         7 push @{ $cumulSumSquaresValues }, ($values[$i] * $values[$i])
  6         23  
91             + $cumulSumSquaresValues->[$i - 1];
92             }
93              
94 6         13 my $sum = my $mean = $min;
95 6         10 my $sumSquares = $min * $min;
96 6         7 my $maxAtThreshold = $max;
97              
98 6         7 for my $pct (@{ $pctThreshold }) {
  6         13  
99              
100 8         10 my $numInThreshold = $count;
101              
102 8 100       18 if ($count > 1) {
103             # Pay attention to the rounding: should behave the same
104             # as etsy's statsd, that's using a Math.round(x).
105             # int(x + 0.5) does this.
106 4         11 $numInThreshold = int(($pct / 100 * $count) + 0.5);
107 4 50       11 next if $numInThreshold == 0;
108              
109 4 50       9 if ($pct > 0) {
110 4         8 $maxAtThreshold = $values[$numInThreshold - 1];
111 4         6 $sum = $cumulativeValues->[$numInThreshold - 1];
112 4         7 $sumSquares = $cumulSumSquaresValues->[$numInThreshold - 1];
113             }
114             else {
115 0         0 $maxAtThreshold = $values[$count - $numInThreshold];
116 0         0 $sum = $cumulativeValues->[$count - 1] - $cumulativeValues->[$count - $numInThreshold - 1];
117 0         0 $sumSquares = $cumulSumSquaresValues->[$count - 1] - $cumulSumSquaresValues->[$count - $numInThreshold - 1];
118             }
119 4         13 $mean = $sum / $numInThreshold;
120             }
121              
122 8         20 my $clean_pct = "" . $pct;
123 8         17 $clean_pct =~ s{\.}{_}g;
124 8         11 $clean_pct =~ s{-}{top}g;
125 8         21 $current_timer_data->{"count_${clean_pct}"} = $numInThreshold;
126 8         15 $current_timer_data->{"mean_${clean_pct}"} = $mean;
127 8 50       25 $current_timer_data->{($pct > 0 ? "upper_" : "lower_") . $clean_pct} = $maxAtThreshold;
128 8         15 $current_timer_data->{"sum_${clean_pct}"} = $sum;
129 8         34 $current_timer_data->{"sum_squares_${clean_pct}"} = $sumSquares;
130             }
131              
132 6         15 $sum = $cumulativeValues->[$count - 1];
133 6         9 $sumSquares = $cumulSumSquaresValues->[$count - 1];
134 6         8 $mean = $sum / $count;
135              
136             # Calculate standard deviation
137 6         7 my $sumOfDiffs = 0;
138 6         16 for (0 .. $count - 1) {
139 12         30 $sumOfDiffs += ($values[$_] - $mean) ** 2;
140             }
141 6         23 my $stddev = sqrt($sumOfDiffs / $count);
142 6         12 my $mid = int($count / 2);
143 6 50       17 my $median = $count % 2
144             ? $values[$mid]
145             : ($values[$mid - 1] + $values[$mid]) / 2;
146              
147 6         11 $current_timer_data->{std} = $stddev;
148 6         11 $current_timer_data->{upper} = $max;
149 6         12 $current_timer_data->{lower} = $min;
150 6         10 $current_timer_data->{count} = $count;
151 6         8 $current_timer_data->{count_ps} = $count / $flush_interval;
152 6         13 $current_timer_data->{sum} = $sum;
153 6         7 $current_timer_data->{sum_squares} = $sumSquares;
154 6         7 $current_timer_data->{mean} = $mean;
155 6         20 $current_timer_data->{median} = $median;
156              
157             }
158             else {
159 1         4 $current_timer_data->{count} = 0;
160 1         2 $current_timer_data->{count_ps} = 0;
161             }
162              
163 7         20 $timer_data->{$key} = $current_timer_data;
164             }
165              
166             # This is originally ms in statsd
167 12         58 $statsd_metrics->{processing_time} = Time::HiRes::tv_interval($starttime) * 1000;
168              
169             # Add processed metrics to the metrics_hash
170 12         671 $metrics->{counter_rates} = $counter_rates;
171 12         18 $metrics->{timer_data} = $timer_data;
172 12         18 $metrics->{statsd_metrics} = $statsd_metrics;
173              
174 12         36 return $metrics;
175             }
176              
177             sub as_hash {
178 12     12 0 16 my $self = $_[0];
179              
180 12         61 my %metrics = (
181             counters => $self->{counters},
182             timers => $self->{timers},
183             gauges => $self->{gauges},
184             pctThreshold => $self->{pctThreshold},
185             );
186              
187 12         29 return \%metrics;
188             }
189              
190             1;