| 1 | package Net::Prometheus::Pushgateway; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use 5.14.2; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Carp qw/croak carp/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | my %METRIC_VALID_TYPES = ( | |||||||||||||||||||
| 13 | 'untyped' => 1, | ||||||||||||||||||||
| 14 | 'counter' => 1, | ||||||||||||||||||||
| 15 | 'gauge' => 1, | ||||||||||||||||||||
| 16 | 'histogram' => 1, | ||||||||||||||||||||
| 17 | 'summary' => 1, | ||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | sub new { | ||||||||||||||||||||
| 21 | 0 | ▶ | my ($class, %opt) = @_; | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 22 | 0 | my $self = {}; | |||||||||||||||||||
| 23 | 0 | ▶ | $self->{'host'} = $opt{'-host'} // croak "You must specify '-host' param"; | ||||||||||||||||||
|
Condition0%0/3
$opt{"-host"} // croak("You must specify '-host' param")
MC/DC0%0/2
$opt{"-host"} // croak("You must specify '-host' param")
$opt{"-host"}
croak("You must specify '-host' param")
Truth table0%0/3
A // B
A= $opt{"-host"}B= croak("You must specify '-host' param")
| |||||||||||||||||||||
| 24 | 0 | ▶ | $self->{'port'} = $opt{'-port'} // croak "You must specify '-port' param"; | ||||||||||||||||||
|
Condition0%0/3
$opt{"-port"} // croak("You must specify '-port' param")
MC/DC0%0/2
$opt{"-port"} // croak("You must specify '-port' param")
$opt{"-port"}
croak("You must specify '-port' param")
Truth table0%0/3
A // B
A= $opt{"-port"}B= croak("You must specify '-port' param")
| |||||||||||||||||||||
| 25 | 0 | my $path = $opt{'-path'}; | |||||||||||||||||||
| 26 | 0 | $self->{'ua'} = LWP::UserAgent->new(); | |||||||||||||||||||
| 27 | 0 | $self->{'url'} = 'http://' . $self->{host} . ':' . $self->{'port'} . $path; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | return bless $self, $class; | |||||||||||||||||||
| 30 | } | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | sub add { | ||||||||||||||||||||
| 33 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 34 | 0 | my $raw_str = $self->_add(@_); | |||||||||||||||||||
| 35 | 0 | return $self->_send_to_prometheus($raw_str); | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | sub increment { | ||||||||||||||||||||
| 39 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
increment: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 40 | 0 | my $raw_str = $self->_add( | |||||||||||||||||||
| 41 | @_, | ||||||||||||||||||||
| 42 | '-value' => 1, | ||||||||||||||||||||
| 43 | '-type' => 'counter', | ||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||
| 45 | 0 | return $self->_send_to_prometheus($raw_str); | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub summary { | ||||||||||||||||||||
| 49 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
summary: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 50 | 0 | my $raw_str = $self->_add( | |||||||||||||||||||
| 51 | @_, | ||||||||||||||||||||
| 52 | '-type' => 'summary', | ||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||
| 54 | 0 | return $self->_send_to_prometheus($raw_str); | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | sub gauge { | ||||||||||||||||||||
| 58 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
gauge: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 59 | 0 | my $raw_str = $self->_add( | |||||||||||||||||||
| 60 | @_, | ||||||||||||||||||||
| 61 | '-type' => 'gauge', | ||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||
| 63 | 0 | return $self->_send_to_prometheus($raw_str); | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub histogram { | ||||||||||||||||||||
| 67 | 0 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||
|
Subroutine
histogram: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||
| 68 | 0 | ▶ | my $metric_name = $opt{'-metric_name'} // croak "You must specify '-metric_name' param"; | ||||||||||||||||||
|
Condition0%0/3
$opt{"-metric_name"} // croak("You must specify '-metric_name' param")
MC/DC0%0/2
$opt{"-metric_name"} // croak("You must specify '-metric_name' param")
$opt{"-metric_name"}
croak("You must specify '-metric_name' param")
Truth table0%0/3
A // B
A= $opt{"-metric_name"}B= croak("You must specify '-metric_name' param")
| |||||||||||||||||||||
| 69 | 0 | ▶ | my $label = $opt{'-label'} // {}; | ||||||||||||||||||
|
Condition0%0/2
$opt{"-label"} // {}
MC/DC0%0/1
$opt{"-label"} // {}
$opt{"-label"}
Truth table0%0/2
A
A= $opt{"-label"}
| |||||||||||||||||||||
| 70 | 0 | ▶ | my $value = $opt{'-value'} // croak "You must specify '-value' param"; | ||||||||||||||||||
|
Condition0%0/3
$opt{"-value"} // croak("You must specify '-value' param")
MC/DC0%0/2
$opt{"-value"} // croak("You must specify '-value' param")
$opt{"-value"}
croak("You must specify '-value' param")
Truth table0%0/3
A // B
A= $opt{"-value"}B= croak("You must specify '-value' param")
| |||||||||||||||||||||
| 71 | 0 | ▶ | my $buckets = $opt{'-buckets'} // croak "You must specify '-buckets' param"; | ||||||||||||||||||
|
Condition0%0/3
$opt{"-buckets"} // croak("You must specify '-buckets' param")
MC/DC0%0/2
$opt{"-buckets"} // croak("You must specify '-buckets' param")
$opt{"-buckets"}
croak("You must specify '-buckets' param")
Truth table0%0/3
A // B
A= $opt{"-buckets"}B= croak("You must specify '-buckets' param")
| |||||||||||||||||||||
| 72 | 0 | ▶ | croak "Param '-buckets' must be arrayref" if ref($buckets) ne 'ARRAY'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | 0 | ▶ | croak "Label must be hashref" if ref($label) ne 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | my @metrics; | |||||||||||||||||||
| 76 | 0 | push @metrics, "# TYPE $metric_name histogram\n"; | |||||||||||||||||||
| 77 | 0 | push @metrics, $self->_prepare_raw_metric($metric_name . '_count', $label, 1); | |||||||||||||||||||
| 78 | 0 | push @metrics, $self->_prepare_raw_metric($metric_name . '_sum', $label, $value); | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 0 | for my $bucket (@$buckets) { | |||||||||||||||||||
| 81 | 0 | ▶ | push @metrics, $self->_prepare_raw_metric($metric_name . '_bucket', { %$label, 'le' => $bucket}, $value <= $bucket ? 1 : 0); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | 0 | push @metrics, $self->_prepare_raw_metric($metric_name . '_bucket', { %$label, 'le' => '+Inf'}, 1); | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | return $self->_send_to_prometheus(join('', @metrics)); | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | sub _add { | ||||||||||||||||||||
| 89 | 0 | ▶ | my ($self, %opt) = @_; | ||||||||||||||||||
|
Subroutine
_add: not called CC=7 SCAR=40.3
| |||||||||||||||||||||
| 90 | 0 | ▶ | my $metric_name = $opt{'-metric_name'} // croak "You must specify '-metric_name' param"; | ||||||||||||||||||
|
Condition0%0/3
$opt{"-metric_name"} // croak("You must specify '-metric_name' param")
MC/DC0%0/2
$opt{"-metric_name"} // croak("You must specify '-metric_name' param")
$opt{"-metric_name"}
croak("You must specify '-metric_name' param")
Truth table0%0/3
A // B
A= $opt{"-metric_name"}B= croak("You must specify '-metric_name' param")
| |||||||||||||||||||||
| 91 | 0 | ▶ | my $label = $opt{'-label'} // {}; | ||||||||||||||||||
|
Condition0%0/2
$opt{"-label"} // {}
MC/DC0%0/1
$opt{"-label"} // {}
$opt{"-label"}
Truth table0%0/2
A
A= $opt{"-label"}
| |||||||||||||||||||||
| 92 | 0 | ▶ | my $value = $opt{'-value'} // croak "You must specify '-value' param"; | ||||||||||||||||||
|
Condition0%0/3
$opt{"-value"} // croak("You must specify '-value' param")
MC/DC0%0/2
$opt{"-value"} // croak("You must specify '-value' param")
$opt{"-value"}
croak("You must specify '-value' param")
Truth table0%0/3
A // B
A= $opt{"-value"}B= croak("You must specify '-value' param")
| |||||||||||||||||||||
| 93 | 0 | ▶ | my $type = $opt{'-type'} // 'untyped'; | ||||||||||||||||||
|
Condition0%0/2
$opt{"-type"} // "untyped"
MC/DC0%0/1
$opt{"-type"} // "untyped"
$opt{"-type"}
Truth table0%0/2
A
A= $opt{"-type"}
| |||||||||||||||||||||
| 94 | 0 | $type = lc($type); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | ▶ | croak "Label must be hashref" if ref($label) ne 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 0 | ▶ | croak "Unvalid metric type: '$type'. Valid types: " . join(', ', keys %METRIC_VALID_TYPES) if not $METRIC_VALID_TYPES{$type}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 0 | my $type_str = "# TYPE $metric_name $type\n"; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 0 | my $raw_metric = $self->_prepare_raw_metric($metric_name, $label, $value); | |||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | return $type_str . $raw_metric; | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | sub _prepare_raw_metric { | ||||||||||||||||||||
| 107 | 0 | ▶ | my ($self, $metric_name, $label, $value) = @_; | ||||||||||||||||||
|
Subroutine
_prepare_raw_metric: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 108 | 0 | my $raw_str = $metric_name; | |||||||||||||||||||
| 109 | 0 | ▶ | if ($label) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | 0 | $raw_str .= '{' . join (', ', map {$_ . '="' . $label->{$_} . '"'} keys %$label) . '}'; | |||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | 0 | $raw_str .= " $value\n"; | |||||||||||||||||||
| 113 | 0 | return $raw_str; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | sub _send_to_prometheus { | ||||||||||||||||||||
| 117 | 0 | ▶ | my ($self, $str) = @_; | ||||||||||||||||||
|
Subroutine
_send_to_prometheus: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 0 | my $request = HTTP::Request->new('POST', $self->{'url'}); | |||||||||||||||||||
| 120 | 0 | $request->content($str); | |||||||||||||||||||
| 121 | 0 | my $response = $self->{'ua'}->request($request); | |||||||||||||||||||
| 122 | 0 | ▶ | return 1 if ($response->is_success); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | croak "Can't send POST request to '$self->{'url'}'. MSG: " . $response->decoded_content . " Code: " . $response->code; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 1 | 1; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =pod | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =encoding UTF-8 | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head1 NAME | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | B<Net::Prometheus::Pushgateway> - client module for pushing metrics to prometheus exporter (pushgateway, prometheus aggregation gateway) | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head1 SYNOPSYS | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | use Net::Prometheus::Pushgateway; | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | # Create Net::Prometheus::Pushgateway object for pushgateway exporter | ||||||||||||||||||||
| 142 | my $metric = Net::Prometheus::Pushgateway->new( | ||||||||||||||||||||
| 143 | '-host' => '127.0.0.1', | ||||||||||||||||||||
| 144 | '-port' => 9091, | ||||||||||||||||||||
| 145 | '-path' => '/metrics/job/<job_name>/instance/<instance_name>', | ||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||
| 147 | # OR | ||||||||||||||||||||
| 148 | # Create Net::Prometheus::Pushgateway object for prometheus aggregation gateway | ||||||||||||||||||||
| 149 | my $metric = Net::Prometheus::Pushgateway->new( | ||||||||||||||||||||
| 150 | '-host' => '127.0.0.1', | ||||||||||||||||||||
| 151 | '-port' => 9091, | ||||||||||||||||||||
| 152 | '-path' => '/api/ui/metrics', | ||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | # Send increment metric | ||||||||||||||||||||
| 156 | $metric->increment(-metric_name => 'perl_metric_increment', -label => {'perl_label' => 5}); | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | # Send summary metric | ||||||||||||||||||||
| 159 | $metric->summary(-metric_name => 'perl_metric_summary', -label => {'perl_label' => 5}, -value => 15); | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | # Send histogram metric | ||||||||||||||||||||
| 162 | $metric->histogram(-metric_name => 'perl_metric_histogram', -label => {'perl_label' => 5}, -value => 15, -buckets => [qw/1 2 3 4 5/]); | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | =head1 METHODS | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =head2 new(%opt) | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | Create Net::Prometheus::Pushgateway object | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | Options: | ||||||||||||||||||||
| 172 | -host => Prometheus exporter host | ||||||||||||||||||||
| 173 | -port => Prometheus exporter port number | ||||||||||||||||||||
| 174 | -path => Path to prometheus exporter host (/api/ui/metrics - prometheus aggregation gateway, /metrics/job/<job_name>/instance/<instance_name> - prometeus | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head1 PUSH METRICS | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head1 add(%opt) | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | Push custom metrics | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | Options: | ||||||||||||||||||||
| 183 | -metric_name => Name of pushed metrics | ||||||||||||||||||||
| 184 | -label => HashRef to metric labels | ||||||||||||||||||||
| 185 | -value => Metric value | ||||||||||||||||||||
| 186 | -type => Metric type (default: untyped. Valid metric types in %Net::Prometheus::Pushgateway::METRIC_VALID_TYPE)S | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head2 increment(%opt) | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | Push increment metrics | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | Options: | ||||||||||||||||||||
| 193 | -metric_name => Name of pushed metrics | ||||||||||||||||||||
| 194 | -label => HashRef to metric labels | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =head2 summary(%opt) | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | Push summary metrics | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | Options: | ||||||||||||||||||||
| 201 | -metric_name => Name of pushed metrics | ||||||||||||||||||||
| 202 | -value => Metric value | ||||||||||||||||||||
| 203 | -label => HashRef to metric labels (default: {}) | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =head2 histogram(%opt) | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | Push histogram metric | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | Options: | ||||||||||||||||||||
| 210 | -metric_name => Name of pushed metrics | ||||||||||||||||||||
| 211 | -value => Metric value | ||||||||||||||||||||
| 212 | -label => HashRef to metric labels (default: {}) | ||||||||||||||||||||
| 213 | -buckets => ArayRef to buckets values | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | =head1 DEPENDENCE | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | L<LWP::UserAgent> | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =head1 AUTHORS | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =over 4 | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | =item * | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | Pavel Andryushin <vrag867@gmail.com> | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | =back | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | This software is copyright (c) 2020 by Pavel Andryushin. | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 234 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||