| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2016-2026 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Net::Prometheus 0.16; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use v5.20; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use feature qw( postderef signatures ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 10 | ▶ | no warnings qw( experimental::postderef experimental::signatures ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use meta 0.008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 10 | ▶ | no warnings 'meta::experimental'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 10 | ▶ | use List::Util 1.29 qw( pairmap ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 10 | ▶ | use Net::Prometheus::Gauge; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 10 | ▶ | use Net::Prometheus::Counter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 10 | ▶ | use Net::Prometheus::Summary; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 10 | ▶ | use Net::Prometheus::Histogram; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 10 | ▶ | use Net::Prometheus::Registry; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 10 | ▶ | use Net::Prometheus::ProcessCollector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 10 | ▶ | use Net::Prometheus::PerlCollector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 10 | ▶ | use Net::Prometheus::Types qw( MetricSamples ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 NAME | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | C<Net::Prometheus> - export monitoring metrics for F<prometheus> | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =for highlighter language=perl | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | use Net::Prometheus; | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | my $client = Net::Prometheus->new; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | my $counter = $client->new_counter( | ||||||||||||||||||||||||||||
| 46 | name => "requests", | ||||||||||||||||||||||||||||
| 47 | help => "Number of received requests", | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub handle_request | ||||||||||||||||||||||||||||
| 51 | { | ||||||||||||||||||||||||||||
| 52 | $counter->inc; | ||||||||||||||||||||||||||||
| 53 | ... | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | use Plack::Builder; | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | builder { | ||||||||||||||||||||||||||||
| 59 | mount "/metrics" => $client->psgi_app; | ||||||||||||||||||||||||||||
| 60 | ... | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | This module provides the ability for a program to collect monitoring metrics | ||||||||||||||||||||||||||||
| 66 | and export them to the F<prometheus.io> monitoring server. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | As C<prometheus> will expect to collect the metrics by making an HTTP request, | ||||||||||||||||||||||||||||
| 69 | facilities are provided to yield a L<PSGI> application that the containing | ||||||||||||||||||||||||||||
| 70 | program can embed in its own structure to provide the results, or the | ||||||||||||||||||||||||||||
| 71 | application can generate a plain-text result directly and serve them by its | ||||||||||||||||||||||||||||
| 72 | own means. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 Metrics::Any | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | For more flexibility of metrics reporting, other modules may wish to use | ||||||||||||||||||||||||||||
| 77 | L<Metrics::Any> as an abstraction interface instead of directly using this | ||||||||||||||||||||||||||||
| 78 | API. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | By using C<Metrics::Any> instead, the module does not directly depend on | ||||||||||||||||||||||||||||
| 81 | C<Net::Prometheus>, and in addition program ultimately using the module gets | ||||||||||||||||||||||||||||
| 82 | the flexibility to use Prometheus (via L<Metrics::Any::Adapter::Prometheus>) | ||||||||||||||||||||||||||||
| 83 | or use another reporting system via a different adapter. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head2 new | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | $prometheus = Net::Prometheus->new; | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | Returns a new C<Net::Prometheus> instance. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Takes the following named arguments: | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =over | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =item disable_process_collector => BOOL | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | If present and true, this instance will not load the default process collector | ||||||||||||||||||||||||||||
| 104 | from L<Net::Prometheus::ProcessCollector>. If absent or false, such a | ||||||||||||||||||||||||||||
| 105 | collector will be loaded by default. | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =item disable_perl_collector => BOOL | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | If present and true, this instance will not load perl-specific collector from | ||||||||||||||||||||||||||||
| 110 | L<Net::Prometheus::PerlCollector>. If absent or false this collector is loaded | ||||||||||||||||||||||||||||
| 111 | by default. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | These two options are provided for testing purposes, or for specific use-cases | ||||||||||||||||||||||||||||
| 114 | where such features are not required. Usually it's best just to leave these | ||||||||||||||||||||||||||||
| 115 | enabled. | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =back | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 11 | ▶ | sub new ( $class, %args ) | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||
| 123 | 11 | my $self = bless { | |||||||||||||||||||||||||||
| 124 | registry => Net::Prometheus::Registry->new, | ||||||||||||||||||||||||||||
| 125 | }, $class; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 11 | ▶ | if( not $args{disable_process_collector} and | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $args{"disable_process_collector"} and my $process_collector = "Net::Prometheus::ProcessCollector"->new
MC/DC50%1/2
not $args{"disable_process_collector"} and my $process_collector = "Net::Prometheus::ProcessCollector"->new
not $args{"disable_process_collector"}
my $process_collector = "Net::Prometheus::ProcessCollector"->new
Truth table66%2/3
not(A) and B
A= not $args{"disable_process_collector"}B= my $process_collector = "Net::Prometheus::ProcessCollector"->new
| |||||||||||||||||||||||||||||
| 128 | my $process_collector = Net::Prometheus::ProcessCollector->new ) { | ||||||||||||||||||||||||||||
| 129 | 4 | $self->register( $process_collector ); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 11 | ▶ | if( not $args{disable_perl_collector} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 4 | $self->register( Net::Prometheus::PerlCollector->new ); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 11 | return $self; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 METHODS | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 register | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | $collector = $prometheus->register( $collector ); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Registers a new L<collector|/COLLECTORS> to be collected from by the C<render> | ||||||||||||||||||||||||||||
| 148 | method. The collector instance itself is returned, for convenience. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 21 | ▶ | sub register ( $self, $collector ) | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | { | ||||||||||||||||||||||||||||
| 154 | 21 | return $self->{registry}->register( $collector ); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 unregister | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | $prometheus->unregister( $collector ); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Removes a previously-registered collector. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =cut | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 2 | ▶ | sub unregister ( $self, $collector ) | ||||||||||||||||||||||||||
|
Subroutine
unregister: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||||||||
| 167 | 2 | return $self->{registry}->unregister( $collector ); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 new_gauge | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | $gauge = $prometheus->new_gauge( %args ); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Constructs a new L<Net::Prometheus::Gauge> using the arguments given and | ||||||||||||||||||||||||||||
| 175 | registers it with the exporter. The newly-constructed gauge is returned. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =cut | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 7 | ▶ | sub new_gauge ( $self, %args ) | ||||||||||||||||||||||||||
|
Subroutine
new_gauge: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | { | ||||||||||||||||||||||||||||
| 181 | 7 | return $self->register( Net::Prometheus::Gauge->new( %args ) ); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head2 new_counter | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | $counter = $prometheus->new_counter( %args ); | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Constructs a new L<Net::Prometheus::Counter> using the arguments given and | ||||||||||||||||||||||||||||
| 189 | registers it with the exporter. The newly-constructed counter is returned. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =cut | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | ▶ | sub new_counter ( $self, %args ) | ||||||||||||||||||||||||||
|
Subroutine
new_counter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 194 | { | ||||||||||||||||||||||||||||
| 195 | 1 | return $self->register( Net::Prometheus::Counter->new( %args ) ); | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head2 new_summary | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | $summary = $prometheus->new_summary( %args ); | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Constructs a new L<Net::Prometheus::Summary> using the arguments given | ||||||||||||||||||||||||||||
| 203 | and registers it with the exporter. The newly-constructed summary is returned. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 0 | ▶ | sub new_summary ( $self, %args ) | ||||||||||||||||||||||||||
|
Subroutine
new_summary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 208 | { | ||||||||||||||||||||||||||||
| 209 | 0 | return $self->register( Net::Prometheus::Summary->new( %args ) ); | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head2 new_histogram | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | $histogram = $prometheus->new_histogram( %args ); | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Constructs a new L<Net::Prometheus::Histogram> using the arguments given | ||||||||||||||||||||||||||||
| 217 | and registers it with the exporter. The newly-constructed histogram is | ||||||||||||||||||||||||||||
| 218 | returned. | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =cut | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | ▶ | sub new_histogram ( $self, %args ) | ||||||||||||||||||||||||||
|
Subroutine
new_histogram: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 223 | { | ||||||||||||||||||||||||||||
| 224 | 0 | return $self->register( Net::Prometheus::Histogram->new( %args ) ); | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head2 new_metricgroup | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | $group = $prometheus->new_metricgroup( %args ); | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Returns a new Metric Group instance as a convenience for registering multiple | ||||||||||||||||||||||||||||
| 232 | metrics using the same C<namespace> and C<subsystem> arguments. Takes the | ||||||||||||||||||||||||||||
| 233 | following named arguments: | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | =over | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | =item namespace => STR | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =item subsystem => STR | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | String values to pass by default into new metrics the group will construct. | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =back | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | Once constructed, the group acts as a proxy to the other C<new_*> methods, | ||||||||||||||||||||||||||||
| 246 | passing in these values as overrides. | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | $gauge = $group->new_gauge( ... ); | ||||||||||||||||||||||||||||
| 249 | $counter = $group->new_counter( ... ); | ||||||||||||||||||||||||||||
| 250 | $summary = $group->new_summary( ... ); | ||||||||||||||||||||||||||||
| 251 | $histogram = $group->new_histogram( ... ); | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =cut | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 2 | ▶ | sub new_metricgroup ( $self, %args ) | ||||||||||||||||||||||||||
|
Subroutine
new_metricgroup: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 256 | { | ||||||||||||||||||||||||||||
| 257 | 2 | return Net::Prometheus::_MetricGroup->new( | |||||||||||||||||||||||||||
| 258 | $self, %args | ||||||||||||||||||||||||||||
| 259 | ); | ||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =head2 collect | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | @metricsamples = $prometheus->collect( $opts ); | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | Returns a list of L<Net::Prometheus::Types/MetricSamples> obtained from all | ||||||||||||||||||||||||||||
| 267 | of the currently-registered collectors. | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =cut | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 22 | ▶ | sub collect ( $self, $opts = undef ) | ||||||||||||||||||||||||||
|
Subroutine
collect: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 272 | 22 | { | |||||||||||||||||||||||||||
| 273 | 22 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$opts //= {}
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 22 | my %samples_by_name; | |||||||||||||||||||||||||||
| 276 | 22 | foreach my $collector ( $self->{registry}->collectors, Net::Prometheus::Registry->collectors ) { | |||||||||||||||||||||||||||
| 277 | 30 | push $samples_by_name{ $_->fullname }->@*, $_ for $collector->collect( $opts ); | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | return map { | ||||||||||||||||||||||||||||
| 281 | 22 | my @results = $samples_by_name{ $_ }->@*; | |||||||||||||||||||||||||||
| 282 | 122 | my $first = $results[0]; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | @results > 1 ? | ||||||||||||||||||||||||||||
| 285 | MetricSamples( $first->fullname, $first->type, $first->help, | ||||||||||||||||||||||||||||
| 286 | 122 | ▶ | [ map { $_->samples->@* } @results ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | ) : | ||||||||||||||||||||||||||||
| 288 | $first; | ||||||||||||||||||||||||||||
| 289 | } sort keys %samples_by_name; | ||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | =head2 render | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | $str = $prometheus->render; | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | Returns a string in the Prometheus text exposition format containing the | ||||||||||||||||||||||||||||
| 297 | current values of all the registered metrics. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | $str = $prometheus->render( { options => "for collectors" } ); | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | An optional HASH reference may be provided; if so it will be passed into the | ||||||||||||||||||||||||||||
| 302 | C<collect> method of every registered collector. | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | Values that are set to C<undef> will be absent from the output (this usually | ||||||||||||||||||||||||||||
| 305 | applies to gauges). Values set to NaN will be rendered as C<NaN>. | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =cut | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 58 | ▶ | sub _render_label_value ( $v ) | ||||||||||||||||||||||||||
|
Subroutine
_render_label_value: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 310 | { | ||||||||||||||||||||||||||||
| 311 | 58 | $v =~ s/(["\\])/\\$1/g; | |||||||||||||||||||||||||||
| 312 | 58 | $v =~ s/\n/\\n/g; | |||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | 58 | return qq("$v"); | |||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 160 | ▶ | sub _render_labels ( $labels ) | ||||||||||||||||||||||||||
|
Subroutine
_render_labels: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 318 | { | ||||||||||||||||||||||||||||
| 319 | 160 | ▶ | return "" if !scalar @$labels; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | return "{" . | ||||||||||||||||||||||||||||
| 322 | 57 | ▶ | join( ",", pairmap { $a . "=" . _render_label_value( $b ) } @$labels ) . | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 323 | "}"; | ||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 21 | ▶ | sub render ( $self, $opts = undef ) | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 327 | 21 | { | |||||||||||||||||||||||||||
| 328 | return join "", map { | ||||||||||||||||||||||||||||
| 329 | 21 | my $metricsamples = $_; | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 121 | my $fullname = $metricsamples->fullname; | |||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 121 | my $help = $metricsamples->help; | |||||||||||||||||||||||||||
| 334 | 121 | $help =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||
| 335 | 121 | $help =~ s/\n/\\n/g; | |||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | "# HELP $fullname $help\n", | ||||||||||||||||||||||||||||
| 338 | "# TYPE $fullname " . $metricsamples->type . "\n", | ||||||||||||||||||||||||||||
| 339 | map { | ||||||||||||||||||||||||||||
| 340 | 121 | my $sample = $_; | |||||||||||||||||||||||||||
| 341 | 161 | my $value = $sample->value; | |||||||||||||||||||||||||||
| 342 | 161 | ▶ | ( defined $value ) ? | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 343 | sprintf "%s%s %s\n", | ||||||||||||||||||||||||||||
| 344 | $sample->varname, | ||||||||||||||||||||||||||||
| 345 | _render_labels( $sample->labels ), | ||||||||||||||||||||||||||||
| 346 | ( ( $value != $value ) ? "NaN" : $value ) : | ||||||||||||||||||||||||||||
| 347 | (); | ||||||||||||||||||||||||||||
| 348 | } $metricsamples->samples->@* | ||||||||||||||||||||||||||||
| 349 | } $self->collect( $opts ); | ||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =head2 handle | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | $response = $prometheus->handle( $request ); | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | Given an HTTP request in an L<HTTP::Request> instance, renders the metrics in | ||||||||||||||||||||||||||||
| 357 | response to it and returns an L<HTTP::Response> instance. | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | This application will respond to any C<GET> request, and reject requests for | ||||||||||||||||||||||||||||
| 360 | any other method. If a query string is present on the URI it will be parsed | ||||||||||||||||||||||||||||
| 361 | for collector options to pass into the L</render> method. | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | This method is useful for integrating metrics into an existing HTTP server | ||||||||||||||||||||||||||||
| 364 | application which uses these objects. For example: | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | my $prometheus = Net::Prometheus->new; | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | sub serve_request | ||||||||||||||||||||||||||||
| 369 | { | ||||||||||||||||||||||||||||
| 370 | my ( $request ) = @_; | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | if( $request->uri->path eq "/metrics" ) { | ||||||||||||||||||||||||||||
| 373 | return $prometheus->handle( $request ); | ||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | ... | ||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =cut | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # Some handy pseudomethods to make working on HTTP::Response less painful | ||||||||||||||||||||||||||||
| 382 | 1 | ▶ | my $set_header = sub ( $resp, @args ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 383 | 1 | $resp->header( @args ); | |||||||||||||||||||||||||||
| 384 | 1 | $resp; | |||||||||||||||||||||||||||
| 385 | 4 | }; | |||||||||||||||||||||||||||
| 386 | 1 | ▶ | my $set_content = sub ( $resp, @args ){ | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 387 | 1 | $resp->content( @args ); | |||||||||||||||||||||||||||
| 388 | 1 | $resp; | |||||||||||||||||||||||||||
| 389 | 4 | }; | |||||||||||||||||||||||||||
| 390 | 1 | ▶ | my $fix_content_length = sub ( $resp ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 391 | 1 | ▶ | $resp->content_length or $resp->content_length( length $resp->content ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 1 | $resp; | |||||||||||||||||||||||||||
| 393 | 4 | }; | |||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 1 | ▶ | sub handle ( $self, $request ) | ||||||||||||||||||||||||||
|
Subroutine
handle: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 396 | { | ||||||||||||||||||||||||||||
| 397 | 1 | require HTTP::Response; | |||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 1 | ▶ | $request->method eq "GET" or return | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | HTTP::Response->new( 405 ) | ||||||||||||||||||||||||||||
| 401 | ->$set_header( Content_Type => "text/plain" ) | ||||||||||||||||||||||||||||
| 402 | ->$set_content( "Method " . $request->method . " not supported" ) | ||||||||||||||||||||||||||||
| 403 | ->$fix_content_length; | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 1 | my $opts; | |||||||||||||||||||||||||||
| 406 | 1 | ▶ | $opts = { $request->uri->query_form } if length $request->uri->query; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | 1 | return HTTP::Response->new( 200 ) | |||||||||||||||||||||||||||
| 409 | ->$set_header( Content_Type => "text/plain; version=0.0.4; charset=utf-8" ) | ||||||||||||||||||||||||||||
| 410 | ->$set_content( $self->render( $opts ) ) | ||||||||||||||||||||||||||||
| 411 | ->$fix_content_length; | ||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | =head2 psgi_app | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | $app = $prometheus->psgi_app; | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | Returns a new L<PSGI> application as a C<CODE> reference. This application | ||||||||||||||||||||||||||||
| 419 | will render the metrics in the Prometheus text exposition format, suitable for | ||||||||||||||||||||||||||||
| 420 | scraping by the Prometheus collector. | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | This application will respond to any C<GET> request, and reject requests for | ||||||||||||||||||||||||||||
| 423 | any other method. If a C<QUERY_STRING> is present in the environment it will | ||||||||||||||||||||||||||||
| 424 | be parsed for collector options to pass into the L</render> method. | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | This method is useful for integrating metrics into an existing HTTP server | ||||||||||||||||||||||||||||
| 427 | application which is uses or is based on PSGI. For example: | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | use Plack::Builder; | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | my $prometheus = Net::Prometheus::->new; | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | builder { | ||||||||||||||||||||||||||||
| 434 | mount "/metrics" => $prometheus->psgi_app; | ||||||||||||||||||||||||||||
| 435 | ... | ||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =cut | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 1 | ▶ | sub psgi_app ( $self ) | ||||||||||||||||||||||||||
|
Subroutine
psgi_app: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||||||||||||||||||||
| 441 | { | ||||||||||||||||||||||||||||
| 442 | 1 | require URI; | |||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 1 | ▶ | return sub ( $env ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.8
| |||||||||||||||||||||||||||||
| 445 | 1 | my $method = $env->{REQUEST_METHOD}; | |||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | 1 | ▶ | $method eq "GET" or return [ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | 405, | ||||||||||||||||||||||||||||
| 449 | [ "Content-Type" => "text/plain" ], | ||||||||||||||||||||||||||||
| 450 | [ "Method $method not supported" ], | ||||||||||||||||||||||||||||
| 451 | ]; | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 1 | my $opts; | |||||||||||||||||||||||||||
| 454 | 1 | ▶ | if( defined $env->{QUERY_STRING} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 455 | 0 | $opts = +{ URI->new( "?$env->{QUERY_STRING}", "http" )->query_form }; | |||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | return [ | ||||||||||||||||||||||||||||
| 459 | 1 | 200, | |||||||||||||||||||||||||||
| 460 | [ "Content-Type" => "text/plain; version=0.0.4; charset=utf-8" ], | ||||||||||||||||||||||||||||
| 461 | [ $self->render( $opts ) ], | ||||||||||||||||||||||||||||
| 462 | ]; | ||||||||||||||||||||||||||||
| 463 | 1 | }; | |||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | =head2 export_to_Future_IO | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | $f = $prometheus->export_to_Future_IO( %args ); | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | Performs the necessary steps to create a minimal HTTP server for exporting | ||||||||||||||||||||||||||||
| 471 | metrics over HTTP, by using L<Future::IO> directly. This requires | ||||||||||||||||||||||||||||
| 472 | C<Future::IO> version 0.11 or above, and a containing process that has already | ||||||||||||||||||||||||||||
| 473 | loaded a non-default loop implementation that supports multiple filehandles. | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | This new server will listen on its own port number for any incoming request, | ||||||||||||||||||||||||||||
| 476 | and will serve metrics regardless of path. | ||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||
| 478 | This server is a very small, minimal implementation just sufficient to support | ||||||||||||||||||||||||||||
| 479 | C<prometheus> itself, or simple tools like C<wget>, C<curl> or perhaps a | ||||||||||||||||||||||||||||
| 480 | web-browser for manual inspection. It is not intended to be a fully-featured | ||||||||||||||||||||||||||||
| 481 | HTTP server and certainly does not support many HTTP features at all. | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | Takes the following named arguments: | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | =over 4 | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | =item port => INT | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | Port number on which to listen for incoming HTTP requests. | ||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | =back | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | The returned L<Future> instance will remain pending for the entire lifetime of | ||||||||||||||||||||||||||||
| 494 | the process. If the containing program has nothing else to do it can call the | ||||||||||||||||||||||||||||
| 495 | C<await> method on it, or else combine it with other toplevel event futures it | ||||||||||||||||||||||||||||
| 496 | is using for its own purposes. | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | =cut | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | 0 | ▶ | sub export_to_Future_IO ( $self, %args ) | ||||||||||||||||||||||||||
|
Subroutine
export_to_Future_IO: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 501 | { | ||||||||||||||||||||||||||||
| 502 | 0 | require Net::Prometheus::_FutureIO; | |||||||||||||||||||||||||||
| 503 | 0 | require IO::Socket::IP; | |||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | my $listensock = IO::Socket::IP->new( | ||||||||||||||||||||||||||||
| 506 | LocalPort => $args{port}, | ||||||||||||||||||||||||||||
| 507 | 0 | ▶ | Type => Socket::SOCK_STREAM(), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 508 | # TODO: LocalHost | ||||||||||||||||||||||||||||
| 509 | Listen => 1, | ||||||||||||||||||||||||||||
| 510 | ReuseAddr => 1, | ||||||||||||||||||||||||||||
| 511 | ) or die "Cannot create listening socket - $@"; | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | 0 | ▶ | $args{on_listen} and $args{on_listen}->( $listensock ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | 0 | return Net::Prometheus::_FutureIO->start( $self, $listensock ); | |||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | =head2 export_to_IO_Async | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | $prometheus->export_to_IO_Async( $loop, %args ); | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | Performs the necessary steps to create an HTTP server for exporting metrics | ||||||||||||||||||||||||||||
| 523 | over HTTP via L<IO::Async>. This will involve creating a new | ||||||||||||||||||||||||||||
| 524 | L<Net::Async::HTTP::Server> instance added to the loop. | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | This new server will listen on its own port number for any incoming request, | ||||||||||||||||||||||||||||
| 527 | and will serve metrics regardless of path. | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | Note this should only be used in applications that don't otherwise have an | ||||||||||||||||||||||||||||
| 530 | HTTP server, such as self-contained monitoring exporters or exporting metrics | ||||||||||||||||||||||||||||
| 531 | as a side-effect of other activity. For existing HTTP server applications it | ||||||||||||||||||||||||||||
| 532 | is better to integrate with the existing request/response processing of the | ||||||||||||||||||||||||||||
| 533 | application, such as by using the L</handle> or L</psgi_app> methods. | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | Takes the following named arguments: | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | =over 4 | ||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | =item port => INT | ||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | Port number on which to listen for incoming HTTP requests. | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | =back | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | =cut | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | 0 | ▶ | sub export_to_IO_Async ( $self, $loop, %args ) | ||||||||||||||||||||||||||
|
Subroutine
export_to_IO_Async: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 548 | { | ||||||||||||||||||||||||||||
| 549 | 0 | require IO::Async::Loop; | |||||||||||||||||||||||||||
| 550 | 0 | require Net::Async::HTTP::Server; | |||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | 0 | ▶ | $loop //= IO::Async::Loop->new; | ||||||||||||||||||||||||||
|
Condition0%0/3
$loop //= "IO::Async::Loop"->new
MC/DC0%0/2
$loop //= "IO::Async::Loop"->new
$loop
"IO::Async::Loop"->new
Truth table0%0/3
A //= B
A= $loopB= "IO::Async::Loop"->new
| |||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | my $httpserver = Net::Async::HTTP::Server->new( | ||||||||||||||||||||||||||||
| 555 | 0 | ▶ | on_request => sub ( $httpserver, $req ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 556 | 0 | $req->respond( $self->handle( $req->as_http_request ) ); | |||||||||||||||||||||||||||
| 557 | }, | ||||||||||||||||||||||||||||
| 558 | 0 | ); | |||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | 0 | $loop->add( $httpserver ); | |||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | # Yes this is a blocking call | ||||||||||||||||||||||||||||
| 563 | $httpserver->listen( | ||||||||||||||||||||||||||||
| 564 | socktype => "stream", | ||||||||||||||||||||||||||||
| 565 | service => $args{port}, | ||||||||||||||||||||||||||||
| 566 | 0 | )->get; | |||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | { | ||||||||||||||||||||||||||||
| 570 | 4 | package | |||||||||||||||||||||||||||
| 571 | Net::Prometheus::_MetricGroup; | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | 2 | ▶ | sub new ( $class, $prometheus, %args ) | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 574 | { | ||||||||||||||||||||||||||||
| 575 | return bless { | ||||||||||||||||||||||||||||
| 576 | prometheus => $prometheus, | ||||||||||||||||||||||||||||
| 577 | namespace => $args{namespace}, | ||||||||||||||||||||||||||||
| 578 | subsystem => $args{subsystem}, | ||||||||||||||||||||||||||||
| 579 | 2 | }, $class; | |||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | 4 | my $metapkg = meta::get_this_package; | |||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 4 | foreach my $method (qw( new_gauge new_counter new_summary new_histogram )) { | |||||||||||||||||||||||||||
| 585 | 2 | ▶ | $metapkg->add_named_sub( $method => sub ( $self, @args ) { | ||||||||||||||||||||||||||
|
Subroutine
new_gauge: called CC=1 SCAR=2.6
Subroutine
new_histogram: not called CC=1 SCAR=2.6
Subroutine
new_summary: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 586 | $self->{prometheus}->$method( | ||||||||||||||||||||||||||||
| 587 | namespace => $self->{namespace}, | ||||||||||||||||||||||||||||
| 588 | subsystem => $self->{subsystem}, | ||||||||||||||||||||||||||||
| 589 | 0 | @args, | |||||||||||||||||||||||||||
| 590 | ); | ||||||||||||||||||||||||||||
| 591 | 16 | } ); | |||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | =head1 COLLECTORS | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | The toplevel C<Net::Prometheus> object stores a list of "collector" instances, | ||||||||||||||||||||||||||||
| 598 | which are used to generate the values that will be made visible via the | ||||||||||||||||||||||||||||
| 599 | L</render> method. A collector can be any object instance that has a method | ||||||||||||||||||||||||||||
| 600 | called C<collect>, which when invoked is passed no arguments and expected to | ||||||||||||||||||||||||||||
| 601 | return a list of L<Net::Prometheus::Types/MetricSamples> structures. | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | @metricsamples = $collector->collect( $opts ) | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | The L<Net::Prometheus::Metric> class is already a valid collector (and hence, | ||||||||||||||||||||||||||||
| 606 | so too are the individual metric type subclasses). This interface allows the | ||||||||||||||||||||||||||||
| 607 | creation of new custom collector objects, that more directly collect | ||||||||||||||||||||||||||||
| 608 | information to be exported. | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | Collectors might choose to behave differently in the presence of some | ||||||||||||||||||||||||||||
| 611 | specifically-named option; typically to provide extra detail not normally | ||||||||||||||||||||||||||||
| 612 | provided (maybe at the expense of extra processing time to calculate it). | ||||||||||||||||||||||||||||
| 613 | Collectors must not complain about the presence of unrecognised options; the | ||||||||||||||||||||||||||||
| 614 | hash is shared among all potential collectors. | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | =cut | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =head1 TODO | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | =over 8 | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | =item * | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | Histogram/Summary 'start_timer' support | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | =item * | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | Add other C<export_to_*> methods for other event systems and HTTP-serving | ||||||||||||||||||||||||||||
| 629 | frameworks, e.g. L<Mojo>. | ||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||
| 631 | =back | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | =cut | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | =cut | ||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | 4 | 0x55AA; | |||||||||||||||||||||||||||