1

My question is similar to following question. I want to check health for Kstream application which is coded through functional approach. Spring Actuator + Kafka Streams - Add kafka stream status to health check endpoint

In the above link answers are given in terms of Autowiring Kafka Streams. I am not able to autowire it as it gives following error.

Field kafkaStreams in <package_name>.metrics.KafkaStreamsHealthIndicator required a bean of type 'org.apache.kafka.streams.KafkaStreams' that could not be found.

I tried adding following class as the link in the above explains but it gives error for Kafka Stream autowiring

`import org.apache.kafka.streams.KafkaStreams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.kafka.annotation.EnableKafkaStreams;
import org.springframework.stereotype.Component;

//Note that class name prefix before `HealthIndicator` will be camel-cased
//and used as a health component name, `kafkaStreams` here
@Component
@EnableKafkaStreams
public class KafkaStreamsHealthIndicator implements HealthIndicator {
//    StreamsBuilder streamsBuilder = new StreamsBuilder();

    //if you have multiple instances, inject as Map<String, KafkaStreams>
    //Spring will map KafkaStreams instances by bean names present in context
    //so you can provide status details for each stream by name
    @Autowired
    private KafkaStreams kafkaStreams;


    @Override
    public Health health() {
        KafkaStreams.State kafkaStreamsState = kafkaStreams.state();

        // CREATED, RUNNING or REBALANCING
        if (kafkaStreamsState == KafkaStreams.State.CREATED || kafkaStreamsState.isRunningOrRebalancing()) {
            //set details if you need one
            return Health.up().build();
        }

        // ERROR, NOT_RUNNING, PENDING_SHUTDOWN,
        return Health.down().withDetail("state", kafkaStreamsState.name()).build();
    }
}`
Kedar Jog
  • 11
  • 3

1 Answers1

0

KafkaStreams is not a bean defined within the spring-kafka module instead you should use StreamsBuilderFactoryBean which exposes KafkaStreams once they are initialized and provides other spring and kafka related lifecycle methods.

You can also hook into the kafka streams state listener to alter the health state from there.