Skip to content
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

Closed
wants to merge 14 commits into from
Closed

Add example for actix-web framework #54

wants to merge 14 commits into from

Conversation

oriontvv
Copy link
Contributor

Resolves #53

Copy link
Member

@mxinden mxinden left a 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.


/// 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// 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.

use prometheus_client::registry::Registry;

#[derive(Clone, Hash, PartialEq, Eq, Encode)]
pub enum RequestType {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, agree.

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)));
Copy link
Member

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.

Copy link
Contributor Author

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

}

/// Registers and collects metrics
pub struct MetricsCollector {
Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With #57 fixed, is this still needed @oriontvv?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With #57 fixed, is this still needed @oriontvv?

Copy link
Contributor Author

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.

@mxinden
Copy link
Member

mxinden commented Jul 18, 2022

👋 @oriontvv still interested in finishing this pull request?

@oriontvv
Copy link
Contributor Author

Yes, I will simplify the example without unnecessary wrapper in few days 👋

Copy link
Member

@mxinden mxinden left a 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].

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[PR 54]: https://github.com/prometheus/client_rust/pull/54/

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))
Copy link
Member

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?

.content_type("application/openmetrics-text; version=1.0.0; charset=utf-8")

Copy link
Contributor Author

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.

Copy link
Member

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?

https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#overall-structure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//CC @RichiH

Copy link
Contributor Author

@oriontvv oriontvv Jul 25, 2022

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)

examples/actix-web.rs Outdated Show resolved Hide resolved
oriontvv and others added 8 commits July 23, 2022 00:54
Signed-off-by: oriontvv <taranov.vv@gmail.com>
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>
Signed-off-by: oriontvv <taranov.vv@gmail.com>
@oriontvv
Copy link
Contributor Author

Sorry for the garbage commits. I'm trying to figure out how to deal with your tricky checks of signing.

@mxinden
Copy link
Member

mxinden commented Jul 25, 2022

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)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Contributor Author

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

Copy link
Member

@mxinden mxinden left a 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>
@oriontvv
Copy link
Contributor Author

reopened in #76

This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add example for actix-web framework
3 participants