Example metrics server using Hyper#94
Conversation
9400593 to
5021289
Compare
mxinden
left a comment
There was a problem hiding this comment.
🚀 Thanks for the help. Very much appreciated.
| Response::builder() | ||
| .header( | ||
| hyper::header::CONTENT_TYPE, | ||
| "application/openmetrics-text; version=1.0.0; charset=utf-8", |
examples/hyper.rs
Outdated
| loop { | ||
| EXAMPLE_COUNTER.inc(); | ||
| tokio::time::sleep(Duration::from_secs(1)).await; | ||
| } |
There was a problem hiding this comment.
This is a neat idea, though for the sake of consistency with the other examples, I would prefer showcasing a standard HTTP request counter in this example.
See tide example:
Lines 47 to 57 in 12c1de5
I think an example should showcase a single thing only. (Though it is hard where the boundaries of a thing are.) I am fine for something like the above to be shown in a separate example.
There was a problem hiding this comment.
I agree that examples should only show a single thing. From my point of view, having an example counter simplifies the example, because this is really just teaching users how to serve a metrics endpoint using Hyper. It's not really about how metrics are created and used, just about serving them.
53f15ad to
99c62f4
Compare
mxinden
left a comment
There was a problem hiding this comment.
Just two comments. Otherwise looks good to me. Thanks for the follow-ups.
examples/hyper.rs
Outdated
| async fn main() { | ||
| let request_counter = Counter::default(); | ||
|
|
||
| let registry = register_metrics(&request_counter); |
There was a problem hiding this comment.
I find it confusing that the logic for registering a metric is extracted into a separate function. It is only called once. Say one would have more than one metric to register, the function would register all of them under the same name.
Can you expand on the rational for the extraction? With what I understand today, I would prefer for it to be removed.
There was a problem hiding this comment.
I removed the function, you're right
examples/hyper.rs
Outdated
| Box::pin(async move { | ||
| let mut buf = Vec::new(); | ||
| encode(&mut buf, ®.clone()).map(|_| { | ||
| let body = std::str::from_utf8(buf.as_slice()).unwrap().to_string(); |
There was a problem hiding this comment.
Given that one can build a Body from a [u8] why is the conversion to a string necessary here?
https://docs.rs/hyper/latest/hyper/struct.Body.html#impl-From%3C%26%27static%20%5Bu8%5D%3E-for-Body
There was a problem hiding this comment.
Ah great point, thanks for pointing that out. Simplified it.
Signed-off-by: Adam Chalmers <adam.s.chalmers@gmail.com>
6d909c6 to
c73f09c
Compare
mxinden
left a comment
There was a problem hiding this comment.
Thanks for the help. Thanks for making it easier for others to get started.
Say your Rust app is not a web server. You probably don't need to pull in a whole web framework like Axum or Actix-web just to serve one little OpenMetrics endpoint. You can just use Hyper instead. Here's an example of how to do that.
I also added this an example target in Cargo.toml, so that you can run it with cargo: