|
37 | 37 | //! assert_eq!(expected_msg, buffer); |
38 | 38 | //! ``` |
39 | 39 |
|
40 | | -use crate::encoding::{EncodeExemplarTime, EncodeExemplarValue, EncodeLabelSet, NoLabelSet}; |
41 | | -use crate::metrics::exemplar::Exemplar; |
42 | | -use crate::metrics::MetricType; |
43 | | -use crate::registry::{Prefix, Registry, Unit}; |
| 40 | +use crate::{ |
| 41 | + encoding::{EncodeExemplarTime, EncodeExemplarValue, EncodeLabelSet, NoLabelSet}, |
| 42 | + metrics::{MetricType, exemplar::Exemplar}, |
| 43 | + registry::{Prefix, Registry, Unit}, |
| 44 | +}; |
44 | 45 |
|
45 | | -use std::borrow::Cow; |
46 | | -use std::collections::HashMap; |
47 | | -use std::fmt::Write; |
| 46 | +use std::{borrow::Cow, collections::HashMap, fmt::Write}; |
48 | 47 |
|
49 | 48 | /// Encode both the metrics registered with the provided [`Registry`] and the |
50 | 49 | /// EOF marker into the provided [`Write`]r using the OpenMetrics text format. |
@@ -736,17 +735,21 @@ impl std::fmt::Write for LabelValueEncoder<'_> { |
736 | 735 | #[cfg(test)] |
737 | 736 | mod tests { |
738 | 737 | use super::*; |
739 | | - use crate::metrics::exemplar::HistogramWithExemplars; |
740 | | - use crate::metrics::family::Family; |
741 | | - use crate::metrics::gauge::Gauge; |
742 | | - use crate::metrics::histogram::{exponential_buckets, Histogram}; |
743 | | - use crate::metrics::info::Info; |
744 | | - use crate::metrics::{counter::Counter, exemplar::CounterWithExemplar}; |
| 738 | + use crate::metrics::{ |
| 739 | + counter::Counter, |
| 740 | + exemplar::{CounterWithExemplar, HistogramWithExemplars}, |
| 741 | + family::Family, |
| 742 | + gauge::Gauge, |
| 743 | + histogram::{Histogram, exponential_buckets}, |
| 744 | + info::Info, |
| 745 | + }; |
745 | 746 | use pyo3::{prelude::*, types::PyModule}; |
746 | | - use std::borrow::Cow; |
747 | | - use std::fmt::Error; |
748 | | - use std::sync::atomic::{AtomicI32, AtomicU32}; |
749 | | - use std::time::{SystemTime, UNIX_EPOCH}; |
| 747 | + use std::{ |
| 748 | + borrow::Cow, |
| 749 | + fmt::Error, |
| 750 | + sync::atomic::{AtomicI32, AtomicU32}, |
| 751 | + time::{SystemTime, UNIX_EPOCH}, |
| 752 | + }; |
750 | 753 |
|
751 | 754 | #[test] |
752 | 755 | fn encode_counter() { |
@@ -893,8 +896,7 @@ mod tests { |
893 | 896 |
|
894 | 897 | encode(&mut encoded, ®istry).unwrap(); |
895 | 898 |
|
896 | | - let expected = "# HELP my_prefix_my_counter_family My counter family.\n" |
897 | | - .to_owned() |
| 899 | + let expected = "# HELP my_prefix_my_counter_family My counter family.\n".to_owned() |
898 | 900 | + "# TYPE my_prefix_my_counter_family counter\n" |
899 | 901 | + "my_prefix_my_counter_family_total{my_key=\"my_value\",method=\"GET\",status=\"200\"} 1\n" |
900 | 902 | + "# EOF\n"; |
@@ -1278,4 +1280,41 @@ def parse(input): |
1278 | 1280 | .unwrap(); |
1279 | 1281 | }) |
1280 | 1282 | } |
| 1283 | + |
| 1284 | + #[test] |
| 1285 | + fn encode_omit_empty() { |
| 1286 | + let mut registry = Registry::default(); |
| 1287 | + let counter1: Family<Vec<(&'static str, &'static str)>, Counter> = Default::default(); |
| 1288 | + let counter2: Family<Vec<(&'static str, &'static str)>, Counter> = Default::default(); |
| 1289 | + let counter3: Family<Vec<(&'static str, &'static str)>, Counter> = Default::default(); |
| 1290 | + |
| 1291 | + registry.register("counter1", "First counter", counter1.clone()); |
| 1292 | + registry.register("counter2", "Second counter", counter2.clone()); |
| 1293 | + registry.register("counter3", "Third counter", counter3.clone()); |
| 1294 | + |
| 1295 | + counter1.get_or_create(&vec![("label", "value")]).inc(); |
| 1296 | + |
| 1297 | + let mut encoded = String::new(); |
| 1298 | + encode(&mut encoded, ®istry).unwrap(); |
| 1299 | + |
| 1300 | + let expected = "# HELP counter1 First counter.\n".to_owned() |
| 1301 | + + "# TYPE counter1 counter\n" |
| 1302 | + + "counter1_total{label=\"value\"} 1\n" |
| 1303 | + + "# EOF\n"; |
| 1304 | + assert_eq!(expected, encoded); |
| 1305 | + |
| 1306 | + counter2.get_or_create(&vec![("label", "value")]).inc(); |
| 1307 | + |
| 1308 | + let mut encoded = String::new(); |
| 1309 | + encode(&mut encoded, ®istry).unwrap(); |
| 1310 | + |
| 1311 | + let expected = "# HELP counter1 First counter.\n".to_owned() |
| 1312 | + + "# TYPE counter1 counter\n" |
| 1313 | + + "counter1_total{label=\"value\"} 1\n" |
| 1314 | + + "# HELP counter2 Second counter.\n" |
| 1315 | + + "# TYPE counter2 counter\n" |
| 1316 | + + "counter2_total{label=\"value\"} 1\n" |
| 1317 | + + "# EOF\n"; |
| 1318 | + assert_eq!(expected, encoded); |
| 1319 | + } |
1281 | 1320 | } |
0 commit comments