首页 > 解决方案 > How to update label value in promethues java implementation

问题描述

I have to update the label values in prometheus histogram where one of the label is the response status and that can only be updated once I get the response. Below is my Implementation of the histogram metric. How can I update the labels

Histogram

public class CustomHistogram implements CountryHistogram{

    private final Histogram histogram;

    CustomHistogram(SampleRegistry registry) {
        histogram = Histogram.build().name("custom_metric").help("Sample metrics")
                .labelNames("name", "status").register(registry);
    }


    @Override
    public Histogram.Timer startTimer() {
        return histogram.labels("name","dummy").startTimer();
    }

    @Override
    public void observeDuration(Histogram.Timer timer) {
        //need to update label here
        timer.observeDuration();
    }
}

Usage

public void executeCall() {

  CustomHistogram hist = MetricFactory.getHistogram();
  Histogram.Timer timer = hist.startTimer();

  try{
  //do external call here
  } finally {
      hist.observeDuration(timer);
  }
}

How can I update the status label in the observeDuration method?

标签: prometheus-java

解决方案


推荐阅读