Skip to content

Counter and Family encode empty LabelSet differently #286

@howardjohn

Description

@howardjohn

If you create a Family, the empty label set will encode like metric{} 2. If you use a raw Counter, it will encode like counter 2.

This seems like an unintended difference

use std::fmt::Error;
use prometheus_client::encoding::EncodeLabelSet;
use prometheus_client::encoding::text::encode;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::family::Family;
use prometheus_client::registry::Registry;

#[test]
fn test_counter_vs_family() {
    let mut registry = Registry::default();
    let family: Family::<EmptyLabels, Counter> = Family::default();
    let ctr: Counter = Counter::default();
    registry.register("family", "", family.clone());
    registry.register("counter", "", ctr.clone());

    #[derive(Eq, PartialEq, Hash, Debug, Clone)]
    struct EmptyLabels {}

    impl EncodeLabelSet for EmptyLabels {
        fn encode(&self, _encoder: &mut prometheus_client::encoding::LabelSetEncoder) -> Result<(), Error> {
            Ok(())
        }
    }

    family.get_or_create(&EmptyLabels {}).inc();
    ctr.inc();

    let mut encoded = String::new();

    encode(&mut encoded, &registry).unwrap();
    println!("{}", encoded);
}
# HELP family .
# TYPE family counter
family_total{} 1
# HELP counter .
# TYPE counter counter
counter_total 1
# EOF

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions