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

desc: Replace regex name validation with functions #89

Merged
merged 4 commits into from
Nov 8, 2016

Conversation

kamalmarhubi
Copy link
Contributor

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

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 {
Copy link
Contributor

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:-)

Copy link
Contributor Author

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);
Copy link
Contributor

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);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@siddontang
Copy link
Contributor

LGTM

Thanks @kamalmarhubi

@siddontang
Copy link
Contributor

PTAL @overvenus

Copy link
Member

@overvenus overvenus left a 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() {
Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@siddontang
Copy link
Contributor

LGTM

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.

3 participants