forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add readiness health check for Kafka Streams
Fixes quarkusio#4793
- Loading branch information
Showing
10 changed files
with
328 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...oyment/src/main/java/io/quarkus/kafka/streams/deployment/KafkaStreamsBuildTimeConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.quarkus.kafka.streams.deployment; | ||
|
||
import io.quarkus.runtime.annotations.ConfigItem; | ||
import io.quarkus.runtime.annotations.ConfigPhase; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
|
||
@ConfigRoot(name = "kafka-streams", phase = ConfigPhase.BUILD_TIME) | ||
public class KafkaStreamsBuildTimeConfig { | ||
|
||
/** | ||
* Whether or not a health check is published in case the smallrye-health extension is present (defaults to true). | ||
*/ | ||
@ConfigItem(name = "health.enabled", defaultValue = "true") | ||
public boolean healthEnabled; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...e/src/main/java/io/quarkus/kafka/streams/runtime/health/KafkaStreamsStateHealthCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.quarkus.kafka.streams.runtime.health; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.apache.kafka.streams.KafkaStreams; | ||
import org.eclipse.microprofile.health.HealthCheck; | ||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
import org.eclipse.microprofile.health.HealthCheckResponseBuilder; | ||
import org.eclipse.microprofile.health.Liveness; | ||
|
||
import io.quarkus.kafka.streams.runtime.KafkaStreamsTopologyManager; | ||
|
||
@Liveness | ||
@ApplicationScoped | ||
public class KafkaStreamsStateHealthCheck implements HealthCheck { | ||
|
||
@Inject | ||
private KafkaStreamsTopologyManager manager; | ||
|
||
@Override | ||
public HealthCheckResponse call() { | ||
HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("Kafka Streams state health check"); | ||
try { | ||
KafkaStreams.State state = manager.getStreams().state(); | ||
responseBuilder.state(state.isRunning()) | ||
.withData("state", state.name()); | ||
} catch (Exception e) { | ||
responseBuilder.down().withData("technical_error", e.getMessage()); | ||
} | ||
return responseBuilder.build(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
.../src/main/java/io/quarkus/kafka/streams/runtime/health/KafkaStreamsTopicsHealthCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.quarkus.kafka.streams.runtime.health; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.eclipse.microprofile.health.HealthCheck; | ||
import org.eclipse.microprofile.health.HealthCheckResponse; | ||
import org.eclipse.microprofile.health.HealthCheckResponseBuilder; | ||
import org.eclipse.microprofile.health.Readiness; | ||
import org.jboss.logging.Logger; | ||
|
||
import io.quarkus.kafka.streams.runtime.KafkaStreamsTopologyManager; | ||
|
||
@Readiness | ||
@ApplicationScoped | ||
public class KafkaStreamsTopicsHealthCheck implements HealthCheck { | ||
|
||
private static final Logger LOGGER = Logger.getLogger(KafkaStreamsTopicsHealthCheck.class.getName()); | ||
|
||
@ConfigProperty(name = "quarkus.kafka-streams.topics") | ||
public List<String> topics; | ||
|
||
// @ConfigProperty(name = "quarkus.kafka-streams.bootstrap-servers") | ||
// public List<String> bootstrapServers; | ||
|
||
@Inject | ||
private KafkaStreamsTopologyManager manager; | ||
|
||
// private String commaSeparatedBootstrapServersConfig; | ||
private List<String> trimmedTopics; | ||
|
||
@PostConstruct | ||
public void init() { | ||
// commaSeparatedBootstrapServersConfig = String.join(",", bootstrapServers); | ||
trimmedTopics = topics.stream().map(String::trim).collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public HealthCheckResponse call() { | ||
HealthCheckResponseBuilder builder = HealthCheckResponse.named("Kafka Streams topics health check").up(); | ||
|
||
try { | ||
Set<String> missingTopics = manager.getMissingTopics(trimmedTopics); | ||
List<String> availableTopics = new ArrayList<>(trimmedTopics); | ||
availableTopics.removeAll(missingTopics); | ||
|
||
if (!availableTopics.isEmpty()) { | ||
builder.withData("available_topics", String.join(",", availableTopics)); | ||
} | ||
if (!missingTopics.isEmpty()) { | ||
builder.down().withData("missing_topics", String.join(",", missingTopics)); | ||
} | ||
} catch (InterruptedException e) { | ||
LOGGER.error("error when retrieving missing topics", e); | ||
builder.down().withData("technical_error", e.getMessage()); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.