| 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
Condition66%2/3
defined $cfg->{"url"} and length $cfg->{"url"}
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"}
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | my ($node) = grep { ($_->{id} // '') eq $self->node_id } @{$self->skeid->nodes}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_->{"id"} // ""
MC/DC0%0/1
$_->{"id"} // ""
$_->{"id"}
Truth table50%1/2
A
A= $_->{"id"}
| |||||||||||||||||||||||||||||
| 47 | 1 | ▶ | my $base = $node ? ($node->{url} // '') : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$node->{"url"} // ""
MC/DC0%0/1
$node->{"url"} // ""
$node->{"url"}
Truth table50%1/2
A
A= $node->{"url"}
| |||||||||||||||||||||||||||||
| 48 | 1 | $base =~ s{/v\d+/?$}{}; | |||||||||||||||||||||||||||
| 49 | 1 | $base =~ s{/+$}{}; | |||||||||||||||||||||||||||
| 50 | 1 | ▶ | my $path = $cfg->{path} // '/metrics'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$cfg->{"path"} // "/metrics"
MC/DC0%0/1
$cfg->{"path"} // "/metrics"
$cfg->{"path"}
Truth table50%1/2
A
A= $cfg->{"path"}
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | $path = "/$path" unless $path =~ m{^/}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 7 | ▶ | my $status = $tx->res->code // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$tx->res->code // 0
MC/DC0%0/1
$tx->res->code // 0
$tx->res->code
Truth table50%1/2
A
A= $tx->res->code
| |||||||||||||||||||||||||||||
| 76 | 7 | ▶ | unless ($status >= 200 && $status < 300) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status >= 200 and $status < 300
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
| |||||||||||||||||||||||||||||
| 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'}
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'}
| |||||||||||||||||||||||||||||
| 85 | 5 | my $waiting = $self->_sum($metrics, $self->config->{waiting}, \@DEFAULT_WAITING); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 5 | ▶ | if (!defined $running && !defined $waiting) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $running or defined $waiting
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
| |||||||||||||||||||||||||||||
| 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
$waiting // 0
MC/DC0%0/1
$running // 0
$running
MC/DC0%0/1
$waiting // 0
$waiting
Truth table50%1/2
A
A= $running
Truth table50%1/2
A
A= $waiting
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $cfg_limit and $cfg_limit > 0
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
| |||||||||||||||||||||||||||||
| 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"} // ""
MC/DC0%0/1
$_->{"id"} // ""
$_->{"id"}
Truth table50%1/2
A
A= $_->{"id"}
| |||||||||||||||||||||||||||||
| 113 | 4 | ▶ | my $max = $node ? 0 + ($node->{max_conns} // 0) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$node->{"max_conns"} // 0
MC/DC0%0/1
$node->{"max_conns"} // 0
$node->{"max_conns"}
Truth table50%1/2
A
A= $node->{"max_conns"}
| |||||||||||||||||||||||||||||
| 114 | 4 | ▶ | return $max > 0 ? $max : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 126 | 8 | ▶ | $total = ($total // 0) + $metrics->{$name}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$total // 0
MC/DC0%0/1
$total // 0
$total
Truth table50%1/2
A
A= $total
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $body and length $body
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
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 11 | for my $line (split /\n/, $body) { | |||||||||||||||||||||||||||
| 138 | 57 | ▶ | next if $line =~ /^\s*(?:#|$)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 31 | ▶ | my ($name, $value) = $line =~ /^\s*([A-Za-z_:][A-Za-z0-9_:]*)(?:\{[^}]*\})?\s+([-+0-9.eE]+|NaN)\s*$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | or next; | ||||||||||||||||||||||||||||
| 141 | 30 | ▶ | next if $value eq 'NaN'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 29 | ▶ | $values{$name} = ($values{$name} // 0) + $value; | ||||||||||||||||||||||||||
|
Condition100%2/2
$values{$name} // 0
MC/DC100%1/1
$values{$name} // 0
$values{$name}
Truth table100%2/2
A
A= $values{$name}
| |||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | 11 | return \%values; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 5 | 1; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||