Skip to content

Commit 27e9c41

Browse files
committed
Add test & changelog
Signed-off-by: Jean-Baptiste Skutnik <[email protected]>
1 parent 40393b1 commit 27e9c41

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## Unreleased
8+
9+
- Filter out empty metric families, to match the go client.
10+
711
## [0.24.0]
812

913
### Added

src/encoding/text.rs

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@
3737
//! assert_eq!(expected_msg, buffer);
3838
//! ```
3939
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+
};
4445

45-
use std::borrow::Cow;
46-
use std::collections::HashMap;
47-
use std::fmt::Write;
46+
use std::{borrow::Cow, collections::HashMap, fmt::Write};
4847

4948
/// Encode both the metrics registered with the provided [`Registry`] and the
5049
/// EOF marker into the provided [`Write`]r using the OpenMetrics text format.
@@ -736,17 +735,21 @@ impl std::fmt::Write for LabelValueEncoder<'_> {
736735
#[cfg(test)]
737736
mod tests {
738737
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+
};
745746
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+
};
750753

751754
#[test]
752755
fn encode_counter() {
@@ -893,8 +896,7 @@ mod tests {
893896

894897
encode(&mut encoded, &registry).unwrap();
895898

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()
898900
+ "# TYPE my_prefix_my_counter_family counter\n"
899901
+ "my_prefix_my_counter_family_total{my_key=\"my_value\",method=\"GET\",status=\"200\"} 1\n"
900902
+ "# EOF\n";
@@ -1278,4 +1280,41 @@ def parse(input):
12781280
.unwrap();
12791281
})
12801282
}
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, &registry).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, &registry).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+
}
12811320
}

0 commit comments

Comments
 (0)