This crate is meant to make writing a proper Prometheus exporter with a minimal effort. It gives you two things.
rust
PrometheusMetric::build()
.with_name("folder_size")
.with_metric_type(MetricType::Counter)
.with_help("Size of the folder")
.build()
.render_and_append_instance(
&PrometheusInstance::new()
.with_label("folder", "/var/log")
.with_value(total_size_log)
.with_current_timestamp()
.expect("error getting the UNIX epoch"),
)
.render()
GET
and responding only to the /metrics
suffix) so all you have to do is supply a Boxed future that will handle your logic. I use it on these crates: prometheuswireguardexporter and prometheusiotaexporter so please refer to these crates if you want to see a real-world example. More simple examples are available in the examples folder.
The PrometheusMetric
struct is used by instantiating it and then "rendering" the header and values - optionally specifying labels. This is an example taken from the documentation:
rust
PrometheusMetric::build()
.with_name("folder_size")
.with_metric_type(MetricType::Counter)
.with_help("Size of the folder")
.build()
.render_and_append_instance(
&PrometheusInstance::new()
.with_label("folder", "/var/log")
.with_value(total_size_log)
.with_current_timestamp()
.expect("error getting the UNIX epoch"),
)
.render()
This will give you something like this:
For a more complete example please refer to the examples folder.
To use Hyper server all you have to do is call the render_prometheus
function. This function requests you to pass:
([0, 0, 0, 0], 32221).into()
listens on every interface on port 32221.For example:
rust
render_prometheus(addr, MyOptions::default(), |request, options| {
async {
Ok("it works!".to_owned())
}
});
As you can see, in order to keep things simple, the Hyper server does not enforce anything to the output. It's up to you to return a meaningful string by using the above mentioned structs.
Once running, test your exporter with any GET enabled tool (such as a browser) at http://127.0.0.1:<your_exporter_port>/metric
.
Please see the LICENSE file (spoiler alert: it's MIT).