blib/lib/Langertha/Role/Runtime/MetricsPoll.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 66.6% 4 / 6 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 89.8% 97 / 108 CC 7 SCAR 7.1
CC
7
Cov
92%
export_otlp_f
4.0
poll_metrics_f
3.3
metrics_url
2.0
CRAP 2.0
« blib/lib/Langertha/Engine/MoonshotAnthropic.pm ↑ Summary blib/lib/Langertha/Engine/Hetzner.pm »
1 package Langertha::Role::Runtime::MetricsPoll;
2 # ABSTRACT: Async Prometheus /metrics scraper for self-hosted engines
3 39 our $VERSION = '0.505';
4 39 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 39 ▶use Langertha::Runtime::Metrics;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 39 requires qw(
14 json
15 url
16 );
17  
18 # poll_metrics_f decodes the scraped body through Role::HTTP's bounded decoder
19 # (karr k346), so this role needs Role::HTTP composed too. Every composer today
20 # (vLLM, SGLang, LlamaCpp) has it via Engine::OpenAIBase; require the attribute
21 # so a future MetricsPoll-only composer fails at composition, not at runtime.
22 39 requires 'response_max_bytes';
23  
24  
25 24 ▶sub metrics_url {
Subroutine metrics_url: called CC=2 SCAR=7.0
Pod documented
26 24 my ( $self ) = @_;
27 24 ▶ croak "metrics_url requires a url attribute" unless $self->has_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_url 0 24 24
28 24 my $uri = URI->new($self->url);
29 24 my $path = $uri->path;
30 # Strip trailing slashes and then a trailing /v1 segment so we get the
31 # bare server root before appending /metrics. /v1 is the OpenAI-compatible
32 # base; /metrics lives at the root for vLLM, SGLang, llama.cpp. A prefix
33 # the server is mounted under (a proxy's /vllm) stays (k306).
34 24 $path =~ s{/+\z}{};
35 24 $path =~ s{/v1\z}{};
36 24 $uri->path($path . '/metrics');
37 24 return $uri->as_string;
38 }
39  
40  
41 2 ▶sub _croak {
Subroutine _croak: called CC=1 SCAR=0.0
42 2 my ($msg) = @_;
43 2 croak($msg);
44 }
45  
46 # The _async_http backend (and its _async_loop) come from
47 # Langertha::Role::AsyncHTTP (composed below): injected client >
48 # Net::Async::HTTP > synchronous LWP fallback. The sync wrappers
49 # (poll_metrics / export_otlp) block with ->get, which drives the pending
50 # future's own loop and never creates one, so the sync fallback runs
51 # without an event loop.
52 39 with 'Langertha::Role::AsyncHTTP';
53  
54 16 ▶async sub poll_metrics_f {
Subroutine poll_metrics_f: called CC=3 SCAR=11.9
Pod documented
55 16 my ( $self, @prefixes ) = @_;
56  
57 16 my $url = $self->metrics_url;
58 16 $log->debugf("[%s] scraping %s", ref($self), $url);
59  
60 16 my $request = HTTP::Request->new(GET => $url);
61  
62 16 my $response = await $self->_async_do_request_f(
63 request => $request,
64 );
65  
66 14 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 14 14
67 0 $log->errorf("[%s] /metrics fetch failed: %s",
68 ref($self), $response->status_line);
69 0 _croak("".(ref($self))." /metrics fetch failed: ".$response->status_line);
70 }
71  
72 # Bounded Content-Encoding decode (karr k346): a self-hosted /metrics endpoint
73 # (or a proxy in front of it) is not trusted to be well-behaved, so a gzip
74 # body inflates under response_max_bytes (Role::HTTP) or is refused with the
75 # too-big croak rather than expanding unbounded in memory.
76 14 ▶ my $body = $self->_bounded_decoded_content($response) // $response->content;
Condition33%1/3
$self->_bounded_decoded_content($response) // $response->content
l!l&&r!l&&!r
1200
MC/DC0%0/2
$self->_bounded_decoded_content($response) // $response->content
$self->_bounded_decoded_content($response) $response->content
Truth table33%1/3
A // B
A= $self->_bounded_decoded_content($response)B= $response->content
ABresult
1X1
011
000
77 12 return Langertha::Runtime::Metrics->new
78 ->parse_and_filter($body, @prefixes);
79 39 }
80  
81  
82 3 ▶sub poll_metrics {
Subroutine poll_metrics: called CC=1 SCAR=0.0
Pod documented
83 3 my ( $self, @prefixes ) = @_;
84 # Synchronous variant. ->get is loop-agnostic: a pending future from
85 # Net::Async::HTTP (or an injected client on the caller's own loop) is an
86 # IO::Async::Future — or whatever Future subclass that client uses — and
87 # its await() drives the loop it belongs to; Future::AsyncAwait builds
88 # the returned future from the first pending one it awaited. On the sync
89 # fallback the future is already complete. No private loop is created.
90 3 return $self->poll_metrics_f(@prefixes)->get;
91 }
92  
93  
94 # The OTLP serializer stays a lazy load, but the require lives in this plain
95 # sub and never in an async sub's own frame: with a coderef in @INC, perl
96 # localizes $INC around the hook call, and Future::AsyncAwait (0.71, perl
97 # 5.38+) aborts the process when it suspends a frame that still holds that
98 # savestack entry (karr k193, t/48_async_require_inc_hook.t). The local is
99 # unwound when this sub returns, before export_otlp_f awaits.
100 6 ▶sub _otlp_json {
Subroutine _otlp_json: called CC=1 SCAR=0.0
101 6 my ( $records, %opts ) = @_;
102 6 require Langertha::Runtime::Metrics::OTLP;
103 6 return Langertha::Runtime::Metrics::OTLP->new->to_json($records, %opts);
104 }
105  
106 7 ▶async sub export_otlp_f {
Subroutine export_otlp_f: called CC=4 SCAR=13.9
Pod documented
107 7 my ( $self, $records, %opts ) = @_;
108 my $endpoint = $opts{endpoint}
109 7 ▶ // _croak("export_otlp_f requires an endpoint option");
Condition66%2/3
$opts{"endpoint"} // _croak("export_otlp_f requires an endpoint option")
l!l&&r!l&&!r
610
MC/DC0%0/2
$opts{"endpoint"} // _croak("export_otlp_f requires an endpoint option")
$opts{"endpoint"} _croak("export_otlp_f requires an endpoint option")
Truth table33%1/3
A // B
A= $opts{"endpoint"}B= _croak("export_otlp_f requires an endpoint option")
ABresult
1X1
011
000
110  
111 6 my $body = _otlp_json($records, %opts);
112  
113 6 my @headers = ( 'Content-Type' => 'application/json' );
114 6 ▶ push @headers, %{ $opts{headers} || {} };
Condition100%2/2
$opts{'headers'} || {}
l!l
15
MC/DC100%1/1
$opts{'headers'} || {}
$opts{'headers'}
Truth table100%2/2
A
A= $opts{'headers'}
Aresult
11
00
115  
116 6 my $request = HTTP::Request->new( POST => $endpoint, \@headers, $body );
117  
118 6 $log->debugf("[%s] exporting %d records to %s",
119 ref($self), scalar(@$records), $endpoint);
120  
121 6 my $response = await $self->_async_do_request_f(
122 request => $request,
123 );
124  
125 6 ▶ unless ( $response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 1 5 6
126 1 $log->errorf("[%s] OTLP export failed: %s",
127 ref($self), $response->status_line);
128 1 _croak("".(ref($self))." OTLP export failed: ".$response->status_line);
129 }
130  
131 5 return $response;
132 39 }
133  
134  
135 2 ▶sub export_otlp {
Subroutine export_otlp: called CC=1 SCAR=0.0
Pod documented
136 2 my ( $self, $records, %opts ) = @_;
137 # Synchronous variant; ->get drives the future's own loop (see
138 # poll_metrics).
139 2 return $self->export_otlp_f($records, %opts)->get;
140 }
141  
142  
143  
144 39 1;
145  
146 __END__
« blib/lib/Langertha/Engine/MoonshotAnthropic.pm ↑ Summary blib/lib/Langertha/Engine/Hetzner.pm »