Add Kafka Health Indicator

See original GitHub issue

In previous versions of Spring-Boot there was an inbuild health indicator for Kafka, however somewhere along the way it was lost.

Refs:

Please add the HealthIndicator for Kafka again and add metrics as well. This can be achieved using the following code:

(includes both metrics and health)

@Configuration
public class KafkaConfig {

	@Autowired
	private KafkaAdmin admin;

	@Autowired
	private MeterRegistry meterRegistry;

	@Autowired
	private Map<String, KafkaTemplate<?, ?>> kafkaTemplates;

	@Bean
	public AdminClient kafkaAdminClient() {
		return AdminClient.create(admin.getConfig());
	}

	@SuppressWarnings("deprecation") // Can be avoided by relying on Double.NaN for non doubles.
	@PostConstruct
	private void initMetrics() {
		final String kafkaPrefix = "kafka.";
		for (Entry<String, KafkaTemplate<?, ?>> templateEntry : kafkaTemplates.entrySet()) {
			final String name = templateEntry.getKey();
			final KafkaTemplate<?, ?> kafkaTemplate = templateEntry.getValue();
			for (Metric metric : kafkaTemplate.metrics().values()) {
				final MetricName metricName = metric.metricName();
				final Builder<Metric> gaugeBuilder = Gauge
						.builder(kafkaPrefix + metricName.name(), metric, Metric::value) // <-- Here
						.description(metricName.description());
				for (Entry<String, String> tagEntry : metricName.tags().entrySet()) {
					gaugeBuilder.tag(kafkaPrefix + tagEntry.getKey(), tagEntry.getValue());
				}
				gaugeBuilder.tag("bean", name);
				gaugeBuilder.register(meterRegistry);
			}
		}
	}

	@Bean
	public HealthIndicator kafkaHealthIndicator() {
		final DescribeClusterOptions describeClusterOptions = new DescribeClusterOptions().timeoutMs(1000);
		final AdminClient adminClient = kafkaAdminClient();
		return () -> {
			final DescribeClusterResult describeCluster = adminClient.describeCluster(describeClusterOptions);
			try {
				final String clusterId = describeCluster.clusterId().get();
				final int nodeCount = describeCluster.nodes().get().size();
				return Health.up()
						.withDetail("clusterId", clusterId)
						.withDetail("nodeCount", nodeCount)
						.build();
			} catch (InterruptedException | ExecutionException e) {
				return Health.down()
						.withException(e)
						.build();
			}
		};

	}

}

Feel free to use or modify the code as you see fit.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:22 (14 by maintainers)

github_iconTop GitHub Comments

12reactions
otaviopradocommented, Feb 11, 2020

Any update about it?

3reactions
vspiliopouloscommented, Nov 16, 2018

In my opinion, Kafka health status should not be under /actuator/health, but under actuator/info, or at least there should be the option for the client to select where to place it. The reason is that microservices usually use /health endpoint status (UP/ DOWN) to scale up or down the microservice itself. Kafka broker being healthy or not is not a reason to scale up or down.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot: Kafka health indicator - java - Stack Overflow
In order to trip health indicator, retrieve data from one of the future objects otherwise indicator is UP even when Kafka is down!!!...
Read more >
SpringBoot Kafka - Health Indicator - LinkedIn
By implementing a Kafka health indicator and combining it with periodically k8s probes access, to automatically trigger a message sending, ...
Read more >
Kafka consumers health check in Spring Boot Actuator - Medium
Kafka consumers HealthIndicator​​ You can iterate this Collection and transform it into a Map of chosen consumer properties. When implementing a ...
Read more >
KafkaHealthIndicator (Spring Boot Docs 2.0.0.RC2 API)
Create a new KafkaHealthIndicator instance. Parameters: kafkaAdmin - the kafka admin: requestTimeout - the request timeout in milliseconds ...
Read more >
Monitoring Kafka Applications — Implementing Healthchecks ...
What Does Health Mean for Kafka Applications? ... For a Kafka application the health check should purposefully fail in two cases: ... Failures...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found