We have a java application called MonitorApp
that is running in a docker container. The purpose of this app is to perform certain tasks, push the metrics to the Pushgateway
and die.
This MonitorApp runs as a Kubernetes Cron Job
. This means after publishing matrices, it will get killed and a new instance will come up after some time.
I am using Java Pushgateway
APIs to publish the matrices.
Here is my sample code for the same:
class MonitorApp {
public void static main(String args[]) {
List<String> labelKeys = Arrays.asList("key01", "key02");
List<String> labelValues = Arrays.asList("value01", "value01");
PushGateway client = new PushGateway("localhost:9091");
CollectorRegistry registry = CollectorRegistry.defaultRegistry;
String jobName = "midm_monitor_app";
String metric = "metrin_name";
String help = "metric_help";
Counter counter = Counter.build()
.name(metric)
.help(help)
.labelNames(labelKeys.toArray(new String[0]))
.register(registry);
counter.labels(labelValues.toArray(new String[0])).inc();
client.push(registry,jobName);
}
}
The issue is that I am not able to increase the value of the Counter metric. I am not able to find a way to fetch the previous counter value so that I can increment it.
I have read that purpose of the Pushgateway
is to handle this kind of use case.
Please suggest how to should I handle this?