|
| 1 | +use super::instrument::{Instrument, Stream}; |
| 2 | +use glob::Pattern; |
1 | 3 | use opentelemetry::{
|
2 | 4 | global,
|
3 | 5 | metrics::{MetricsError, Result},
|
4 | 6 | };
|
5 |
| -use regex::Regex; |
6 |
| - |
7 |
| -use super::instrument::{Instrument, Stream}; |
8 | 7 |
|
9 | 8 | fn empty_view(_inst: &Instrument) -> Option<Stream> {
|
10 | 9 | None
|
@@ -119,16 +118,12 @@ pub fn new_view(criteria: Instrument, mask: Stream) -> Result<Box<dyn View>> {
|
119 | 118 | return Ok(Box::new(empty_view));
|
120 | 119 | }
|
121 | 120 |
|
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 | + |
130 | 125 | Box::new(move |i| {
|
131 |
| - re.is_match(&i.name) |
| 126 | + glob_pattern.matches(&i.name) |
132 | 127 | && criteria.matches_description(i)
|
133 | 128 | && criteria.matches_kind(i)
|
134 | 129 | && criteria.matches_unit(i)
|
@@ -177,3 +172,84 @@ pub fn new_view(criteria: Instrument, mask: Stream) -> Result<Box<dyn View>> {
|
177 | 172 | }
|
178 | 173 | }))
|
179 | 174 | }
|
| 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