micrometer-metrics / micrometer

An application observability facade for the most popular observability tools. Think SLF4J, but for observability.
https://micrometer.io
Apache License 2.0
4.45k stars 981 forks source link

Spring Boot with Kafka Template with supplied tags provider breaks spring_kafka_template metric #5191

Closed HStoyanov-augglobal closed 1 week ago

HStoyanov-augglobal commented 3 months ago

Describe the bug

In a spring project I have metrics and prometheus enabled, I am creating my own KafkaTemplate and setting a micrometerTagsProvider for it. And at that point the spring_kafka_template_... metrics all break, they stop, value at 0, the given tags are not shown. It doesn't matter what tag name or value i provide, or how many, behavior is the same. However, if i provide an empty Map, the metric will start working again.

KafkaTemplate also support adding static tags via KafkaTemplate#setMicrometerTags(Map). This always works and doesn't break the metric. Metric will show these static tags in all cases (even if micrometerTagsProvider is used).

After discussing this in the Spring Kafka project https://github.com/spring-projects/spring-kafka/issues/3275 , it was suggested that the issue has to be with micrometer itself.

In what version(s) of Spring for Apache Kafka are you seeing this issue?

Spring Kafka, see attached sample project.

To Reproduce

1) KafkaTemplate<String, String> template = new KafkaTemplate<>(producerFactory); 2) template.setMicrometerTagsProvider(producerRecord -> Map.of("topic", producerRecord.topic())); 3) spring_kafka_template_... break, value showing 0, tags not present ex:

spring_kafka_template_seconds_sum{exception="none",name="testKafkaTemplateWithTagsProvider",result="success",} 0

Expected behavior

spring_kafka_template_seconds_sum{topic=someTopic, exception="none",name="testKafkaTemplateWithTagsProvider",result="success",} 1.306612559

Sample

You can run a small project to test it yourself if you have access to your own kafka: https://github.com/HStoyanov-augglobal/kafka-metrics-springboot-bug-report/tree/main

Metrics available at http://localhost:8080/actuator/prometheus

See this code in particular where micrometerTagsProvider is specified, along with 3 cases to experiment with: https://github.com/HStoyanov-augglobal/kafka-metrics-springboot-bug-report/blob/main/src/main/java/bugreport/BugReportApplication.java#L33-L47

shakuzen commented 3 weeks ago

Sorry for the lack of response on this. https://github.com/spring-projects/spring-kafka/issues/3275#issuecomment-2213211219 I believe explains what is going on. I tried to modify your tags provider implementation to handle the case when the record is null, but this didn't help because of this code that prevents the configured tags provider from being applied when the record is null. This is a problem for the Prometheus client because it means a different set of tags will be there depending on whether the ProducerRecord is null. I suppose as a workaround you could use setMicrometerTags with default values to be used when the record is null, like Map.of("topic", "NONE"). Does that work for you?

github-actions[bot] commented 2 weeks ago

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

github-actions[bot] commented 1 week ago

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open.