blib/lib/Langertha/Skeid/CapacityProbe/Prometheus.pm

filter: Statementstmt 95.1% 78 / 82 Branchbran 76.4% 26 / 34 Conditioncond 55.2% 21 / 38 MC/DCmcdc 22.7% 5 / 22 Subroutinesub 92.3% 12 / 13 Podpod 100% 4 / 4 totaltotal 75.6% 146 / 193 CC 36 SCAR 19.9
CC
36
Cov
81%
__ANON__
10.5
_limit
8.8
url
8.7
CRAP 7.3
« lib/Langertha/Skeid/CapacityProbe.pm ↑ Summary lib/Langertha/Skeid/Protocol/MCP.pm »
1 package Langertha::Skeid::CapacityProbe::Prometheus;
2 5 our $VERSION = '0.004';
3 # ABSTRACT: Capacity probe reading vLLM/SGLang/TGI Prometheus metrics
4 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 extends 'Langertha::Skeid::CapacityProbe';
9  
10  
11 5 my @DEFAULT_RUNNING = qw(
12 vllm:num_requests_running
13 sglang:num_running_reqs
14 tgi_batch_current_size
15 );
16  
17 5 my @DEFAULT_WAITING = qw(
18 vllm:num_requests_waiting
19 sglang:num_queue_reqs
20 tgi_queue_size
21 );
22  
23 has _ua => (
24 is => 'lazy',
25 5 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine _build__ua: not called CC=1 SCAR=1.2
26 0 my $ua = Mojo::UserAgent->new;
27 0 $ua->connect_timeout(2);
28 # Deliberately shorter than the poll interval: a probe request still outstanding when the
29 # next tick fires is a probe that has started queueing on itself.
30 0 $ua->request_timeout(3);
31 0 return $ua;
32 },
33 5 );
34  
35 has _inflight_poll => (
36 is => 'rw',
37 12 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 5 );
39  
40  
41 10 ▶sub url {
Subroutine url: called CC=8 SCAR=21.6
Pod documented
42 10 my ($self) = @_;
43 10 my $cfg = $self->config;
44 10 ▶ return $cfg->{url} if defined($cfg->{url}) && length($cfg->{url});
Branch: 1 branch
BranchTrueFalseTotal
if defined $cfg->{"url"} and length $cfg->{"url"} 9 1 10
Condition66%2/3
defined $cfg->{"url"} and length $cfg->{"url"}
!ll&&!rl&&r
109
MC/DC50%1/2
defined $cfg->{"url"} and length $cfg->{"url"}
defined $cfg->{"url"} length $cfg->{"url"}
Truth table66%2/3
A and B
A= defined $cfg->{"url"}B= length $cfg->{"url"}
ABresult
0X0
100
111
45  
46 1 ▶ my ($node) = grep { ($_->{id} // '') eq $self->node_id } @{$self->skeid->nodes};
Condition50%1/2
$_->{"id"} // ""
l!l
10
MC/DC0%0/1
$_->{"id"} // ""
$_->{"id"}
Truth table50%1/2
A
A= $_->{"id"}
Aresult
11
00
47 1 ▶ my $base = $node ? ($node->{url} // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
$node ? : 1 0 1
Condition50%1/2
$node->{"url"} // ""
l!l
10
MC/DC0%0/1
$node->{"url"} // ""
$node->{"url"}
Truth table50%1/2
A
A= $node->{"url"}
Aresult
11
00
48 1 $base =~ s{/v\d+/?$}{};
49 1 $base =~ s{/+$}{};
50 1 ▶ my $path = $cfg->{path} // '/metrics';
Condition50%1/2
$cfg->{"path"} // "/metrics"
l!l
01
MC/DC0%0/1
$cfg->{"path"} // "/metrics"
$cfg->{"path"}
Truth table50%1/2
A
A= $cfg->{"path"}
Aresult
11
00
51 1 ▶ $path = "/$path" unless $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 0 1 1
52 1 return $base . $path;
53 }
54  
55  
56 12 ▶sub source { 'prometheus' }
Subroutine source: called CC=1 SCAR=0.0
Pod documented
57  
58  
59 8 ▶sub poll {
Subroutine poll: called CC=2 SCAR=7.0
Pod documented
60 8 my ($self) = @_;
61  
62 # One poll at a time. A slow metrics endpoint would otherwise accumulate requests on every
63 # tick, and the probe becomes the load it is supposed to measure.
64 8 ▶ return if $self->_inflight_poll;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_inflight_poll 0 8 8
65 8 $self->_inflight_poll(1);
66  
67 8 my $url = $self->url;
68 8 ▶ $self->_ua->get($url => sub {
Subroutine __ANON__: called CC=10 SCAR=23.5
69 8 my (undef, $tx) = @_;
70 8 $self->_inflight_poll(0);
71 # Stopped while the request was out -- a restart or a removed node. Whatever it says now
72 # belongs to no running probe, and writing it would overwrite or resurrect a reading.
73 8 ▶ return if $self->is_stopped;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_stopped 1 7 8
74  
75 7 ▶ my $status = $tx->res->code // 0;
Condition50%1/2
$tx->res->code // 0
l!l
70
MC/DC0%0/1
$tx->res->code // 0
$tx->res->code
Truth table50%1/2
A
A= $tx->res->code
Aresult
11
00
76 7 ▶ unless ($status >= 200 && $status < 300) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status >= 200 and $status < 300) 2 5 7
Condition66%2/3
$status >= 200 and $status < 300
!ll&&!rl&&r
025
MC/DC50%1/2
$status >= 200 and $status < 300
$status >= 200 $status < 300
Truth table66%2/3
A and B
A= $status >= 200B= $status < 300
ABresult
0X0
100
111
77 # Unreachable means unknown, and unknown means inflight decides. Never keep the last
78 # reading: it describes a node we can no longer see.
79 2 $self->_forget_own;
80 2 return;
81 }
82  
83 5 my $metrics = $self->parse_metrics($tx->res->body);
84 5 ▶ my $running = $self->_sum($metrics, ($self->config->{running} || $self->config->{used}), \@DEFAULT_RUNNING);
Condition66%2/3
$self->config->{'running'} || $self->config->{'used'}
l!l&&r!l&&!r
203
MC/DC50%1/2
$self->config->{'running'} || $self->config->{'used'}
$self->config->{'running'} $self->config->{'used'}
Truth table66%2/3
A || B
A= $self->config->{'running'}B= $self->config->{'used'}
ABresult
1X1
011
000
85 5 my $waiting = $self->_sum($metrics, $self->config->{waiting}, \@DEFAULT_WAITING);
86  
87 5 ▶ if (!defined $running && !defined $waiting) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $running or defined $waiting) 1 4 5
Condition66%2/3
defined $running or defined $waiting
l!l&&r!l&&!r
401
MC/DC50%1/2
defined $running or defined $waiting
defined $running defined $waiting
Truth table66%2/3
A or B
A= defined $runningB= defined $waiting
ABresult
1X1
011
000
88 # The endpoint answered but said nothing we understand -- most likely the wrong metric
89 # names for this engine. Same rule: report nothing.
90 1 $self->_forget_own;
91 1 return;
92 }
93  
94 $self->skeid->set_capacity_reading(
95 4 ▶ $self->node_id,
Condition50%2/4
$running // 0
l!l
40
$waiting // 0
l!l
40
MC/DC0%0/1
$running // 0
$running
MC/DC0%0/1
$waiting // 0
$waiting
Truth table50%1/2
A
A= $running
Aresult
11
00
Truth table50%1/2
A
A= $waiting
Aresult
11
00
96 source => $self->source,
97 used => (($running // 0) + ($waiting // 0)),
98 limit => $self->_limit,
99 interval_ms => $self->poll_interval_seconds * 1000,
100 );
101 8 });
102 8 return;
103 }
104  
105 4 ▶sub _limit {
Subroutine _limit: called CC=7 SCAR=21.8
106 4 my ($self) = @_;
107 4 my $cfg_limit = $self->config->{limit};
108 4 ▶ return 0 + $cfg_limit if defined($cfg_limit) && $cfg_limit > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cfg_limit and $cfg_limit > 0 0 4 4
Condition33%1/3
defined $cfg_limit and $cfg_limit > 0
!ll&&!rl&&r
400
MC/DC0%0/2
defined $cfg_limit and $cfg_limit > 0
defined $cfg_limit $cfg_limit > 0
Truth table33%1/3
A and B
A= defined $cfg_limitB= $cfg_limit > 0
ABresult
0X0
100
111
109  
110 # Falling back to max_conns keeps one number in one place for the common case where the
111 # operator already wrote down what the node can take.
112 4 ▶ my ($node) = grep { ($_->{id} // '') eq $self->node_id } @{$self->skeid->nodes};
Condition50%1/2
$_->{"id"} // ""
l!l
40
MC/DC0%0/1
$_->{"id"} // ""
$_->{"id"}
Truth table50%1/2
A
A= $_->{"id"}
Aresult
11
00
113 4 ▶ my $max = $node ? 0 + ($node->{max_conns} // 0) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$node ? : 4 0 4
Condition50%1/2
$node->{"max_conns"} // 0
l!l
40
MC/DC0%0/1
$node->{"max_conns"} // 0
$node->{"max_conns"}
Truth table50%1/2
A
A= $node->{"max_conns"}
Aresult
11
00
114 4 ▶ return $max > 0 ? $max : undef;
Branch: 1 branch
BranchTrueFalseTotal
$max > 0 ? : 4 0 4
115 }
116  
117 10 ▶sub _sum {
Subroutine _sum: called CC=6 SCAR=18.1
118 10 my ($self, $metrics, $configured, $defaults) = @_;
119 10 ▶ my @names = $configured
Branch: 2 branches
BranchTrueFalseTotal
$configured ? : 3 7 10
ref $configured eq 'ARRAY' ? : 0 3 3
120 ? (ref($configured) eq 'ARRAY' ? @$configured : ($configured))
121 : @$defaults;
122  
123 10 my $total;
124 10 for my $name (@names) {
125 24 ▶ next unless defined $metrics->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $metrics->{$name} 16 8 24
126 8 ▶ $total = ($total // 0) + $metrics->{$name};
Condition50%1/2
$total // 0
l!l
08
MC/DC0%0/1
$total // 0
$total
Truth table50%1/2
A
A= $total
Aresult
11
00
127 }
128 10 return $total;
129 }
130  
131  
132 11 ▶sub parse_metrics {
Subroutine parse_metrics: called CC=8 SCAR=21.0
Pod documented
133 11 my ($self, $body) = @_;
134 11 my %values;
135 11 ▶ return \%values unless defined $body && length $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body and length $body 0 11 11
Condition33%1/3
defined $body and length $body
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $body and length $body
defined $body length $body
Truth table33%1/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
136  
137 11 for my $line (split /\n/, $body) {
138 57 ▶ next if $line =~ /^\s*(?:#|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*(?:#|$)/ 26 31 57
139 31 ▶ my ($name, $value) = $line =~ /^\s*([A-Za-z_:][A-Za-z0-9_:]*)(?:\{[^}]*\})?\s+([-+0-9.eE]+|NaN)\s*$/
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /^\s*([A-Za-z_:][A-Za-z0-9_:]*)(?:\{[^}]*\})?\s+([-+0-9.eE]+|NaN)\s*$/ or next 30 1 31
140 or next;
141 30 ▶ next if $value eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "NaN" 1 29 30
142 29 ▶ $values{$name} = ($values{$name} // 0) + $value;
Condition100%2/2
$values{$name} // 0
l!l
128
MC/DC100%1/1
$values{$name} // 0
$values{$name}
Truth table100%2/2
A
A= $values{$name}
Aresult
11
00
143 }
144 11 return \%values;
145 }
146  
147  
148 5 1;
149  
150 __END__
« lib/Langertha/Skeid/CapacityProbe.pm ↑ Summary lib/Langertha/Skeid/Protocol/MCP.pm »