Skip to content

Commit

Permalink
add keyformat config option (#436)
Browse files Browse the repository at this point in the history
  • Loading branch information
fape committed Oct 12, 2022
1 parent 2f52018 commit a991b05
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/kafdrop/config/MessageFormatConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ public class MessageFormatConfiguration {
@ConfigurationProperties(prefix = "message")
public static final class MessageFormatProperties {
private MessageFormat format;
private MessageFormat keyFormat;

@PostConstruct
public void init() {
// Set a default message format if not configured.
if (format == null) {
format = MessageFormat.DEFAULT;
}
if (keyFormat == null) {
keyFormat = format; //fallback
}
}

public MessageFormat getFormat() {
Expand All @@ -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;
}
}
}
11 changes: 5 additions & 6 deletions src/main/java/kafdrop/controller/MessageController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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());
Expand Down Expand Up @@ -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();
Expand All @@ -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));

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/kafdrop/controller/TopicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/message-inspector.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -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>

<div id="partitionSizes">
Expand Down

0 comments on commit a991b05

Please sign in to comment.