| 1 | package Net::Prometheus::Pushgateway; | ||
| 2 | |||
| 3 | use 5.14.2; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use utf8; | ||
| 7 | use Carp qw/croak carp/; | ||
| 8 | use LWP::UserAgent; | ||
| 9 | |||
| 10 | our $VERSION = '0.02'; | ||
| 11 | |||
| 12 | 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 | my ($class, %opt) = @_; | ||
| 22 | my $self = {}; | ||
| 23 | $self->{'host'} = $opt{'-host'} // croak "You must specify '-host' param"; | ||
| 24 | $self->{'port'} = $opt{'-port'} // croak "You must specify '-port' param"; | ||
| 25 | my $path = $opt{'-path'}; | ||
| 26 | $self->{'ua'} = LWP::UserAgent->new(); | ||
| 27 | $self->{'url'} = 'http://' . $self->{host} . ':' . $self->{'port'} . $path; | ||
| 28 | |||
| 29 | return bless $self, $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub add { | ||
| 33 | my $self = shift; | ||
| 34 | my $raw_str = $self->_add(@_); | ||
| 35 | return $self->_send_to_prometheus($raw_str); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub increment { | ||
| 39 | my $self = shift; | ||
| 40 | my $raw_str = $self->_add( | ||
| 41 | @_, | ||
| 42 | '-value' => 1, | ||
| 43 | '-type' => 'counter', | ||
| 44 | ); | ||
| 45 | return $self->_send_to_prometheus($raw_str); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub summary { | ||
| 49 | my $self = shift; | ||
| 50 | my $raw_str = $self->_add( | ||
| 51 | @_, | ||
| 52 | '-type' => 'summary', | ||
| 53 | ); | ||
| 54 | return $self->_send_to_prometheus($raw_str); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub gauge { | ||
| 58 | my $self = shift; | ||
| 59 | my $raw_str = $self->_add( | ||
| 60 | @_, | ||
| 61 | '-type' => 'gauge', | ||
| 62 | ); | ||
| 63 | return $self->_send_to_prometheus($raw_str); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub histogram { | ||
| 67 | my ($self, %opt) = @_; | ||
| 68 | my $metric_name = $opt{'-metric_name'} // croak "You must specify '-metric_name' param"; | ||
| 69 | my $label = $opt{'-label'} // {}; | ||
| 70 | my $value = $opt{'-value'} // croak "You must specify '-value' param"; | ||
| 71 | my $buckets = $opt{'-buckets'} // croak "You must specify '-buckets' param"; | ||
| 72 | croak "Param '-buckets' must be arrayref" if ref($buckets) ne 'ARRAY'; | ||
| 73 | croak "Label must be hashref" if ref($label) ne 'HASH'; | ||
| 74 | |||
| 75 | my @metrics; | ||
| 76 | push @metrics, "# TYPE $metric_name histogram\n"; | ||
| 77 | push @metrics, $self->_prepare_raw_metric($metric_name . '_count', $label, 1); | ||
| 78 | push @metrics, $self->_prepare_raw_metric($metric_name . '_sum', $label, $value); | ||
| 79 | |||
| 80 | for my $bucket (@$buckets) { | ||
| 81 | push @metrics, $self->_prepare_raw_metric($metric_name . '_bucket', { %$label, 'le' => $bucket}, $value <= $bucket ? 1 : 0); | ||
| 82 | } | ||
| 83 | push @metrics, $self->_prepare_raw_metric($metric_name . '_bucket', { %$label, 'le' => '+Inf'}, 1); | ||
| 84 | |||
| 85 | return $self->_send_to_prometheus(join('', @metrics)); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _add { | ||
| 89 | my ($self, %opt) = @_; | ||
| 90 | my $metric_name = $opt{'-metric_name'} // croak "You must specify '-metric_name' param"; | ||
| 91 | my $label = $opt{'-label'} // {}; | ||
| 92 | my $value = $opt{'-value'} // croak "You must specify '-value' param"; | ||
| 93 | my $type = $opt{'-type'} // 'untyped'; | ||
| 94 | $type = lc($type); | ||
| 95 | |||
| 96 | croak "Label must be hashref" if ref($label) ne 'HASH'; | ||
| 97 | croak "Unvalid metric type: '$type'. Valid types: " . join(', ', keys %METRIC_VALID_TYPES) if not $METRIC_VALID_TYPES{$type}; | ||
| 98 | |||
| 99 | my $type_str = "# TYPE $metric_name $type\n"; | ||
| 100 | |||
| 101 | my $raw_metric = $self->_prepare_raw_metric($metric_name, $label, $value); | ||
| 102 | |||
| 103 | return $type_str . $raw_metric; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _prepare_raw_metric { | ||
| 107 | my ($self, $metric_name, $label, $value) = @_; | ||
| 108 | my $raw_str = $metric_name; | ||
| 109 | if ($label) { | ||
| 110 | $raw_str .= '{' . join (', ', map {$_ . '="' . $label->{$_} . '"'} keys %$label) . '}'; | ||
| 111 | } | ||
| 112 | $raw_str .= " $value\n"; | ||
| 113 | return $raw_str; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub _send_to_prometheus { | ||
| 117 | my ($self, $str) = @_; | ||
| 118 | |||
| 119 | my $request = HTTP::Request->new('POST', $self->{'url'}); | ||
| 120 | $request->content($str); | ||
| 121 | my $response = $self->{'ua'}->request($request); | ||
| 122 | return 1 if ($response->is_success); | ||
| 123 | |||
| 124 | croak "Can't send POST request to '$self->{'url'}'. MSG: " . $response->decoded_content . " Code: " . $response->code; | ||
| 125 | } | ||
| 126 | |||
| 127 | 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 |