-
Notifications
You must be signed in to change notification settings - Fork 184
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
desc: Replace regex name validation with functions #89
Conversation
This allows dropping the dependency on regex, making the instrumentation library much more lightweight. This also fixes a bug with metric name validation, where an initial colon was incorrectly disallowed. See data model for correct format: https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels This may be because the library closely follows the official Go library, which also had this issue. Refs tikv#85 Refs prometheus/client_golang#255
static ref VALID_LABEL_NAME: Regex = Regex::new(r"^[a-zA-Z_][a-zA-Z0-9_]*$").unwrap(); | ||
// Details of required format are at | ||
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels | ||
fn is_valid_metric_name(name: &str) -> bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer adding some tests to test these function directly now:-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
use errors::Error; | ||
|
||
#[test] | ||
fn test_is_valid_metric_name() { | ||
assert_eq!(is_valid_metric_name(":"), true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer using tuple tables like:
let tbls = vec![("_", true), ("a", true), ("a-", false)];
for (name, valid) in tbls {
assert_eq!(is_valid_metric_name(name), valid);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
LGTM Thanks @kamalmarhubi |
PTAL @overvenus |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
("a_b_9_d:x_", false), | ||
]; | ||
|
||
for &(name, expected) in tbl.iter() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clippy complains explicit-iter-loop
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe for (name, expected) in tbl
is enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
LGTM |
This allows dropping the dependency on regex, making the instrumentation
library much more lightweight.
This also fixes a bug with metric name validation, where an initial
colon was incorrectly disallowed. See data model for correct format:
https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
This may be because the library closely follows the official Go library,
which also had this issue.
Refs #85
Refs prometheus/client_golang#255