Prometheus::Tiny - A tiny Prometheus client
use Prometheus::Tiny;
my $prom = Prometheus::Tiny->new;
$prom->set('some_metric', 5, { some_label => "aaa" });
print $prom->format;
Prometheus::Tiny
is a minimal metrics client for the
Prometheus time-series database.
It does the following things differently to Net::Prometheus:
- No setup. You don't need to pre-declare metrics to get something useful.
- Labels are passed in a hash. Positional parameters get awkward.
- No inbuilt collectors, PSGI apps, etc. Just the metrics.
- Doesn't know anything about different metric types. You get what you ask for.
These could all be pros or cons, depending on what you need. For me, I needed a compact base that I could back on a shared memory region. See Prometheus::Tiny::Shared for that!
my $prom = Prometheus::Tiny->new;
my $prom = Promethus::Tiny->new(default_labels => { my_label => "frob" });
If you pass a default_labels
key to the constructor, these labels will be
included in every metric created on this object.
$prom->set($name, $value, { labels }, [timestamp])
Set the value for the named metric. The labels hashref is optional. The timestamp (milliseconds since epoch) is optional, but requires labels to be provided to use. An empty hashref will work in the case of no labels.
Trying to set a metric to a non-numeric value will emit a warning and the metric will be set to zero.
$prom->add($name, $amount, { labels })
Add the given amount to the already-stored value (or 0 if it doesn't exist). The labels hashref is optional.
Trying to add a non-numeric value to a metric will emit a warning and 0 will be added instead (this will still create the metric if it didn't exist, and will update timestamps etc).
$prom->inc($name, { labels })
A shortcut for
$prom->add($name, 1, { labels })
$prom->dec($name, { labels })
A shortcut for
$prom->add($name, -1, { labels })
$prom->clear;
Remove all stored metric values. Metric metadata (set by declare
) is preserved.
$prom->histogram_observe($name, $value, { labels })
Record a histogram observation. The labels hashref is optional.
You should declare your metric beforehand, using the buckets
key to set the
buckets you want to use. If you don't, the following buckets will be used.
[ 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 7.5, 10 ]
$prom->enum_set($name, $value, { labels }, [timestamp])
Set an enum value for the named metric. The labels hashref is optiona. The timestamp is optional.
You should declare your metric beforehand, using the enum
key to set the
label to use for the enum value, and the enum_values
key to list the
possible values for the enum.
$prom->declare($name, help => $help, type => $type, buckets => [...])
"Declare" a metric by associating metadata with it. Valid keys are:
-
help
Text describing the metric. This will appear in the formatted output sent to Prometheus.
-
type
Type of the metric, typically
gauge
orcounter
. -
buckets
For
histogram
metrics, an arrayref of the buckets to use. Seehistogram_observe
. -
enum
For
enum
metrics, the name of the label to use for the enum value. Seeenum_set
. -
enum_values
For
enum
metrics, the possible values the enum can take. Seeenum_set
.
Declaring a already-declared metric will work, but only if the metadata keys
and values match the previous call. If not, declare
will throw an exception.
my $metrics = $prom->format
Output the stored metrics, values, help text and types in the Prometheus exposition format.
use Plack::Builder
builder {
mount "/metrics" => $prom->psgi;
};
Returns a simple PSGI app that, when hooked up to a web server and called, will
return formatted metrics for Prometheus. This is little more than a wrapper
around format
, namely:
sub app {
my $env = shift;
return [ 200, [ 'Content-Type' => 'text/plain' ], [ $prom->format ] ];
}
This is just a convenience; if you already have a web server or you want to
ship metrics via some other means (eg the Node Exporter's textfile collector),
just use format
.
Please report any bugs or feature requests through the issue tracker at https://github.com/robn/Prometheus-Tiny/issues. You will be notified automatically of any progress on your issue.
This is open source software. The code repository is available for public review and contribution under the terms of the license.
https://github.com/robn/Prometheus-Tiny
git clone https://github.com/robn/Prometheus-Tiny.git
- Rob Norris robn@despairlabs.com
- ben hengst ben.hengst@dreamhost.com
- Danijel Tasov data@consol.de
- Michael McClimon michael@mcclimon.org
This software is copyright (c) 2017 by Rob Norris robn@despairlabs.com
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.