Skip to content

Commit

Permalink
Make metrics exporter OpenMetrics-compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
sorz committed Dec 7, 2021
1 parent f1cbdd9 commit 7730e07
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Features:
first response)
* Optional status web page (latency, traffic, etc. w/ curl-friendly output)
* Optional [Graphite](https://graphite.readthedocs.io/) and
[Prometheus](https://prometheus.io/) support
OpenMetrics ([Prometheus](https://prometheus.io/)) support
(to build fancy dashboard with [Grafana](https://grafana.com/) for example)
* Customizable proxy selection algorithm with Lua script (see
[conf/simple_scroe.lua](conf/simple_score.lua)).
Expand Down Expand Up @@ -125,7 +125,7 @@ given IP address and port number. It returns a HTML page for web browser,
or a ASCII table for `curl`.

The stats page only provides current metrics and a few aggregations. Graphite
(via `--graphite`) or Prometheus (via `--stats-bind` then `\metrics`) should
(via `--graphite`) or OpenMetrics (via `--stats-bind` then `\metrics`) should
be used if you want a full history.

Some examples of Prometheus query (Grafana variant):
Expand Down
6 changes: 4 additions & 2 deletions src/web/open_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{
use super::{ServerStatus, Status};
use crate::{monitor::Monitor, proxy::Delay};

const CONTENT_TYPE: &str = "application/openmetrics-text; version=1.0.0; charset=utf-8";

fn new_metric(buf: &mut String, name: &str, metric_type: &str, help: &str) {
writeln!(buf, "# HELP moproxy_{} {}", name, help).unwrap();
writeln!(buf, "# TYPE moproxy_{} {}", name, metric_type).unwrap();
Expand Down Expand Up @@ -37,7 +39,6 @@ pub fn exporter(start_time: &Instant, monitor: &Monitor) -> Response<Body> {
($name:expr, $help:expr, $func:expr) => {
new_metric(&mut buf, $name, "gauge", $help);
each_server(&mut buf, $name, &status.servers, $func);
writeln!(&mut buf).unwrap();
};
}

Expand Down Expand Up @@ -80,8 +81,9 @@ pub fn exporter(start_time: &Instant, monitor: &Monitor) -> Response<Body> {
|s| s.server.status_snapshot().score
);

writeln!(buf, "# EOF").unwrap();
Response::builder()
.header("Content-Type", "text/plain; charset=utf-8")
.header("Content-Type", CONTENT_TYPE)
.body(buf.into())
.unwrap()
}

0 comments on commit 7730e07

Please sign in to comment.