-
Notifications
You must be signed in to change notification settings - Fork 82
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
Const Metrics / Custom Collectors #36
Comments
Happy that it is of some use ❤️
In this particular use-case, would a metric type called constant not be misleading? In other words, the use-case here produces a non-constant metric, thus one would not expect to be using a metric type with const in its name, no? The crate allows you to access the inner (atomic) type of a /// Exposes the inner atomic type of the [`Counter`].
///
/// This should only be used for advanced use-cases which are not directly
/// supported by the library.
///
/// The caller of this function has to uphold the property of an Open
/// Metrics counter namely that the value is monotonically increasing, i.e.
/// either stays the same or increases.
pub fn inner(&self) -> &A { With this little trick in mind, would the following work for you? let counter: Counter = Counter::default();
// Register the counter with a registry.
// Update the counter value to represent the OS value.
counter.inner().store(os_value); In addition to achieving your goal, your metric would also properly be tagged with the OpenMetrics Counter type.
I agree that this is not obvious. On top of that, there is an additional solution to the same problem, namely to implement |
I agree, but that's the terminology in the official Go Prometheus client, see for example the mysql_exporter where it used for exporting counters from the database itself, or node_exporter where it's used for host CPU stats. It appears to be "const" because after you create it there are no methods to do fancy things (inc, set, etc), you just get an exportable metric that lasts some short period of time (probably until you're done with the scrape). This matches the comment for the type: // NewConstMetric returns a metric with one fixed value that cannot be
// changed. Users of this package will not have much use for it in regular
// operations. However, when implementing custom Collectors, it is useful as a
// throw-away metric that is generated on the fly to send it to Prometheus in
// the Collect method. The Python client calls them Custom Collectors which is probably a less confusing name, and also registers the custom collector with the registry. The Python version seems far clearer in intent and usage.
That's actually a pretty interesting approach, and one I may try out. It would probably simplify my existing code quite a bit by taking care of a few weird edge cases. For now, as long as Thanks a lot for the discussion :) Edit: Minor title update to reflect what this type of metric is called in other clients. I should have read more of the issues too, this is probably the same request as #11. |
Looking at the For example, when looking at how existing |
Possibly. I'm going to have to take a while and try to adapt it to something much closer to what I'm actually doing at the moment. My actual code that's going to use this is at jail_exporter/exporter.rs (this was my first real Rust project, apologies for the state of the code). This is an exporter for FreeBSD jails, it currently uses the tikv/prometheus crate. Most of the metrics there will be easy to adapt, they're simple gauges and don't need anything fancy. The I also need the ability to remove label sets from a metric family, I'm not sure that this is implemented yet. This is because jails may come and go while the exporter is running. In the tests and some code there, you can see the horrific counter bookkeeping that this custom collector feature should help me avoid. :) |
Feel free to tag me on any pull request for a review in case I can be of some help.
Correct. That is not supported today, though is easy to add as one only has to expose client_rust/src/metrics/family.rs Lines 100 to 101 in 16aa166
|
Cross referencing proposal here: #82 |
Thanks, that proposal is looking good. I may also have a PR shortly for the |
You definitely can't reset a counter to 0 arbitrarily :) A counter can only be incremented, it must never be decremented or set to an arbitrary value, resets to 0 are acceptable under the assumption that the process hosting that counter has restarted. |
Hi,
Thanks for creating an official Rust crate for Prometheus. :)
I'm wondering how we should handle Const Metrics with this crate. Are these supported at the moment? If they are, it's a little non-obvious.
My use case is for exporting counters from the operating system. The OS itself guarantees that these are always increasing (unless the counter wraps, etc), but they're difficult to export with only the
inc
andinc_by
methods as additional variables must be maintained to work out the difference between the old OS counter and the current OS counter, so we can finallyinc_by
.If Const Metrics are not currently supported, please consider this a feature request.
Thanks!
The text was updated successfully, but these errors were encountered: