Skip to content

Commit 8ce59de

Browse files
authored
src/desc: Validate metric and label names via regex crate (#354)
Signed-off-by: Max Inden <[email protected]>
1 parent b7be575 commit 8ce59de

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

benches/desc.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2020 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
#![feature(test)]
15+
16+
extern crate test;
17+
18+
use prometheus::core::Desc;
19+
20+
use test::{black_box, Bencher};
21+
22+
#[bench]
23+
fn description_validation(b: &mut Bencher) {
24+
b.iter(|| {
25+
black_box(Desc::new(
26+
"api_http_requests_total".to_string(),
27+
"not empty help".to_string(),
28+
vec!["method".to_string(), "handler".to_string()],
29+
Default::default(),
30+
))
31+
});
32+
}

src/desc.rs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,30 @@ use std::collections::{BTreeSet, HashMap};
55
use std::hash::Hasher;
66

77
use fnv::FnvHasher;
8+
use regex::Regex;
89

910
use crate::errors::{Error, Result};
1011
use crate::metrics::SEPARATOR_BYTE;
1112
use crate::proto::LabelPair;
1213

1314
// Details of required format are at
14-
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
15+
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
1516
fn is_valid_metric_name(name: &str) -> bool {
16-
// Valid metric names must match regex [a-zA-Z_:][a-zA-Z0-9_:]*.
17-
fn valid_start(c: char) -> bool {
18-
c.is_ascii()
19-
&& match c as u8 {
20-
b'a'..=b'z' | b'A'..=b'Z' | b'_' | b':' => true,
21-
_ => false,
22-
}
23-
}
24-
25-
fn valid_char(c: char) -> bool {
26-
c.is_ascii()
27-
&& match c as u8 {
28-
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' | b':' => true,
29-
_ => false,
30-
}
17+
lazy_static! {
18+
static ref VALIDATOR: Regex =
19+
Regex::new("^[a-zA-Z_:][a-zA-Z0-9_:]*$").expect("Regex to be valid.");
3120
}
3221

33-
name.starts_with(valid_start) && !name.contains(|c| !valid_char(c))
22+
VALIDATOR.is_match(name)
3423
}
3524

3625
fn is_valid_label_name(name: &str) -> bool {
37-
// Valid label names must match regex [a-zA-Z_][a-zA-Z0-9_]*.
38-
fn valid_start(c: char) -> bool {
39-
c.is_ascii()
40-
&& match c as u8 {
41-
b'a'..=b'z' | b'A'..=b'Z' | b'_' => true,
42-
_ => false,
43-
}
44-
}
45-
46-
fn valid_char(c: char) -> bool {
47-
c.is_ascii()
48-
&& match c as u8 {
49-
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' => true,
50-
_ => false,
51-
}
26+
lazy_static! {
27+
static ref VALIDATOR: Regex =
28+
Regex::new("^[a-zA-Z_][a-zA-Z0-9_]*$").expect("Regex to be valid.");
5229
}
5330

54-
name.starts_with(valid_start) && !name.contains(|c| !valid_char(c))
31+
VALIDATOR.is_match(name)
5532
}
5633

5734
/// The descriptor used by every Prometheus [`Metric`](crate::core::Metric). It is essentially

0 commit comments

Comments
 (0)