R0b0t0
R0b0t0

Reputation: 390

initialize counter value to 0 in prometheus

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

Upvotes: 3

Views: 6790

Answers (1)

R0b0t0
R0b0t0

Reputation: 390

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

Upvotes: 3

Related Questions