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