prometheus / prometheus/client_rust

Counter and Family encode empty LabelSet differently

Open
#286 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
606
Forks
113
Avg merge
9h 7m
Merged PRs (30d)
8

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided test using prometheus_client::encoding::text::encode and compare the Counter and Family outputs shown in the issue. Trace the encoding entry points for Counter and Family, then confirm that the completed work makes empty label sets encode consistently and that the test passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
observability-sre
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.