Skip to content

Commit

Permalink
feat: migrate to structured logging (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgb authored Oct 14, 2024
1 parent cbe6c1a commit e10ee75
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/main/java/io/statnett/k3a/topicterminator/TopicTerminator.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public TopicTerminator(ApplicationProperties props, KafkaAdmin kafkaAdmin) {

@Scheduled(fixedRateString = "${app.fixed-rate-string}")
public void terminateUnusedTopics() throws ExecutionException, InterruptedException {
log.info("Terminating unused topics{}", props.isDryRun() ? " in dry-run mode" : "");
log.atInfo()
.setMessage("Terminating unused topics")
.addKeyValue("dry-run", props.isDryRun())
.log();
try (AdminClient client = AdminClient.create(kafkaAdmin.getConfigurationProperties())) {
final Set<String> allTopics = client.listTopics().names().get();

Expand All @@ -47,11 +50,20 @@ public void terminateUnusedTopics() throws ExecutionException, InterruptedExcept
unusedTopics.removeAll(reservedTopic.getNames(client));
}

log.info("{} topic(s) to be deleted: ", unusedTopics.size());
log.atInfo()
.setMessage("Start deleting unused topics")
.addKeyValue("count", unusedTopics.size())
.log();
if (props.isDryRun()) {
unusedTopics.forEach(t -> log.info("Topic {} is considered unused and would be deleted in non dry-run mode", t));
unusedTopics.forEach(t -> log.atInfo()
.setMessage("NOT deleting unused topic in dry-run mode")
.addKeyValue("topic", t)
.log());
} else {
unusedTopics.forEach(t -> log.info("Delete unused topic: {}", t));
unusedTopics.forEach(t -> log.atInfo()
.setMessage("Deleting unused topic")
.addKeyValue("topic", t)
.log());
client.deleteTopics(unusedTopics);
}
}
Expand Down

0 comments on commit e10ee75

Please sign in to comment.