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

Registering the same metric multiple times #495

Open
sazzer opened this issue Jul 16, 2023 · 1 comment
Open

Registering the same metric multiple times #495

sazzer opened this issue Jul 16, 2023 · 1 comment

Comments

@sazzer
Copy link

sazzer commented Jul 16, 2023

I've written some Axum middleware to record the number of times each HTTP request is processed, and I'm hitting problems with actually recording the metrics.

What I've done so far is this:

pub async fn record_metrics<B>(
    registry: Extension<prometheus::Registry>,
    request: Request<B>,
    next: Next<B>,
) -> Response {
    let uri = request.uri().to_string();

    let response = next.run(request).await;

    let counter = CounterVec::new(Opts::new("http_request", "HTTP Requests"), &["uri", "status_code"]).unwrap();
    registry.register(Box::new(counter.clone())).unwrap();

    counter.with_label_values(&[&uri, response.status().as_str()]).inc();

    response
}

And the call to registry.register() returns AlreadyReg on all incoming requests after the first one.

Is there some easy way to get back the already registered metric so that I can increment that instead?

I've looked at calling registry.gather() to try and find it, but can't trivially work out how to find the correct entry in the returned set.

Cheers

@sunng87
Copy link

sunng87 commented Jul 6, 2024

Hitting the same situation with #525. We need to be able to get the registered metric from register function. The ideal usage is whenever I call register with legal input, it returns the registered metric, whenever it is just registered, or registered by other call.

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

No branches or pull requests

2 participants