1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
NAME
Net::Prometheus - export monitoring metrics for prometheus
SYNOPSIS
use Net::Prometheus;
my $client = Net::Prometheus->new;
my $counter = $client->new_counter(
name => "requests",
help => "Number of received requests",
);
sub handle_request
{
$counter->inc;
...
}
use Plack::Builder;
builder {
mount "/metrics" => $client->psgi_app;
...
}
DESCRIPTION
This module provides the ability for a program to collect monitoring
metrics and export them to the prometheus.io monitoring server.
As prometheus will expect to collect the metrics by making an HTTP
request, facilities are provided to yield a PSGI application that the
containing program can embed in its own structure to provide the
results, or the application can generate a plain-text result directly
and serve them by its own means.
Metrics::Any
For more flexibility of metrics reporting, other modules may wish to
use Metrics::Any as an abstraction interface instead of directly using
this API.
By using Metrics::Any instead, the module does not directly depend on
Net::Prometheus, and in addition program ultimately using the module
gets the flexibility to use Prometheus (via
Metrics::Any::Adapter::Prometheus) or use another reporting system via
a different adapter.
CONSTRUCTOR
new
$prometheus = Net::Prometheus->new;
Returns a new Net::Prometheus instance.
Takes the following named arguments:
disable_process_collector => BOOL
If present and true, this instance will not load the default process
collector from Net::Prometheus::ProcessCollector. If absent or false,
such a collector will be loaded by default.
disable_perl_collector => BOOL
If present and true, this instance will not load perl-specific
collector from Net::Prometheus::PerlCollector. If absent or false
this collector is loaded by default.
These two options are provided for testing purposes, or for specific
use-cases where such features are not required. Usually it's best
just to leave these enabled.
METHODS
register
$collector = $prometheus->register( $collector );
Registers a new collector to be collected from by the render method.
The collector instance itself is returned, for convenience.
unregister
$prometheus->unregister( $collector );
Removes a previously-registered collector.
new_gauge
$gauge = $prometheus->new_gauge( %args );
Constructs a new Net::Prometheus::Gauge using the arguments given and
registers it with the exporter. The newly-constructed gauge is
returned.
new_counter
$counter = $prometheus->new_counter( %args );
Constructs a new Net::Prometheus::Counter using the arguments given and
registers it with the exporter. The newly-constructed counter is
returned.
new_summary
$summary = $prometheus->new_summary( %args );
Constructs a new Net::Prometheus::Summary using the arguments given and
registers it with the exporter. The newly-constructed summary is
returned.
new_histogram
$histogram = $prometheus->new_histogram( %args );
Constructs a new Net::Prometheus::Histogram using the arguments given
and registers it with the exporter. The newly-constructed histogram is
returned.
new_metricgroup
$group = $prometheus->new_metricgroup( %args );
Returns a new Metric Group instance as a convenience for registering
multiple metrics using the same namespace and subsystem arguments.
Takes the following named arguments:
namespace => STR
subsystem => STR
String values to pass by default into new metrics the group will
construct.
Once constructed, the group acts as a proxy to the other new_* methods,
passing in these values as overrides.
$gauge = $group->new_gauge( ... );
$counter = $group->new_counter( ... );
$summary = $group->new_summary( ... );
$histogram = $group->new_histogram( ... );
collect
@metricsamples = $prometheus->collect( $opts );
Returns a list of "MetricSamples" in Net::Prometheus::Types obtained
from all of the currently-registered collectors.
render
$str = $prometheus->render;
Returns a string in the Prometheus text exposition format containing
the current values of all the registered metrics.
$str = $prometheus->render( { options => "for collectors" } );
An optional HASH reference may be provided; if so it will be passed
into the collect method of every registered collector.
Values that are set to undef will be absent from the output (this
usually applies to gauges). Values set to NaN will be rendered as NaN.
handle
$response = $prometheus->handle( $request );
Given an HTTP request in an HTTP::Request instance, renders the metrics
in response to it and returns an HTTP::Response instance.
This application will respond to any GET request, and reject requests
for any other method. If a query string is present on the URI it will
be parsed for collector options to pass into the "render" method.
This method is useful for integrating metrics into an existing HTTP
server application which uses these objects. For example:
my $prometheus = Net::Prometheus->new;
sub serve_request
{
my ( $request ) = @_;
if( $request->uri->path eq "/metrics" ) {
return $prometheus->handle( $request );
}
...
}
psgi_app
$app = $prometheus->psgi_app;
Returns a new PSGI application as a CODE reference. This application
will render the metrics in the Prometheus text exposition format,
suitable for scraping by the Prometheus collector.
This application will respond to any GET request, and reject requests
for any other method. If a QUERY_STRING is present in the environment
it will be parsed for collector options to pass into the "render"
method.
This method is useful for integrating metrics into an existing HTTP
server application which is uses or is based on PSGI. For example:
use Plack::Builder;
my $prometheus = Net::Prometheus::->new;
builder {
mount "/metrics" => $prometheus->psgi_app;
...
}
export_to_Future_IO
$f = $prometheus->export_to_Future_IO( %args );
Performs the necessary steps to create a minimal HTTP server for
exporting metrics over HTTP, by using Future::IO directly. This
requires Future::IO version 0.11 or above, and a containing process
that has already loaded a non-default loop implementation that supports
multiple filehandles.
This new server will listen on its own port number for any incoming
request, and will serve metrics regardless of path.
This server is a very small, minimal implementation just sufficient to
support prometheus itself, or simple tools like wget, curl or perhaps a
web-browser for manual inspection. It is not intended to be a
fully-featured HTTP server and certainly does not support many HTTP
features at all.
Takes the following named arguments:
port => INT
Port number on which to listen for incoming HTTP requests.
The returned Future instance will remain pending for the entire
lifetime of the process. If the containing program has nothing else to
do it can call the await method on it, or else combine it with other
toplevel event futures it is using for its own purposes.
export_to_IO_Async
$prometheus->export_to_IO_Async( $loop, %args );
Performs the necessary steps to create an HTTP server for exporting
metrics over HTTP via IO::Async. This will involve creating a new
Net::Async::HTTP::Server instance added to the loop.
This new server will listen on its own port number for any incoming
request, and will serve metrics regardless of path.
Note this should only be used in applications that don't otherwise have
an HTTP server, such as self-contained monitoring exporters or
exporting metrics as a side-effect of other activity. For existing HTTP
server applications it is better to integrate with the existing
request/response processing of the application, such as by using the
"handle" or "psgi_app" methods.
Takes the following named arguments:
port => INT
Port number on which to listen for incoming HTTP requests.
COLLECTORS
The toplevel Net::Prometheus object stores a list of "collector"
instances, which are used to generate the values that will be made
visible via the "render" method. A collector can be any object instance
that has a method called collect, which when invoked is passed no
arguments and expected to return a list of "MetricSamples" in
Net::Prometheus::Types structures.
@metricsamples = $collector->collect( $opts )
The Net::Prometheus::Metric class is already a valid collector (and
hence, so too are the individual metric type subclasses). This
interface allows the creation of new custom collector objects, that
more directly collect information to be exported.
Collectors might choose to behave differently in the presence of some
specifically-named option; typically to provide extra detail not
normally provided (maybe at the expense of extra processing time to
calculate it). Collectors must not complain about the presence of
unrecognised options; the hash is shared among all potential
collectors.
TODO
* Histogram/Summary 'start_timer' support
* Add other export_to_* methods for other event systems and
HTTP-serving frameworks, e.g. Mojo.
AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
|