I am using Spring micrometer in my application to flow metrics to Prometheus. I have a requirement where I need to send an unique id and error code inside the labels of @Counted annotation. Is it possible that it ignores the id and count only based on error code ?
My code snipped -
@Counted(name = "my_metric",
labels = {"error_code:$0", "unique_id:$1"})
What I am getting through /prometheus endpoint-
my_metric_total{error_code="test-1",unique_id="id-1"} 1.0
my_metric_total{error_code="test-1",unique_id="id-2"} 1.0
What I want through /prometheus endpoint-
my_metric_total{error_code="test-1",unique_id="id-1"} 1.0
my_metric_total{error_code="test-1",unique_id="id-2"} 2.0
The increment should happen only based on the error_code. Is this possible using @Counted annotation ?
It is not possible because a meter is "uniquely identified by its name and dimensions" meaning that the metric name and tag makes up a separate instance of the meter and hence it will have two entries when you query the prometheus endpoint.
Depending on what you are trying to achieve, you could make the aggregation of the different metrics elsewhere (e.g. Grafana).
A an example, using sum
from PromQL sum by (error_code)...
which will give you the aggregated value.