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

Provide a way to set a Counter to an absolute value #252

Open
K900 opened this issue Dec 4, 2024 · 8 comments
Open

Provide a way to set a Counter to an absolute value #252

K900 opened this issue Dec 4, 2024 · 8 comments

Comments

@K900
Copy link

K900 commented Dec 4, 2024

I'm writing a reexporter for another service that already outputs aggregated metrics (but not in a Prometheus compatible format). It would be very convenient to be able to directly call .set(existing_value) on a Prometheus counter, instead of having to track the previous value manually and inc_by a computed amount.

@mxinden
Copy link
Member

mxinden commented Dec 5, 2024

@K900
Copy link
Author

K900 commented Dec 5, 2024

AFAIUI const counters just can't be changed at all? I want to be able to change the value, I just already have it pre-aggregated.

@mxinden
Copy link
Member

mxinden commented Dec 5, 2024

You would use the Collector trait to implement your own metrics collector. On a scrape of your exporter by Prometheus, prometheus_client would call Collector::collect. Your collector implementation would retrieve the metric values from your actual monitoring target, and expose the values via e.g. ConstCounter. See Collector docs for details.

Hope this is helpful.

@K900
Copy link
Author

K900 commented Dec 5, 2024

Hmm, this would require a lot of refactoring, but I think it could work.

@K900
Copy link
Author

K900 commented Dec 5, 2024

Actually, maybe not... Getting values from the target is async right now, and Collector's interface is sync, so I'd have to block...

@K900
Copy link
Author

K900 commented Dec 19, 2024

OK so some refactoring later, it looks like I can't really do this without Collector being async, or using my own atomics or whatever to store values...

@mxinden
Copy link
Member

mxinden commented Jan 6, 2025

Getting values from the target is async right now, and Collector's interface is sync, so I'd have to block...

@K900 is that a problem? I assume you are running your /metrics http endpoint on a separate OS thread. Prometheus scrapes an enpoint on the order of seconds. I assume the latency to your target is low. Why is blocking on a future an issue here?

@K900
Copy link
Author

K900 commented Jan 6, 2025

Mostly just because I'm calling another API client that's already async. I can probably run it on another thread, but so far everything has been part of one big event loop.

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