| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | package OD::Prometheus::Metric; | ||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | $OD::Prometheus::Metric::VERSION = '0.005'; | |||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use v5.24; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Data::Printer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Regexp::Common qw(number delimited); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | OD::Prometheus::Metric - Class representing a Prometheus metric | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | version 0.005 | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | has metric_name => ( | |||||||||||||||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 25 | required=> 1, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | has value => ( | |||||||||||||||||||||||||||||||||||||||||
| 29 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 30 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 31 | required=> 1, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | has timestamp => ( | ||||||||||||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 36 | isa => 'Num', | ||||||||||||||||||||||||||||||||||||||||||
| 37 | required=> 1, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | default => sub { time }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | has labels => ( | ||||||||||||||||||||||||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | isa => 'HashRef[Str]', | ||||||||||||||||||||||||||||||||||||||||||
| 44 | required=> 1, | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | has type => ( | |||||||||||||||||||||||||||||||||||||||||
| 49 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 50 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | default => 'untyped', #see https://prometheus.io/docs/instrumenting/exposition_formats/ | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | has docstring => ( | |||||||||||||||||||||||||||||||||||||||||
| 55 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 56 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=6 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | if ( @_ == 1 && !ref $_[0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ == 1 and not ref $_[0]
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1
not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | my $r = $class->parse( $_[0] ); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return $r | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | my %temphash = @_; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | if( @_ == 4 && exists $temphash{ line } && exists $temphash{ comments } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@_ == 4 and exists $temphash{"line"} and exists $temphash{"comments"}
@_ == 4 and exists $temphash{"line"}
MC/DC0%0/3
@_ == 4 and exists $temphash{"line"} and exists $temphash{"comments"}
@_ == 4
exists $temphash{"line"}
exists $temphash{"comments"}
Truth table25%1/4
A and B and C
A= @_ == 4B= exists $temphash{"line"}C= exists $temphash{"comments"}
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | return $class->parse( $temphash{ line } , @{ $temphash{ comments } } ); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | return $class->SUPER::BUILDARGS(@_); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | sub to_string { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | join("\n", | ||||||||||||||||||||||||||||||||||||||||||
| 78 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 79 | ( defined( $_[0]->docstring )? '# HELP '.encode_str( $_[0]->docstring ) : () ), | ||||||||||||||||||||||||||||||||||||||||||
| 80 | '# TYPE '.$_[0]->type, | ||||||||||||||||||||||||||||||||||||||||||
| 81 | join(' ', | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | $_[0]->metric_name.( ($_[0]->labels->%*)? '{'.join(',',map { $_.'='.$_[0]->get_label( $_ ) } (sort keys $_[0]->labels->%*)).'}' : '' ), | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to_string: not called CC=3 SCAR=24.8
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | $_[0]->value, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | $_[0]->timestamp | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 86 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 87 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | sub get_label { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | my $self = shift // die 'incorrect call'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_label: not called CC=4 SCAR=30.0
Pod
undocumented
Condition0%0/2
shift() // die("incorrect call")
MC/DC0%0/1
shift() // die("incorrect call")
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $label = shift // die 'incorrect call'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() // die("incorrect call")
MC/DC0%0/1
shift() // die("incorrect call")
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | die "Label $label does not exist" if( !exists( $self->labels->{ $label } ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my $value = $self->labels->{ $label }; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $value = encode_str( $value ); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | return '"'.$value.'"'; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # use to pass from prometheus string to internal (Perl) string | ||||||||||||||||||||||||||||||||||||||||||
| 100 | sub decode_str { | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 12 | ▶ | my $str = shift // die 'incorrect call'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode_str: called CC=2 SCAR=6.9
Pod
undocumented
Condition100%2/2
shift() // die("incorrect call")
MC/DC100%1/1
shift() // die("incorrect call")
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 12 | $str =~ s/\\n/\n/g; # \n becomes a real newline | |||||||||||||||||||||||||||||||||||||||||
| 103 | 12 | $str =~ s/\\\\/\\/g; # escaped backslash (\\) becomes a real backslash | |||||||||||||||||||||||||||||||||||||||||
| 104 | 12 | $str =~ s/\\"/"/g; # \" becomes a plain " | |||||||||||||||||||||||||||||||||||||||||
| 105 | 12 | return $str | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | sub encode_str { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | ▶ | my $str = shift // die 'incorrect call'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_str: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/2
shift() // die("incorrect call")
MC/DC0%0/1
shift() // die("incorrect call")
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | $str =~ s/\\/\\\\/g; # every backslash is encoded as \\ | |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $str =~ s/"/\\"/g; # double quotes " are escaped | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $str =~ s/\n/\\n/g; # newlines are replaced by \n | |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | return $str | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | sub parse { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 2 | ▶ | my $class = shift // die 'incorrect call'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=11 SCAR=24.9
Pod
undocumented
Condition50%1/2
shift() // die("incorrect call")
MC/DC0%0/1
shift() // die("incorrect call")
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 2 | ▶ | my $str = shift // die 'incorrect call'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() // die("incorrect call")
MC/DC0%0/1
shift() // die("incorrect call")
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | my @comments = @_; | |||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 2 | my $ret = {}; | |||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 2 | for my $comment ( @comments ) { | |||||||||||||||||||||||||||||||||||||||||
| 125 | 6 | ▶ | if( $comment =~ / | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | # | ||||||||||||||||||||||||||||||||||||||||||
| 127 | \s* | ||||||||||||||||||||||||||||||||||||||||||
| 128 | TYPE | ||||||||||||||||||||||||||||||||||||||||||
| 129 | \s+ | ||||||||||||||||||||||||||||||||||||||||||
| 130 | (?<metric_name>[a-zA-Z_][a-zA-Z0-9_]*) | ||||||||||||||||||||||||||||||||||||||||||
| 131 | \s+ | ||||||||||||||||||||||||||||||||||||||||||
| 132 | (?<type>counter|gauge|histogram|summary|untyped) | ||||||||||||||||||||||||||||||||||||||||||
| 133 | \s* $/x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 134 | $ret->{ type } = $+{ type } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 2 | } | |||||||||||||||||||||||||||||||||||||||||
| 136 | elsif( $comment =~ / | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # | ||||||||||||||||||||||||||||||||||||||||||
| 138 | \s* | ||||||||||||||||||||||||||||||||||||||||||
| 139 | HELP | ||||||||||||||||||||||||||||||||||||||||||
| 140 | \s+ | ||||||||||||||||||||||||||||||||||||||||||
| 141 | (?<metric_name>[a-zA-Z_][a-zA-Z0-9_]*) | ||||||||||||||||||||||||||||||||||||||||||
| 142 | \s+ | ||||||||||||||||||||||||||||||||||||||||||
| 143 | (?<docstring>.*) | ||||||||||||||||||||||||||||||||||||||||||
| 144 | $/x ) { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 2 | $ret->{ docstring } = decode_str( $+{ docstring } ); | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | #p $ret; | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | if( $str =~ / | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 153 | ^ | ||||||||||||||||||||||||||||||||||||||||||
| 154 | (?<metric_name>[a-zA-Z_][a-zA-Z0-9_]*) | ||||||||||||||||||||||||||||||||||||||||||
| 155 | (?:{(?<labels>.*)\s*,*\s*})* | ||||||||||||||||||||||||||||||||||||||||||
| 156 | \s+ | ||||||||||||||||||||||||||||||||||||||||||
| 157 | (?<value>(?:$RE{num}{real}|NaN|\+Inf|-Inf)) | ||||||||||||||||||||||||||||||||||||||||||
| 158 | (?: \s+ (?<timestamp>\d+) )* | ||||||||||||||||||||||||||||||||||||||||||
| 159 | \s* | ||||||||||||||||||||||||||||||||||||||||||
| 160 | $ | ||||||||||||||||||||||||||||||||||||||||||
| 161 | /x) { | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 2 | $ret->{ metric_name } = $+{ metric_name }; | |||||||||||||||||||||||||||||||||||||||||
| 163 | 2 | $ret->{ value } = $+{ value }; #TODO: Must consult https://golang.org/pkg/strconv/ to make sure we parse -Inf,+Inf,Nan | |||||||||||||||||||||||||||||||||||||||||
| 164 | 2 | ▶ | $ret->{ timestamp } = $+{ timestamp } if exists $+{ timestamp }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 2 | my $labels = {}; | |||||||||||||||||||||||||||||||||||||||||
| 166 | 2 | ▶ | if( exists $+{ labels } ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 2 | my @labelstrs = split(/\s*,\s*/,$+{ labels }); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 2 | for my $labelstr ( @labelstrs ) { | |||||||||||||||||||||||||||||||||||||||||
| 169 | #### say $labelstr; | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 10 | ▶ | if( $labelstr =~ / | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | ^ | ||||||||||||||||||||||||||||||||||||||||||
| 172 | (?<label_name>[a-zA-Z_][a-zA-Z0-9_]*) #capture $1 | ||||||||||||||||||||||||||||||||||||||||||
| 173 | \s* = \s* | ||||||||||||||||||||||||||||||||||||||||||
| 174 | $RE{delimited}{-delim=>'"'}{-keep} # $2 entire,$3 opening ",$4 content between ",escape is by default \ | ||||||||||||||||||||||||||||||||||||||||||
| 175 | /x) { | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 10 | my $label_name = $+{ label_name }; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 10 | $labels->{ $label_name } = decode_str( $4 ); | |||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | die "Cannot parse labelstr: $labelstr" | |||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||||||||||||||||
| 184 | 2 | $ret->{ labels } = $labels | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | die 'Cannot parse this string: '.$str | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 2 | return $ret | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | Copyright 2018 Athanasios Douitsis, all rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | This program is free software; you can use it | ||||||||||||||||||||||||||||||||||||||||||
| 198 | under the terms of Artistic License 2.0 which can be found at | ||||||||||||||||||||||||||||||||||||||||||
| 199 | http://www.perlfoundation.org/artistic_license_2_0 | ||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||