| 1 | package Langertha::Runtime::Metrics; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Prometheus text exposition format parser with prefix filter | ||||||||||||||||||||||||||||
| 3 | 39 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||
| 4 | 39 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 39 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 39 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 39 | has _default_type => ( | |||||||||||||||||||||||||||
| 10 | is => 'ro', | ||||||||||||||||||||||||||||
| 11 | isa => 'Str', | ||||||||||||||||||||||||||||
| 12 | default => 'untyped', | ||||||||||||||||||||||||||||
| 13 | ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 22 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=10 SCAR=23.2
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 22 | my ( $self, $payload ) = @_; | |||||||||||||||||||||||||||
| 17 | 22 | ▶ | croak "parse() requires a payload string" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $payload and ref $payload ne "HASH"
MC/DC50%1/2
defined $payload and ref $payload ne "HASH"
defined $payload
ref $payload ne "HASH"
Truth table66%2/3
A and B
A= defined $payloadB= ref $payload ne "HASH"
| |||||||||||||||||||||||||||||
| 18 | unless defined $payload && ref($payload) ne 'HASH'; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 21 | my @records; | |||||||||||||||||||||||||||
| 21 | my %type_for; | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 21 | for my $line ( split /\r?\n/, $payload ) { | |||||||||||||||||||||||||||
| 24 | 105 | ▶ | next if $line eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 105 | ▶ | next if $line =~ /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 105 | ▶ | if ( $line =~ /^\#\s*TYPE\s+(\S+)\s+(\S+)\s*$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 30 | $type_for{$1} = $2; | |||||||||||||||||||||||||||
| 29 | 30 | next; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 75 | ▶ | next if $line =~ /^\s*#/; # HELP and other directives | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Sample line. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Sample line. Three shapes per the Prometheus spec: | ||||||||||||||||||||||||||||
| 36 | # metric_name [labels] value [timestamp] | ||||||||||||||||||||||||||||
| 37 | # metric_name value [timestamp] | ||||||||||||||||||||||||||||
| 38 | # Prometheus values are always numeric (or the special floats NaN / | ||||||||||||||||||||||||||||
| 39 | # +Inf / -Inf), so we anchor the value group to that — without the | ||||||||||||||||||||||||||||
| 40 | # numeric anchor, plain English lines like "this is a comment" match | ||||||||||||||||||||||||||||
| 41 | # the lenient name+value pattern and become bogus records. | ||||||||||||||||||||||||||||
| 42 | 45 | my ( $name, $labels_str, $value ); | |||||||||||||||||||||||||||
| 43 | # A quoted label value may itself contain } (k306). | ||||||||||||||||||||||||||||
| 44 | 45 | ▶ | if ( $line =~ /^([a-zA-Z_:][a-zA-Z0-9_:]*)(\{(?:[^"}]|"(?:[^"\\]|\\.)*")*\})?\s+(-?\d+(?:\.\d+)?(?:[eE][-+]?\d+)?|NaN|\+?Inf|-Inf)/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 44 | ( $name, $labels_str, $value ) = ( $1, $2, $3 ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | else { | ||||||||||||||||||||||||||||
| 48 | # Unparseable — skip rather than croak; partial scrapes survive. | ||||||||||||||||||||||||||||
| 49 | 1 | next; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 44 | my $labels = $self->_parse_labels($labels_str); | |||||||||||||||||||||||||||
| 53 | 44 | my $numeric = 0 + $value; # numeric coercion; non-numeric values (NaN, | |||||||||||||||||||||||||||
| 54 | # +Inf) round-trip as 0 / inf in Perl — good | ||||||||||||||||||||||||||||
| 55 | # enough for the shape contract. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 44 | ▶ | push @records, { | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->_resolve_type(\%type_for, $name) || $self->_default_type
MC/DC0%0/2
$self->_resolve_type(\%type_for, $name) || $self->_default_type
$self->_resolve_type(\%type_for, $name)
$self->_default_type
Truth table66%2/3
A || B
A= $self->_resolve_type(\%type_for, $name)B= $self->_default_type
| |||||||||||||||||||||||||||||
| 58 | name => $name, | ||||||||||||||||||||||||||||
| 59 | type => $self->_resolve_type(\%type_for, $name) || $self->_default_type, | ||||||||||||||||||||||||||||
| 60 | value => $numeric, | ||||||||||||||||||||||||||||
| 61 | labels => $labels, | ||||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 21 | return \@records; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Resolve the declared TYPE for a series name. Histograms and summaries | ||||||||||||||||||||||||||||
| 69 | # emit TYPE for the base name only — `_bucket`, `_sum`, `_count` (and the | ||||||||||||||||||||||||||||
| 70 | # summary's analogous suffixes) inherit it. Look up the most-specific | ||||||||||||||||||||||||||||
| 71 | # match first, then walk the name progressively left-to-right. | ||||||||||||||||||||||||||||
| 72 | 44 | ▶ | sub _resolve_type { | ||||||||||||||||||||||||||
|
Subroutine
_resolve_type: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 73 | 44 | my ( $self, $type_for, $name ) = @_; | |||||||||||||||||||||||||||
| 74 | 44 | ▶ | return $type_for->{$name} if exists $type_for->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 17 | my @parts = split /_/, $name; | |||||||||||||||||||||||||||
| 76 | 17 | ▶ | while ( @parts > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 20 | pop @parts; | |||||||||||||||||||||||||||
| 78 | 20 | my $candidate = join '_', @parts; | |||||||||||||||||||||||||||
| 79 | 20 | ▶ | return $type_for->{$candidate} if exists $type_for->{$candidate}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 7 | return undef; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 44 | ▶ | sub _parse_labels { | ||||||||||||||||||||||||||
|
Subroutine
_parse_labels: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 86 | 44 | my ( $self, $labels_str ) = @_; | |||||||||||||||||||||||||||
| 87 | 44 | ▶ | return {} unless defined $labels_str && length $labels_str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $labels_str and length $labels_str
MC/DC50%1/2
defined $labels_str and length $labels_str
defined $labels_str
length $labels_str
Truth table66%2/3
A and B
A= defined $labels_strB= length $labels_str
| |||||||||||||||||||||||||||||
| 88 | # Strip surrounding braces | ||||||||||||||||||||||||||||
| 89 | 24 | $labels_str =~ s/^\{//; | |||||||||||||||||||||||||||
| 90 | 24 | $labels_str =~ s/\}$//; | |||||||||||||||||||||||||||
| 91 | 24 | ▶ | return {} unless length $labels_str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 24 | my %labels; | |||||||||||||||||||||||||||
| 94 | # name="value" pairs per the exposition format: optional whitespace | ||||||||||||||||||||||||||||
| 95 | # around names, '=' and ',', a trailing comma allowed; a quoted value may | ||||||||||||||||||||||||||||
| 96 | # hold commas, '}' and the escapes \\ \" \n (vLLM joins its LoRA adapter | ||||||||||||||||||||||||||||
| 97 | # list with commas — k306). A malformed rest ends the parse; the pairs | ||||||||||||||||||||||||||||
| 98 | # read so far are kept. | ||||||||||||||||||||||||||||
| 99 | 24 | ▶ | while ( $labels_str =~ /\G\s*([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*"((?:[^"\\]|\\.)*)"\s*(?:,|\z)/gc ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 34 | my ( $k, $v ) = ( $1, $2 ); | |||||||||||||||||||||||||||
| 101 | 34 | ▶ | $v =~ s/\\(.)/$1 eq 'n' ? "\n" : $1/ge; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 34 | $labels{$k} = $v; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 24 | return \%labels; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 4 | ▶ | sub filter_prefix { | ||||||||||||||||||||||||||
|
Subroutine
filter_prefix: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 4 | my ( $self, $records, $prefix ) = @_; | |||||||||||||||||||||||||||
| 109 | 4 | ▶ | croak "filter_prefix() requires an ArrayRef of records" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | unless ref($records) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 111 | 4 | ▶ | croak "filter_prefix() requires a string prefix" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $prefix and not ref $prefix
MC/DC50%1/2
defined $prefix and not ref $prefix
defined $prefix
not ref $prefix
Truth table66%2/3
A and not(B)
A= defined $prefixB= not ref $prefix
| |||||||||||||||||||||||||||||
| 112 | unless defined $prefix && !ref($prefix); | ||||||||||||||||||||||||||||
| 113 | 3 | ▶ | my @filtered = grep { defined $_->{name} && index( $_->{name}, $prefix ) == 0 } @$records; | ||||||||||||||||||||||||||
|
Condition66%2/3
defined $$_{'name'} && index($$_{'name'}, $prefix) == 0
MC/DC50%1/2
defined $$_{'name'} && index($$_{'name'}, $prefix) == 0
defined $$_{'name'}
index($$_{'name'}, $prefix) == 0
Truth table66%2/3
A && B
A= defined $$_{'name'}B= index($$_{'name'}, $prefix) == 0
| |||||||||||||||||||||||||||||
| 114 | 3 | return \@filtered; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 15 | ▶ | sub parse_and_filter { | ||||||||||||||||||||||||||
|
Subroutine
parse_and_filter: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 15 | my ( $self, $payload, @prefixes ) = @_; | |||||||||||||||||||||||||||
| 120 | 15 | my $records = $self->parse($payload); | |||||||||||||||||||||||||||
| 121 | 15 | ▶ | return $records unless @prefixes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 6 | my @kept; | |||||||||||||||||||||||||||
| 123 | 6 | PREFIX: for my $r ( @$records ) { | |||||||||||||||||||||||||||
| 124 | 25 | for my $p ( @prefixes ) { | |||||||||||||||||||||||||||
| 125 | 32 | ▶ | if ( defined $r->{name} && index( $r->{name}, $p ) == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $$r{"name"} and index($$r{"name"}, $p) == 0
MC/DC50%1/2
defined $$r{"name"} and index($$r{"name"}, $p) == 0
defined $$r{"name"}
index($$r{"name"}, $p) == 0
Truth table66%2/3
A and B
A= defined $$r{"name"}B= index($$r{"name"}, $p) == 0
| |||||||||||||||||||||||||||||
| 126 | 19 | push @kept, $r; | |||||||||||||||||||||||||||
| 127 | 19 | next PREFIX; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 6 | return \@kept; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 39 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 39 | 1; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||||||||||||