diff --git a/README.md b/README.md index 96cb3b9b..13113123 100644 --- a/README.md +++ b/README.md @@ -60,11 +60,13 @@ and if you also require basic auth for your schema registry connection you shoul --schemaregistry.auth=username:password ``` -Finally, a default message format (e.g. to deserialize Avro messages) can optionally be configured as follows: +Finally, a default message and key format (e.g. to deserialize Avro messages or keys) can optionally be configured as follows: ``` --message.format=AVRO +--message.keyFormat=DEFAULT ``` -Valid format values are `DEFAULT`, `AVRO`, `PROTOBUF`. This can also be configured at the topic level via dropdown when viewing messages. +Valid format values are `DEFAULT`, `AVRO`, `PROTOBUF`. This can also be configured at the topic level via dropdown when viewing messages. +If key format is unspecified, message format will be used for key too. ## Configure Protobuf message type ### Option 1: Using Protobuf Descriptor diff --git a/src/main/java/kafdrop/config/MessageFormatConfiguration.java b/src/main/java/kafdrop/config/MessageFormatConfiguration.java index e43e90cf..45bdc19e 100644 --- a/src/main/java/kafdrop/config/MessageFormatConfiguration.java +++ b/src/main/java/kafdrop/config/MessageFormatConfiguration.java @@ -14,6 +14,7 @@ public class MessageFormatConfiguration { @ConfigurationProperties(prefix = "message") public static final class MessageFormatProperties { private MessageFormat format; + private MessageFormat keyFormat; @PostConstruct public void init() { @@ -21,6 +22,9 @@ public void init() { if (format == null) { format = MessageFormat.DEFAULT; } + if (keyFormat == null) { + keyFormat = format; //fallback + } } public MessageFormat getFormat() { @@ -30,5 +34,13 @@ public MessageFormat getFormat() { public void setFormat(MessageFormat format) { this.format = format; } + + public MessageFormat getKeyFormat() { + return keyFormat; + } + + public void setKeyFormat(MessageFormat keyFormat) { + this.keyFormat = keyFormat; + } } } diff --git a/src/main/java/kafdrop/controller/MessageController.java b/src/main/java/kafdrop/controller/MessageController.java index 340fc8bf..b25a7b12 100644 --- a/src/main/java/kafdrop/controller/MessageController.java +++ b/src/main/java/kafdrop/controller/MessageController.java @@ -62,17 +62,15 @@ public final class MessageController { private final MessageInspector messageInspector; private final MessageFormatProperties messageFormatProperties; - private final MessageFormatProperties keyFormatProperties; private final SchemaRegistryProperties schemaRegistryProperties; private final ProtobufDescriptorProperties protobufProperties; - public MessageController(KafkaMonitor kafkaMonitor, MessageInspector messageInspector, MessageFormatProperties messageFormatProperties, MessageFormatProperties keyFormatProperties, SchemaRegistryProperties schemaRegistryProperties, ProtobufDescriptorProperties protobufProperties) { + public MessageController(KafkaMonitor kafkaMonitor, MessageInspector messageInspector, MessageFormatProperties messageFormatProperties, SchemaRegistryProperties schemaRegistryProperties, ProtobufDescriptorProperties protobufProperties) { this.kafkaMonitor = kafkaMonitor; this.messageInspector = messageInspector; this.messageFormatProperties = messageFormatProperties; - this.keyFormatProperties = keyFormatProperties; this.schemaRegistryProperties = schemaRegistryProperties; this.protobufProperties = protobufProperties; } @@ -88,12 +86,13 @@ public String viewAllMessages(@PathVariable("name") String topicName, Model model, @RequestParam(name = "count", required = false) Integer count) { final int size = (count != null? count : 100); final MessageFormat defaultFormat = messageFormatProperties.getFormat(); - final MessageFormat defaultKeyFormat = keyFormatProperties.getFormat(); + final MessageFormat defaultKeyFormat = messageFormatProperties.getKeyFormat(); final TopicVO topic = kafkaMonitor.getTopic(topicName) .orElseThrow(() -> new TopicNotFoundException(topicName)); model.addAttribute("topic", topic); model.addAttribute("defaultFormat", defaultFormat); + model.addAttribute("defaultKeyFormat", defaultKeyFormat); model.addAttribute("messageFormats", MessageFormat.values()); model.addAttribute("keyFormats", KeyFormat.values()); model.addAttribute("descFiles", protobufProperties.getDescFilesList()); @@ -132,7 +131,7 @@ public String viewMessageForm(@PathVariable("name") String topicName, BindingResult errors, Model model) { final MessageFormat defaultFormat = messageFormatProperties.getFormat(); - final MessageFormat defaultKeyFormat = keyFormatProperties.getFormat(); + final MessageFormat defaultKeyFormat = messageFormatProperties.getKeyFormat(); if (messageForm.isEmpty()) { final PartitionOffsetInfo defaultForm = new PartitionOffsetInfo(); @@ -157,7 +156,7 @@ public String viewMessageForm(@PathVariable("name") String topicName, model.addAttribute("defaultFormat", defaultFormat); model.addAttribute("messageFormats", MessageFormat.values()); model.addAttribute("defaultKeyFormat", defaultKeyFormat); - model.addAttribute("keyFormats",KeyFormat.values()); + model.addAttribute("keyFormats", KeyFormat.values()); model.addAttribute("descFiles", protobufProperties.getDescFilesList()); model.addAttribute("isAnyProtoOpts", List.of(true, false)); diff --git a/src/main/java/kafdrop/controller/TopicController.java b/src/main/java/kafdrop/controller/TopicController.java index 09a946c0..785f5929 100644 --- a/src/main/java/kafdrop/controller/TopicController.java +++ b/src/main/java/kafdrop/controller/TopicController.java @@ -41,21 +41,19 @@ public final class TopicController { private final boolean topicDeleteEnabled; private final boolean topicCreateEnabled; private final MessageFormatConfiguration.MessageFormatProperties messageFormatProperties; - private final MessageFormatConfiguration.MessageFormatProperties keyFormatProperties; public TopicController(KafkaMonitor kafkaMonitor, - @Value("${topic.deleteEnabled:true}") Boolean topicDeleteEnabled, @Value("${topic.createEnabled:true}") Boolean topicCreateEnabled, MessageFormatConfiguration.MessageFormatProperties messageFormatProperties, MessageFormatConfiguration.MessageFormatProperties keyFormatProperties) { + @Value("${topic.deleteEnabled:true}") Boolean topicDeleteEnabled, @Value("${topic.createEnabled:true}") Boolean topicCreateEnabled, MessageFormatConfiguration.MessageFormatProperties messageFormatProperties) { this.kafkaMonitor = kafkaMonitor; this.topicDeleteEnabled = topicDeleteEnabled; this.topicCreateEnabled = topicCreateEnabled; this.messageFormatProperties = messageFormatProperties; - this.keyFormatProperties = keyFormatProperties; } @RequestMapping("/{name:.+}") public String topicDetails(@PathVariable("name") String topicName, Model model) { final MessageFormat defaultFormat = messageFormatProperties.getFormat(); - final MessageFormat defaultKeyFormat = keyFormatProperties.getFormat(); + final MessageFormat defaultKeyFormat = messageFormatProperties.getKeyFormat(); final var topic = kafkaMonitor.getTopic(topicName) .orElseThrow(() -> new TopicNotFoundException(topicName)); diff --git a/src/main/resources/templates/message-inspector.ftlh b/src/main/resources/templates/message-inspector.ftlh index 0b64821e..f06fe19f 100644 --- a/src/main/resources/templates/message-inspector.ftlh +++ b/src/main/resources/templates/message-inspector.ftlh @@ -73,7 +73,7 @@ <#assign selectedPartition=messageForm.partition!0?number> <#assign selectedFormat=messageForm.format!defaultFormat> -<#assign selectedKeyFormat=messageForm.keyFormat!defaultFormat> +<#assign selectedKeyFormat=messageForm.keyFormat!defaultKeyFormat> <#assign selectedIsAnyProtoOpt=messageForm.isAnyProto>