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

Don't be generic over numbers #1

Open
wants to merge 3 commits into
base: serde-style
Choose a base branch
from

Conversation

thomaseizinger
Copy link

We only have a limited number of data types in prometheus. Instead of being generic over them, unroll the abstractions manually and one implementation per data type.

mxinden and others added 3 commits November 19, 2022 20:32
Adopt encoding style similar to serde. `EncodeXXX` for a type that can be
encoded (see serde's `Serialize`) and `XXXEncoder` for a supported encoding i.e.
text and protobuf (see serde's `Serializer`).

- Compatible with Rust's additive features. Enabling the `protobuf` feature does
not change type signatures.
- `EncodeMetric` is trait object safe, and thus `Registry` can use dynamic dispatch.
- Implement a single encoding trait `EncodeMetric` per metric type instead of
one implementation per metric AND per encoding format.
- Leverage `std::fmt::Write` instead of `std::io::Write`. The OpenMetrics text
format has to be unicode compliant. The OpenMetrics Protobuf format requires
labels to be valid strings.

Signed-off-by: Max Inden <mail@max-inden.de>
@mxinden
Copy link
Owner

mxinden commented Nov 27, 2022

Thanks for providing the patch Thomas! Very much appreciate you exploring alternative approaches.

I am not convinced this is the best way forward.

This patch reduces complexity by cutting out the the EncodeCounterValue, EncodeGaugeValue and EncodeExemplarValue traits plus their corresponding *Encoder types. As a side effect that results in a nice reduction in lines of code.

I don't expect the majority of all library users to implement any of the Encode*Value traits, but instead, make use of the existing implementations. I do expect some power-users to implement EnocdeMetric on their own type. To me, this patch only reduces complexity of the former, but not the latter.

With that in mind, I don't think the reduced complexity of this patch is worth the additional methods on MetricEncoder.

Let's keep this pull request open. I want to experiment with the large refactoring in downstream code which might change my mind.

@thomaseizinger
Copy link
Author

Up to you! My main concern is the use of type parameters for a problem that doesn't need to be "open to extension". The set of types is fixed within prometheus but the usage of type parameters suggests that it is not and should instead be extended.

serde follows the same approach btw. It defines a data model and allows you to operate within that but you can't extend it. That makes sense because a particular data format has to operate within a defined data model if it wants to be compatible with the ecosystem.

The bottom line is that I think we are overly abstract here which creates a fair bit of indirection and complexity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants