lib/App/ElasticSearch/Utilities/Metrics.pm

filter: Statementstmt 22.2% 35 / 157 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 42.8% 9 / 21 Podpod 100% 4 / 4 totaltotal 19.2% 48 / 249 CC 40 SCAR 43.6
CC
40
Cov
16%
collect_index_metrics
240.0
_collect_index_blocks
56.0
_stat_collector
56.0
CRAP 78.0
« lib/App/ElasticSearch/Utilities/Connection.pm ↑ Summary lib/App/ElasticSearch/Utilities/QueryString/FileExpansion.pm »
1 package App::ElasticSearch::Utilities::Metrics;
2 # ABSTRACT: Fetches performance metrics about the node
3  
4  
5 1 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '9.1'; # VERSION
9  
10 1 ▶use App::ElasticSearch::Utilities qw(es_connect);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use CLI::Helpers qw(:output);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Ref::Util qw(is_ref is_arrayref is_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Types::Standard qw( ArrayRef Bool HashRef InstanceOf Int Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 has 'connection' => (
21 is => 'ro',
22 isa => InstanceOf['App::ElasticSearch::Utilities::Connection'],
23 0 ▶ default => sub { es_connect() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 1 handles => [qw(host port request)],
25 );
26  
27 1 my @_IGNORES = qw(
28 _all _shards
29 attributes id timestamp uptime_in_millis
30 );
31  
32  
33 has 'ignore' => (
34 is => 'lazy',
35 isa => ArrayRef[Str],
36 0 ▶ default => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
37 0 my ($self) = @_;
38  
39 0 my %roles = map { $_ => 1 } @{ $self->node_details->{roles} };
40 0 my @ignore = qw(adaptive_selection discovery);
41  
42 # Easy roles and sections are the same
43 0 foreach my $section ( qw(ingest ml transform) ) {
44 push @ignore, $section
45 0 ▶ unless $roles{$section};
Branch: 1 branch
BranchTrueFalseTotal
unless $roles{$section} 0 0 0
46 }
47  
48 0 ▶ if( ! $roles{ml} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($roles{"ml"}) 0 0 0
49 0 push @ignore, qw(ml_datafeed ml_job_comms ml_utility);
50 }
51  
52 # Skip some sections if we're not a data node
53 0 ▶ if( ! grep { /^data/ } keys %roles ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/^data/u;} keys %roles) 0 0 0
54 0 push @ignore, qw(force_merge indexing indices merges pressure recovery segments translog);
55 }
56  
57 0 return \@ignore;
58 },
59 1 );
60  
61  
62 1 has 'node_details' => (
63 is => 'lazy',
64 isa => HashRef,
65 init_arg => undef,
66 );
67  
68 0 ▶sub _build_node_details {
Subroutine _build_node_details: not called CC=3 SCAR=24.8
69 0 my ($self) = @_;
70  
71 0 ▶ if( my $res = $self->request('_nodes/_local')->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->request("_nodes/_local")->content) 0 0 0
72 0 ▶ if( my $nodes = $res->{nodes} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $nodes = $res->{"nodes"}) 0 0 0
73 0 my ($id) = keys %{ $nodes };
74 return {
75 0 %{ $nodes->{$id} },
76 id => $id,
77 }
78 }
79 }
80  
81 # Fail our type check
82 0 return;
83 }
84  
85  
86 1 has 'node_id' => (
87 is => 'lazy',
88 isa => Str,
89 );
90  
91 0 ▶sub _build_node_id {
Subroutine _build_node_id: not called CC=2 SCAR=17.9
92 0 my ($self) = @_;
93  
94 0 ▶ if( my $details = $self->node_details ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $details = $self->node_details) 0 0 0
95 0 return $details->{id};
96 }
97  
98 0 warn sprintf "unable to determine node_id for %s:%d",
99 $self->host, $self->port;
100  
101 # Fail our type check
102 0 return;
103 }
104  
105  
106 has 'with_cluster_metrics' => (
107 is => 'lazy',
108 isa => Bool,
109 0 ▶ builder => sub {
Subroutine _build_with_cluster_metrics: not called CC=2 SCAR=17.9
110 0 my ($self) = @_;
111 0 ▶ if( my $info = $self->node_details ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $self->node_details) 0 0 0
112 0 return !!grep { $_ eq 'master' } @{ $info->{roles} };
113 }
114 0 return 0;
115 },
116 1 );
117  
118  
119  
120 has 'with_index_metrics' => (
121 is => 'lazy',
122 isa => Bool,
123 0 ▶ builder => sub {
Subroutine _build_with_index_metrics: not called CC=2 SCAR=17.9
124 0 my ($self) = @_;
125 0 ▶ if( my $info = $self->node_details ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $self->node_details) 0 0 0
126 0 return !!grep { /^data/ } @{ $info->{roles} };
127 }
128 0 return 0;
129 },
130 1 );
131  
132  
133 0 ▶sub get_metrics {
Subroutine get_metrics: not called CC=3 SCAR=24.8
Pod documented
134 0 my ($self) = @_;
135  
136 # Fetch Node Local Stats
137 0 my @collected = $self->collect_node_metrics();
138  
139 0 ▶ push @collected, $self->collect_cluster_metrics()
Branch: 1 branch
BranchTrueFalseTotal
if $self->with_cluster_metrics 0 0 0
140 if $self->with_cluster_metrics;
141  
142 0 ▶ push @collected, $self->collect_index_metrics()
Branch: 1 branch
BranchTrueFalseTotal
if $self->with_index_metrics 0 0 0
143 if $self->with_index_metrics;
144  
145 # Flatten Collected and Return the Stats
146 0 return \@collected;
147 }
148  
149  
150 0 ▶sub collect_node_metrics {
Subroutine collect_node_metrics: not called CC=2 SCAR=17.9
Pod documented
151 0 my ($self) = @_;
152  
153 0 ▶ if( my $res = $self->request('_nodes/_local/stats')->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->request("_nodes/_local/stats")->content) 0 0 0
154 0 return $self->_stat_collector( $res->{nodes}{$self->node_id} );
155 }
156  
157 # Explicit return of empty list
158 0 return;
159 }
160  
161  
162 0 ▶sub collect_cluster_metrics {
Subroutine collect_cluster_metrics: not called CC=2 SCAR=17.9
Pod documented
163 0 my ($self) = @_;
164  
165 0 my @stats = ();
166  
167 0 ▶ if( my $res = $self->request('_cluster/health')->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->request("_cluster/health")->content) 0 0 0
168 push @stats,
169 { key => "cluster.nodes.total", value => $res->{number_of_nodes}, },
170 { key => "cluster.nodes.data", value => $res->{number_of_data_nodes}, },
171 { key => "cluster.shards.primary", value => $res->{active_primary_shards}, },
172 { key => "cluster.shards.active", value => $res->{active_shards}, },
173 { key => "cluster.shards.initializing", value => $res->{initializing_shards}, },
174 { key => "cluster.shards.relocating", value => $res->{relocating_shards}, },
175 { key => "cluster.shards.unassigned", value => $res->{unassigned_shards}, },
176 0 ;
177 }
178 0 push @stats, $self->_collect_index_blocks();
179 0 return @stats;
180 }
181  
182 0 ▶sub _collect_index_blocks {
Subroutine _collect_index_blocks: not called CC=7 SCAR=40.3
183 0 my ($self) = @_;
184  
185 0 my @req = (
186 '_settings/index.blocks.*',
187 {
188 index => '_all',
189 uri_param => {
190 flat_settings => 'true',
191 },
192 },
193 );
194  
195 0 ▶ if( my $res = $self->request(@req)->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $res = $self->request(@req)->content) 0 0 0
196 0 my %collected=();
197 0 foreach my $idx ( keys %{ $res } ) {
198 0 ▶ if( my $settings = $res->{$idx}{settings} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $settings = $res->{$idx}{"settings"}) 0 0 0
199 0 foreach my $block ( keys %{ $settings } ) {
200 0 my $value = $settings->{$block};
201 0 ▶ if( lc $value eq 'true') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $value eq "true") 0 0 0
202 0 ▶ $collected{$block} ||= 0;
Condition0%0/2
$collected{$block} ||= 0
l!l
00
MC/DC0%0/1
$collected{$block} ||= 0
$collected{$block}
Truth table0%0/2
A
A= $collected{$block}
Aresult
11
00
203 0 $collected{$block}++;
204 }
205 }
206 }
207 }
208 0 return map { { key => "cluster.$_", value => $collected{$_} } } sort keys %collected;
209 }
210  
211 # Explicit return of empty list
212 0 return;
213 }
214  
215  
216 0 ▶sub collect_index_metrics {
Subroutine collect_index_metrics: not called CC=15 SCAR=54.8
Pod documented
217 0 my ($self) = @_;
218  
219 0 my $id = $self->node_id;
220 0 my $shardres = $self->request('_cat/shards',
221 {
222 uri_param => {
223 format => 'json',
224 bytes => 'b',
225 h => join(',', qw( index prirep docs store id state )),
226 }
227 }
228 )->content;
229  
230 0 my %results;
231 0 foreach my $shard ( @{ $shardres } ) {
232 # Skip unallocated shards
233 0 ▶ next unless $shard->{id};
Branch: 1 branch
BranchTrueFalseTotal
unless $shard->{"id"} 0 0 0
234  
235 # Skip unless this shard is allocated to this shard
236 0 ▶ next unless $shard->{id} eq $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $shard->{"id"} eq $id 0 0 0
237  
238 # Skip "Special" Indexes
239 0 ▶ next if $shard->{index} =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $shard->{"index"} =~ /^\./u 0 0 0
240  
241 # Figure out the Index Basename
242 0 my $index = $shard->{index} =~ s/[-_]\d{4}([.-])\d{2}\g{1}\d{2}(?:[-_.]\d+)?$//r;
243 0 ▶ next unless $index;
Branch: 1 branch
BranchTrueFalseTotal
unless $index 0 0 0
244 0 $index =~ s/[^a-zA-Z0-9]+/_/g;
245  
246 0 ▶ my $type = $shard->{prirep} eq 'p' ? 'primary' : 'replica';
Branch: 1 branch
BranchTrueFalseTotal
$shard->{'prirep'} eq 'p' ? : 0 0 0
247  
248 # Initialize
249 0 ▶ $results{$index} ||= { map { $_ => 0 } qw( docs bytes primary replica ) };
Condition0%0/2
$results{$index} ||= {map({$_, 0;} "docs", "bytes", "primary", "replica")}
l!l
00
MC/DC0%0/1
$results{$index} ||= {map({$_, 0;} "docs", "bytes", "primary", "replica")}
$results{$index}
Truth table0%0/2
A
A= $results{$index}
Aresult
11
00
250 0 ▶ $results{$index}->{state} ||= {};
Condition0%0/2
$results{$index}{"state"} ||= {}
l!l
00
MC/DC0%0/1
$results{$index}{"state"} ||= {}
$results{$index}{"state"}
Truth table0%0/2
A
A= $results{$index}{"state"}
Aresult
11
00
251 0 ▶ $results{$index}->{state}{$shard->{state}} ||= 0;
Condition0%0/2
$results{$index}{"state"}{$shard->{"state"}} ||= 0
l!l
00
MC/DC0%0/1
$results{$index}{"state"}{$shard->{"state"}} ||= 0
$results{$index}{"state"}{$shard->{"state"}}
Truth table0%0/2
A
A= $results{$index}{"state"}{$shard->{"state"}}
Aresult
11
00
252 0 $results{$index}->{state}{$shard->{state}}++;
253  
254 # Add it up, Add it up
255 0 $results{$index}->{docs} += $shard->{docs};
256 0 $results{$index}->{bytes} += $shard->{store};
257 0 $results{$index}->{$type}++;
258 }
259  
260 0 my @results;
261 0 foreach my $idx (sort keys %results) {
262 0 foreach my $k ( sort keys %{ $results{$idx} } ) {
263 # Skip the complex
264 0 ▶ next if ref $results{$idx}->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if ref $results{$idx}{$k} 0 0 0
265 push @results,
266 {
267 key => sprintf("node.indices.%s.%s", $idx, $k),
268 0 value => $results{$idx}->{$k},
269 };
270 }
271 0 ▶ my $states = $results{$idx}->{state} || {};
Condition0%0/2
$results{$idx}{'state'} || {}
l!l
00
MC/DC0%0/1
$results{$idx}{'state'} || {}
$results{$idx}{'state'}
Truth table0%0/2
A
A= $results{$idx}{'state'}
Aresult
11
00
272  
273 0 foreach my $k ( sort keys %{ $states } ) {
274 push @results,
275 {
276 key => sprintf("node.indices.%s.state.%s", $idx, $k),
277 0 value => $states->{$k},
278 };
279 }
280 }
281 0 return @results;
282 }
283  
284 #------------------------------------------------------------------------#
285 # Parse Statistics Dynamically
286 0 ▶sub _stat_collector {
Subroutine _stat_collector: not called CC=7 SCAR=40.3
287 0 my $self = shift;
288 0 my $ref = shift;
289 0 my @path = @_;
290 0 my @stats = ();
291  
292 # Base Case
293 0 ▶ return unless is_hashref($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_hashref($ref) 0 0 0
294  
295 0 my %ignores = map { $_ => 1 } @{ $self->ignore }, @_IGNORES;
296 0 foreach my $key (sort keys %{ $ref }) {
297 # Skip uninteresting keys
298 0 ▶ next if $ignores{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $ignores{$key} 0 0 0
299  
300 # Skip peak values, we'll see those in the graphs.
301 0 ▶ next if $key =~ /^peak/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^peak/u 0 0 0
302  
303 # Sanitize Key Name
304 0 my $key_name = $key;
305 0 $key_name =~ s/(?:_time)?(?:_in)?_millis/_ms/;
306 0 $key_name =~ s/(?:size_)?in_bytes/bytes/;
307 0 $key_name =~ s/[^a-zA-Z0-9]+/_/g;
308  
309 0 ▶ if( is_hashref($ref->{$key}) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (Ref::Util::XS::is_hashref($ref->{$key})) { } 0 0 0
elsif ($ref->{$key} =~ /^\d+(?:\.\d+)?$/u) { } 0 0 0
310 # Recurse
311 0 push @stats, $self->_stat_collector($ref->{$key},@path,$key_name);
312 }
313 elsif( $ref->{$key} =~ /^\d+(?:\.\d+)?$/ ) {
314 # Numeric
315 push @stats, {
316 key => join('.',@path,$key_name),
317 0 value => $ref->{$key},
318 };
319 }
320 }
321  
322 0 return @stats;
323 }
324  
325 1 __PACKAGE__->meta->make_immutable;
326  
327 __END__
« lib/App/ElasticSearch/Utilities/Connection.pm ↑ Summary lib/App/ElasticSearch/Utilities/QueryString/FileExpansion.pm »