Skip to content

Counter and Family encode empty LabelSet differently #286

Open

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

Activity

  1. howardjohn commented on Sep 22, 2025

    @howardjohn
    ContributorAuthor

    The difference is the family_labels is Some(EmptyLabels) here for Family (

    && self.family_labels.is_none()
    ) and None for Counter

  2. howardjohn commented on Sep 22, 2025

    @howardjohn
    ContributorAuthor

    Note (AFAIK) both are valid outputs. It just seems odd they are inconsistent. The reason I am looking into this is allegedly DataDog parses these differently, but I think if it it does that is a DD bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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