Rahul Kumar
Rahul Kumar

Reputation: 109

Dynamic metric counter in spring boot - prometheus pushgateway

I have an incoming event (to a spring boot application)in json format that contains a field called "component". I need to create a metrics counter and push to prometheus pushgateway per-component. There should be individual counters for every type of component. How to achieve this?

Upvotes: 1

Views: 1181

Answers (1)

Mykola Pavluchynskyi
Mykola Pavluchynskyi

Reputation: 21

You can implement component for that. Autowire MeterRegistry

private MeterRegistry registry;

And put map of counters inside.

private Map<String, Counter> counters = new ConcurrentHashMap<>();

add method:

public void increment(String componentName){
  Counter counter = counters.get(componentName);
  if(counter == null) {
    counter = Counter.builder("your.metrics.name").tags("component", componentName).register(registry);
    counters.put(tagValue, counter);
  }
  counter.increment();
}

Upvotes: 2

Related Questions