line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Prometheus::Pushgateway; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
55199
|
use 5.14.2; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
16
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
19
|
|
6
|
1
|
|
|
1
|
|
506
|
use utf8; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
3
|
|
7
|
1
|
|
|
1
|
|
28
|
use Carp qw/croak carp/; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
39
|
|
8
|
1
|
|
|
1
|
|
549
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
38952
|
|
|
1
|
|
|
|
|
434
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
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
|
0
|
|
|
0
|
1
|
|
my ($class, %opt) = @_; |
22
|
0
|
|
|
|
|
|
my $self = {}; |
23
|
0
|
|
0
|
|
|
|
$self->{'host'} = $opt{'-host'} // croak "You must specify '-host' param"; |
24
|
0
|
|
0
|
|
|
|
$self->{'port'} = $opt{'-port'} // 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 increment { |
33
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
34
|
0
|
|
|
|
|
|
$self->add( |
35
|
|
|
|
|
|
|
@_, |
36
|
|
|
|
|
|
|
'-value' => 1, |
37
|
|
|
|
|
|
|
'-type' => 'counter', |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub summary { |
42
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
43
|
0
|
|
|
|
|
|
$self->add( |
44
|
|
|
|
|
|
|
@_, |
45
|
|
|
|
|
|
|
'-type' => 'summary', |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub add { |
50
|
0
|
|
|
0
|
0
|
|
my ($self, %opt) = @_; |
51
|
0
|
|
0
|
|
|
|
my $metric_name = $opt{'-metric_name'} // croak "You must specify '-metric_name' param"; |
52
|
0
|
|
0
|
|
|
|
my $label = $opt{'-label'} // {}; |
53
|
0
|
|
0
|
|
|
|
my $value = $opt{'-value'} // croak "You must specify '-value' param"; |
54
|
0
|
|
0
|
|
|
|
my $type = lc($opt{'-type'}) // 'untyped'; |
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
croak "Label must be hashref" if ref($label) ne 'HASH'; |
57
|
0
|
0
|
|
|
|
|
croak "Unvalid metric type: '$type'. Valid types: " . join(', ', keys %METRIC_VALID_TYPES) if not $METRIC_VALID_TYPES{$type}; |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
my $raw_str = "# TYPE $metric_name $type\n"; |
60
|
0
|
|
|
|
|
|
$raw_str .= $metric_name; |
61
|
0
|
0
|
|
|
|
|
if ($label) { |
62
|
0
|
|
|
|
|
|
$raw_str .= '{' . join (', ', map {$_ . '="' . $label->{$_} . '"'} keys %$label) . '}'; |
|
0
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
} |
64
|
0
|
|
|
|
|
|
$raw_str .= " $value\n"; |
65
|
0
|
|
|
|
|
|
return $self->_send_to_prometheus($raw_str); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _send_to_prometheus { |
69
|
0
|
|
|
0
|
|
|
my ($self, $str) = @_; |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
my $request = HTTP::Request->new('POST', $self->{'url'}); |
72
|
0
|
|
|
|
|
|
$request->content($str); |
73
|
0
|
|
|
|
|
|
my $response = $self->{'ua'}->request($request); |
74
|
0
|
0
|
|
|
|
|
return 1 if ($response->is_success); |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
croak "Can't send POST request to '$self->{'url'}'. MSG: " . $response->decoded_content . " Code: " . $response->code; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=pod |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=encoding UTF-8 |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 NAME |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
B - client module for pushing metrics to prometheus exporter (pushgateway, prometheus aggregation gateway) |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 SYNOPSYS |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
use Net::Prometheus::Pushgateway; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Create Net::Prometheus::Pushgateway object for pushgateway exporter |
94
|
|
|
|
|
|
|
my $metric = Net::Prometheus::Pushgateway->new( |
95
|
|
|
|
|
|
|
'-host' => '127.0.0.1', |
96
|
|
|
|
|
|
|
'-port' => 9091, |
97
|
|
|
|
|
|
|
'-path' => '/metrics/job//instance/', |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
# OR |
100
|
|
|
|
|
|
|
# Create Net::Prometheus::Pushgateway object for prometheus aggregation gateway |
101
|
|
|
|
|
|
|
my $metric = Net::Prometheus::Pushgateway->new( |
102
|
|
|
|
|
|
|
'-host' => '127.0.0.1', |
103
|
|
|
|
|
|
|
'-port' => 9091, |
104
|
|
|
|
|
|
|
'-path' => '/api/ui/metrics', |
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# Send increment metric |
108
|
|
|
|
|
|
|
$metric->increment(-metric_name => 'perl_metric_increment', -label => {'perl_label' => 5}); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# Send summary metric |
111
|
|
|
|
|
|
|
$metric->summary(-metric_name => 'perl_metric_summary', -label => {'perl_label' => 5}, value => 15); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 METHODS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 new(%opt) |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Create Net::Prometheus::Pushgateway object |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Options: |
122
|
|
|
|
|
|
|
-host => Prometheus exporter host |
123
|
|
|
|
|
|
|
-port => Prometheus exporter port number |
124
|
|
|
|
|
|
|
-path => Path to prometheus exporter host (/api/ui/metrics - prometheus aggregation gateway, /metrics/job//instance/ - prometeus |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 PUSH METRICS |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 add(%opt) |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Push custom metrics |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Options: |
133
|
|
|
|
|
|
|
-metric_name => Name of pushed metrics |
134
|
|
|
|
|
|
|
-label => HashRef to metric labels |
135
|
|
|
|
|
|
|
-value => Metric value |
136
|
|
|
|
|
|
|
-type => Metric type (default: untyped. Valid metric types in %Net::Prometheus::Pushgateway::METRIC_VALID_TYPE)S |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 increment(%opt) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Push increment metrics |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Options: |
143
|
|
|
|
|
|
|
-metric_name => Name of pushed metrics |
144
|
|
|
|
|
|
|
-label => HashRef to metric labels |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 summary(%opt) |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Push summary metrics |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Options: |
151
|
|
|
|
|
|
|
-metric_name => Name of pushed metrics |
152
|
|
|
|
|
|
|
-value => Metric value |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 DEPENDENCE |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
L |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 AUTHORS |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=over 4 |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item * |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Pavel Andryushin |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=back |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
This software is copyright (c) 2020 by Pavel Andryushin. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
173
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=cut |