Skip to content

Commit 02f9d56

Browse files
committed
Added a unittest with the feature configuration
1 parent 392a5ca commit 02f9d56

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

opentelemetry-sdk/src/metrics/meter.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -712,9 +712,7 @@ mod tests {
712712
};
713713

714714
#[cfg(not(feature = "experimental_metrics_disable_name_validation"))]
715-
use super::{
716-
INSTRUMENT_NAME_EMPTY, INSTRUMENT_NAME_FIRST_ALPHABETIC, INSTRUMENT_NAME_INVALID_CHAR,
717-
};
715+
use super::{INSTRUMENT_NAME_FIRST_ALPHABETIC, INSTRUMENT_NAME_INVALID_CHAR};
718716

719717
#[test]
720718
#[cfg(not(feature = "experimental_metrics_disable_name_validation"))]
@@ -733,6 +731,11 @@ mod tests {
733731
("", INSTRUMENT_NAME_EMPTY),
734732
("\\allow\\slash /sec", INSTRUMENT_NAME_FIRST_ALPHABETIC),
735733
("\\allow\\$$slash /sec", INSTRUMENT_NAME_FIRST_ALPHABETIC),
734+
("Total $ Count", INSTRUMENT_NAME_INVALID_CHAR),
735+
(
736+
"\\test\\UsagePercent(Total) > 80%",
737+
INSTRUMENT_NAME_FIRST_ALPHABETIC,
738+
),
736739
("/not / allowed", INSTRUMENT_NAME_FIRST_ALPHABETIC),
737740
];
738741
for (name, expected_error) in instrument_name_test_cases {
@@ -768,6 +771,8 @@ mod tests {
768771
("", INSTRUMENT_NAME_EMPTY),
769772
("\\allow\\slash /sec", ""),
770773
("\\allow\\$$slash /sec", ""),
774+
("Total $ Count", ""),
775+
("\\test\\UsagePercent(Total) > 80%", ""),
771776
("/not / allowed", ""),
772777
];
773778
for (name, expected_error) in instrument_name_test_cases {

opentelemetry-sdk/src/metrics/mod.rs

+67-2
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,16 @@ mod tests {
132132
use std::time::Duration;
133133

134134
// Run all tests in this mod
135-
// cargo test metrics::tests --features=testing
135+
// cargo test metrics::tests --features=testing,spec_unstable_metrics_views
136136
// Note for all tests from this point onwards in this mod:
137137
// "multi_thread" tokio flavor must be used else flush won't
138138
// be able to make progress!
139139

140140
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
141+
#[cfg(not(feature = "experimental_metrics_disable_name_validation"))]
141142
async fn invalid_instrument_config_noops() {
142143
// Run this test with stdout enabled to see output.
143-
// cargo test invalid_instrument_config_noops --features=testing -- --nocapture
144+
// cargo test invalid_instrument_config_noops --features=testing,spec_unstable_metrics_views -- --nocapture
144145
let invalid_instrument_names = vec![
145146
"_startWithNoneAlphabet",
146147
"utf8char锈",
@@ -215,6 +216,66 @@ mod tests {
215216
}
216217
}
217218

219+
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
220+
#[cfg(feature = "experimental_metrics_disable_name_validation")]
221+
async fn valid_instrument_config_noops_with_feature_experimental_metrics_disable_name_validation(
222+
) {
223+
// Run this test with stdout enabled to see output.
224+
// cargo test valid_instrument_config_noops_with_feature_experimental_metrics_disable_name_validation --features "testing,spec_unstable_metrics_views,experimental_metrics_disable_name_validation" -- --nocapture
225+
let invalid_instrument_names = vec![
226+
"_startWithNoneAlphabet",
227+
"utf8char锈",
228+
"\\allow\\slash /sec",
229+
"\\allow\\$$slash /sec",
230+
"Total $ Count",
231+
"\\test\\UsagePercent(Total) > 80%",
232+
"invalid name",
233+
];
234+
for name in invalid_instrument_names {
235+
let test_context = TestContext::new(Temporality::Cumulative);
236+
let counter = test_context.meter().u64_counter(name).build();
237+
counter.add(1, &[]);
238+
239+
let up_down_counter = test_context.meter().i64_up_down_counter(name).build();
240+
up_down_counter.add(1, &[]);
241+
242+
let gauge = test_context.meter().f64_gauge(name).build();
243+
gauge.record(1.9, &[]);
244+
245+
let histogram = test_context.meter().f64_histogram(name).build();
246+
histogram.record(1.0, &[]);
247+
248+
let _observable_counter = test_context
249+
.meter()
250+
.u64_observable_counter(name)
251+
.with_callback(move |observer| {
252+
observer.observe(1, &[]);
253+
})
254+
.build();
255+
256+
let _observable_gauge = test_context
257+
.meter()
258+
.f64_observable_gauge(name)
259+
.with_callback(move |observer| {
260+
observer.observe(1.0, &[]);
261+
})
262+
.build();
263+
264+
let _observable_up_down_counter = test_context
265+
.meter()
266+
.i64_observable_up_down_counter(name)
267+
.with_callback(move |observer| {
268+
observer.observe(1, &[]);
269+
})
270+
.build();
271+
272+
test_context.flush_metrics();
273+
274+
// As instrument name is invalid, no metrics should be exported
275+
test_context.check_no_metrics();
276+
}
277+
}
278+
218279
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
219280
async fn counter_aggregation_delta() {
220281
// Run this test with stdout enabled to see output.
@@ -2495,6 +2556,10 @@ mod tests {
24952556
.get_finished_metrics()
24962557
.expect("metrics expected to be exported"); // TODO: Need to fix InMemoryMetricExporter to return None.
24972558

2559+
#[cfg(feature = "experimental_metrics_disable_name_validation")]
2560+
assert!(!resource_metrics.is_empty(), "metrics should be exported"); // TODO: Update this when enhanced feature flag is added.
2561+
2562+
#[cfg(not(feature = "experimental_metrics_disable_name_validation"))]
24982563
assert!(resource_metrics.is_empty(), "no metrics should be exported");
24992564
}
25002565

0 commit comments

Comments
 (0)