3

I'm using a simple client Prometheus version 0.6.0

I have a set of counters that are defined this way

private static final Counter CD_PRODUCT_SUCCESS_CREATED = new  Counter.Builder()
            .name("cd_Product_success_created")
            .help("product success created.")
            .labelNames("podname")
            .create()
            .register(CollectorRegistry.defaultRegistry);
....
 public List<MetricFamilySamples> collect() {
        List<MetricFamilySamples> samples = new ArrayList<>();
        samples.addAll(CD_PRODUCT_SUCCESS_CREATED.collect());
        ....
        return samples;

public void addCdProductSuccessCreated() {
        CD_PRODUCT_SUCCESS_CREATED.labels(podName).inc();
    }

I inject this class in places where I need it and then call the methods like addCdProductSuccessCreated() yet for the metrics that do not have values they look empty when I scrape them ex:

# HELP cd_Product_success_created
# TYPE cd_Product_success_created counter

my question is how can I initilize the counter with the value 0 so that it appears even tho the inc method is not called

R0b0t0
  • 390
  • 6
  • 20
  • See [Existential issues with metrics](https://www.robustperception.io/existential-issues-with-metrics). You should able to initialize to zero by proceeding as if you were `inc`'ing or `set`'ting the metric value **but** exclude the `inc` or `set` – DazWilkin Nov 09 '21 at 00:15
  • checked the article you posted I couldn't quite get the answer should I add a static block static { counter.labels("podname"); ... } like this ? – R0b0t0 Nov 09 '21 at 01:11
  • I'm not a Java developer. The example appears to match what you're doing and that should give you a zeroed `counter` so that the metric appears in the exporter and you can include it in PromQL. Have you tried it? – DazWilkin Nov 09 '21 at 01:23

1 Answers1

3

found the answer I did add a block like this

static { CD_PRODUCT_SUCCESS_CREATED .labels("podname"); ... }

for all the metrics and now I could see the 0.0 value for the metrics

R0b0t0
  • 390
  • 6
  • 20