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