Skip to content

feat(prometheus-client-derive): initial implemenation #270

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ADD-SP
Copy link
Contributor

@ADD-SP ADD-SP commented Apr 19, 2025

Summary

This PR implements features proposed by #140, supersedes #218 (which doesn't makes new progress in the past several months).

This PR adds and publishes (to crates.io) a new crate prometheus-client-derive, and re-exports its macros in crate prometheus-client, so the user should use prometheus-client instead of prometheus-client-derive. This re-export pattern is also used by serde.

The new crate only has one macros Registrant derive the trait that allows structs to be registered in a registry.

Futures of derive-encode

This PR doesn't touch it, but I think we should merge it into prometheus-client-derive eventually as the first one's functionalities is a subset of the second one.

Since this is a breaking change, this is not the call should be made by outside contributor, even if we can do breaking change when releasing 0.24.0 based on the Rust semantic version.

Issues

close #140, supersede #218

@ADD-SP ADD-SP force-pushed the add_sp/feat-prometheus-client-derive branch from 74c6b17 to a194c89 Compare April 19, 2025 08:35
@@ -24,6 +32,7 @@ parking_lot = "0.12"
prometheus-client-derive-encode = { version = "0.5.0", path = "derive-encode" }
prost = { version = "0.12.0", optional = true }
prost-types = { version = "0.12.0", optional = true }
prometheus-client-derive = { version = "0.24.0", path = "./prometheus-client-derive" }
Copy link
Contributor Author

@ADD-SP ADD-SP Apr 19, 2025

Choose a reason for hiding this comment

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

Shall we gate this dependency using a feature flag?

If so, we should also gate the prometheus-client-derive-encode, which should be considered as a breaking change even if the feature flag is default on.

Since the latest release is 0.23.1, the current version is 0.24.0, we can do breaking changes, but this based on the policy of the project. So the project maintainer may able to make a better call.

Comment on lines +97 to +101
// unit should be lowercase
let unit = syn::LitStr::new(
unit.value().as_str().to_ascii_lowercase().as_str(),
unit.span(),
);
Copy link
Contributor Author

@ADD-SP ADD-SP Apr 19, 2025

Choose a reason for hiding this comment

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

There are two options:

  1. Silently covert it to lowercase .
  2. Emit compilation error.

Unit is a part of the metric name in Prometheus output, but Prometheus allow uppercase in name which is not the best practice.

What do you think?

);
attr.unit = Some(unit);
} else if meta.path.is_ident("rename") {
let rename = meta.value()?.parse::<syn::LitStr>()?;
Copy link
Contributor Author

@ADD-SP ADD-SP Apr 19, 2025

Choose a reason for hiding this comment

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

Enforce "Names SHOULD be in snake_case".

From README.md

Shall we emit compilation error if the metric name is not snake_case?

// public.
#[derive(Default)]
pub struct Attribute {
pub help: Option<syn::LitStr>,
Copy link
Contributor Author

@ADD-SP ADD-SP Apr 19, 2025

Choose a reason for hiding this comment

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

Shall we make the help as a required field? Missing help message might make no sense.

@koushiro
Copy link
Contributor

Interesting idea, would you be interested in porting this PR to my openmetrics library?

@ADD-SP
Copy link
Contributor Author

ADD-SP commented May 10, 2025

@mxinden Could you take a look?

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.

feat: derive register logic on struct of metrics
2 participants