-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example for actix-web framework #54
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of comments. Thanks for the work here. More examples are very much appreciated.
examples/actix-web.rs
Outdated
|
||
/// Holds all metrics. | ||
/// We shouldn't store metrics inside a MetricsCollector - | ||
/// overwise we would have to take a lock for each increment of any metric |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// overwise we would have to take a lock for each increment of any metric | |
/// otherwise we would have to take a lock for each increment of any metric. |
examples/actix-web.rs
Outdated
use prometheus_client::registry::Registry; | ||
|
||
#[derive(Clone, Hash, PartialEq, Eq, Encode)] | ||
pub enum RequestType { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pub enum RequestType { | |
pub enum Method { |
In the tide
example we call it Method
. I don't really care about the names, though I would prefer to be consistent. What do you think?
https://github.com/prometheus/client_rust/blob/master/examples/tide.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, agree.
examples/actix-web.rs
Outdated
async fn main() -> std::io::Result<()> { | ||
let metrics = web::Data::new(Metrics::new()); | ||
// We have to wrap it with Mutex because `Registry` doesn't implement Copy-trait | ||
let metrics_collector = web::Data::new(Mutex::new(MetricsCollector::new(&metrics))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that encode
takes an immutable reference to Registry
. Thus there is no need for a Mutex
in case you never register new metrics after the first Prometheus scrape. An Arc
should suffice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reasonable, please check out a comment
examples/actix-web.rs
Outdated
} | ||
|
||
/// Registers and collects metrics | ||
pub struct MetricsCollector { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this wrapper around Registry
? Despite the fact that client_rust
does use the Collector
abstraction (yet see #49), if I am not mistaken this concept is not wrapping a Registry
in the other implementations, but rather the other way around.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to avoid Mutex here in this abstraction, but didn't found the one.. would like to see any suggestions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, it looks like the PR fixes this issue, will try to adapt the example in few days.
👋 @oriontvv still interested in finishing this pull request? |
Yes, I will simplify the example without unnecessary wrapper in few days 👋 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very clean. Two more comments. Otherwise this looks good to me.
### Added | ||
|
||
- Example for actix-web framework. See [PR 54]. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[PR 54]: https://github.com/prometheus/client_rust/pull/54/ | |
examples/actix-web.rs
Outdated
let mut buf = Vec::new(); | ||
encode(&mut buf, &state.registry)?; | ||
let body = std::str::from_utf8(buf.as_slice()).unwrap().to_string(); | ||
Ok(HttpResponse::Ok().body(body)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would one not need to set the content type?
Line 37 in 6b7a345
.content_type("application/openmetrics-text; version=1.0.0; charset=utf-8") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually no, my experience shows that it is not required. Everything works well without this detail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the OpenMetrics specification it "MUST" be set. Am I missing something @oriontvv?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
//CC @RichiH
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll add this. (btw, we use at work without it and everything is fine)
Signed-off-by: oriontvv <taranov.vv@gmail.com>
Signed-off-by: oriontvv <taranov.vv@gmail.com>
Signed-off-by: oriontvv <taranov.vv@gmail.com>
There is no need for mutable access to the `Registry` once all metrics have been registered. Encoding the registered metrics does not require a mutable reference. With that in mind, an `Arc` is good enough. There is no need for a `Mutex`. Signed-off-by: Max Inden <mail@max-inden.de> Signed-off-by: oriontvv <taranov.vv@gmail.com>
This will allow the default `Registry` to be used in multi-threaded environments (e.g. web servers) where a ref to the registry needs to be shared between threads. Closes #57. Signed-off-by: Ben Sully <ben.sully@grafana.com> Signed-off-by: Max Inden <mail@max-inden.de> Co-authored-by: Max Inden <mail@max-inden.de> Signed-off-by: oriontvv <taranov.vv@gmail.com>
Signed-off-by: oriontvv <taranov.vv@gmail.com>
Sorry for the garbage commits. I'm trying to figure out how to deal with your tricky checks of signing. |
Sorry for the trouble. It is unfortunate that we require this from a legal side. |
src/registry.rs
Outdated
@@ -57,7 +57,6 @@ use std::borrow::Cow; | |||
/// # "# EOF\n"; | |||
/// # assert_eq!(expected, String::from_utf8(buffer).unwrap()); | |||
/// ``` | |||
#[derive(Debug)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, it was because of unsuccessful conflicts with rebase, fixed
Signed-off-by: oriontvv <taranov.vv@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Just needs the sign-off. Feel free to just squash into a single signed off commit in case that is more convenient for you @oriontvv.
Signed-off-by: oriontvv <taranov.vv@gmail.com>
reopened in #76 |
Resolves #53