Skip to content

Commit 175f1cf

Browse files
authored
Replace regex crate with glob crate (#1301)
* remove regex crate * update cargo.toml * fix dependency * add unittest * add changelog * order tests * fix unit test * do disk cleanup before tests * fix disk cleanup * fix disk cleanup * fix disk cleanup * fix disk cleanup * fix disk cleanup
1 parent 5eaaf53 commit 175f1cf

File tree

4 files changed

+98
-14
lines changed

4 files changed

+98
-14
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ jobs:
1313
rust: [stable, beta]
1414
runs-on: ubuntu-latest
1515
steps:
16+
- name: Free disk space
17+
run: |
18+
df -h
19+
sudo rm -rf /usr/local/lib/android
20+
sudo rm -rf /usr/share/dotnet
21+
df -h
1622
- uses: actions/checkout@v1
1723
with:
1824
submodules: true

opentelemetry-sdk/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
- Increase instrument name maximum length from 63 to 255 (#1269)
2929
- Updated crate documentation and examples.
3030
[#1256](https://github.com/open-telemetry/opentelemetry-rust/issues/1256)
31+
- Replace regex with glob (#1301)
32+
3133

3234
### Removed
3335

opentelemetry-sdk/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ once_cell = "1.10"
2222
ordered-float = "4.0"
2323
percent-encoding = { version = "2.0", optional = true }
2424
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"], optional = true }
25-
regex = { version = "1.0", optional = true }
25+
glob = {version = "0.3.1", optional =true}
2626
serde = { version = "1.0", features = ["derive", "rc"], optional = true }
2727
serde_json = { version = "1", optional = true }
2828
thiserror = "1"
@@ -47,7 +47,7 @@ trace = ["opentelemetry/trace", "crossbeam-channel", "rand", "async-trait", "per
4747
jaeger_remote_sampler = ["trace", "opentelemetry-http", "http", "serde", "serde_json", "url"]
4848
logs = ["opentelemetry/logs", "crossbeam-channel", "async-trait", "serde_json"]
4949
logs_level_enabled = ["logs", "opentelemetry/logs_level_enabled"]
50-
metrics = ["opentelemetry/metrics", "regex", "async-trait"]
50+
metrics = ["opentelemetry/metrics", "glob", "async-trait"]
5151
testing = ["opentelemetry/testing", "trace", "metrics", "logs", "rt-async-std", "rt-tokio", "rt-tokio-current-thread", "tokio/macros", "tokio/rt-multi-thread"]
5252
rt-tokio = ["tokio", "tokio-stream"]
5353
rt-tokio-current-thread = ["tokio", "tokio-stream"]

opentelemetry-sdk/src/metrics/view.rs

Lines changed: 88 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
use super::instrument::{Instrument, Stream};
2+
use glob::Pattern;
13
use opentelemetry::{
24
global,
35
metrics::{MetricsError, Result},
46
};
5-
use regex::Regex;
6-
7-
use super::instrument::{Instrument, Stream};
87

98
fn empty_view(_inst: &Instrument) -> Option<Stream> {
109
None
@@ -119,16 +118,12 @@ pub fn new_view(criteria: Instrument, mask: Stream) -> Result<Box<dyn View>> {
119118
return Ok(Box::new(empty_view));
120119
}
121120

122-
let pattern = criteria
123-
.name
124-
.trim_start_matches('^')
125-
.trim_end_matches('$')
126-
.replace('?', ".")
127-
.replace('*', ".*");
128-
let re =
129-
Regex::new(&format!("^{pattern}$")).map_err(|e| MetricsError::Config(e.to_string()))?;
121+
let pattern = criteria.name.clone();
122+
let glob_pattern =
123+
Pattern::new(&pattern).map_err(|e| MetricsError::Config(e.to_string()))?;
124+
130125
Box::new(move |i| {
131-
re.is_match(&i.name)
126+
glob_pattern.matches(&i.name)
132127
&& criteria.matches_description(i)
133128
&& criteria.matches_kind(i)
134129
&& criteria.matches_unit(i)
@@ -177,3 +172,84 @@ pub fn new_view(criteria: Instrument, mask: Stream) -> Result<Box<dyn View>> {
177172
}
178173
}))
179174
}
175+
176+
#[cfg(test)]
177+
mod tests {
178+
use super::*;
179+
use crate::metrics::Instrument;
180+
#[test]
181+
fn test_new_view_matching_all() {
182+
let criteria = Instrument::new().name("*");
183+
let mask = Stream::new();
184+
185+
let view = new_view(criteria, mask).expect("Expected to create a new view");
186+
187+
let test_instrument = Instrument::new().name("test_instrument");
188+
assert!(
189+
view.match_inst(&test_instrument).is_some(),
190+
"Expected to match all instruments with * pattern"
191+
);
192+
}
193+
194+
#[test]
195+
fn test_new_view_exact_match() {
196+
let criteria = Instrument::new().name("counter_exact_match");
197+
let mask = Stream::new();
198+
199+
let view = new_view(criteria, mask).expect("Expected to create a new view");
200+
201+
let matching_instrument = Instrument::new().name("counter_exact_match");
202+
assert!(
203+
view.match_inst(&matching_instrument).is_some(),
204+
"Expected to match instrument with exact name"
205+
);
206+
207+
let non_matching_instrument = Instrument::new().name("counter_non_exact_match");
208+
assert!(
209+
view.match_inst(&non_matching_instrument).is_none(),
210+
"Expected not to match instrument with different name"
211+
);
212+
}
213+
214+
#[test]
215+
fn test_new_view_with_wildcard_pattern() {
216+
let criteria = Instrument::new().name("prefix_*");
217+
let mask = Stream::new();
218+
219+
let view = new_view(criteria, mask).expect("Expected to create a new view");
220+
221+
let matching_instrument = Instrument::new().name("prefix_counter");
222+
assert!(
223+
view.match_inst(&matching_instrument).is_some(),
224+
"Expected to match instrument with matching prefix"
225+
);
226+
227+
let non_matching_instrument = Instrument::new().name("nonprefix_counter");
228+
assert!(
229+
view.match_inst(&non_matching_instrument).is_none(),
230+
"Expected not to match instrument with different prefix"
231+
);
232+
}
233+
234+
#[test]
235+
fn test_new_view_wildcard_question_mark() {
236+
let criteria = Instrument::new().name("test_?");
237+
let mask = Stream::new();
238+
239+
let view = new_view(criteria, mask).expect("Expected to create a new view");
240+
241+
// Instrument name that should match the pattern "test_?".
242+
let matching_instrument = Instrument::new().name("test_1");
243+
assert!(
244+
view.match_inst(&matching_instrument).is_some(),
245+
"Expected to match instrument with test_? pattern"
246+
);
247+
248+
// Instrument name that should not match the pattern "test_?".
249+
let non_matching_instrument = Instrument::new().name("test_12");
250+
assert!(
251+
view.match_inst(&non_matching_instrument).is_none(),
252+
"Expected not to match instrument with test_? pattern"
253+
);
254+
}
255+
}

0 commit comments

Comments
 (0)