Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some Kafka headers are not displayed if type is not String #1342

Closed
gschmutz opened this issue Jan 8, 2023 · 7 comments
Closed

Some Kafka headers are not displayed if type is not String #1342

gschmutz opened this issue Jan 8, 2023 · 7 comments
Labels
enhancement New feature or request good first issue Good for newcomers topic data Kafka Topic data

Comments

@gschmutz
Copy link
Contributor

gschmutz commented Jan 8, 2023

Hi

First, thanks a lot for this great tool!

When using the Dead-Letter topic support of Spring Kafka, then a message which is sent to the dead-letter topic includes the original-partition and the original-offset of the message as Kafka Headers. Partition is of type Integer and Offset of type Long and therefore they are not visible when showing the header details. The same is true for the original timestamp.

akhq-with-header

@tchiotludo
Copy link
Owner

Hi @gschmutz,

Thanks for the report, but this one is really complicated it seems.
For now, AKHQ don't have any mapping of type and you failed on the same issues than for the body, but for the headers.
Except manual configuration for the topic, I don't have any clues to handle that.
If you have an idea, it's welcome

@tchiotludo tchiotludo added enhancement New feature or request topic data Kafka Topic data labels Jan 8, 2023
@gschmutz
Copy link
Contributor Author

gschmutz commented Jan 9, 2023

@tchiotludo when creating the issue yesterday, I understood the difficulty for both key and value, as their types are provided by the Kafka producer client.

To be honest, until now (after checking the javadoc) I realized that the header value is a byte[] and not an Object. Which makes sense, so that it is Interoperable. That of course makes it impossible to handle the value without knowing how it has been serialized.

First I thought you could display a special value instead of empty but even that will not work because it was only the case because both offset and partition were single digits. I just found it confusing because first I thought the DLQ message did not contain values for these two headers.

Lessons learnt I guess is, that if using headers of type other than String makes it really hard for a consumer to handle in a generic way. Now I understand why all the visualization tools I have tried have the same problem with headers.

@tchiotludo
Copy link
Owner

👍 exactly!
Keep it open because AKHQ should not be filtered, and maybe displayed as special representation to understand unserialized case.

@tchiotludo tchiotludo added the good first issue Good for newcomers label Jan 9, 2023
@tchiotludo tchiotludo changed the title Kafka headers are not displayed correctly if data type is not String Some Kafka headers are not displayed if type is not String Jan 9, 2023
@gschmutz
Copy link
Contributor Author

gschmutz commented Jan 9, 2023

Assuming that the Strings are encoded iin UTF-8, I found this java code here to detect it: https://stackoverflow.com/questions/1193200/how-can-i-check-whether-a-byte-array-contains-a-unicode-string-in-java.

    static boolean looksLikeUTF8(byte[] utf8) throws UnsupportedEncodingException
    {
        Pattern p = Pattern.compile("\\A(\n" +
                "  [\\x09\\x0A\\x0D\\x20-\\x7E]             # ASCII\\n" +
                "| [\\xC2-\\xDF][\\x80-\\xBF]               # non-overlong 2-byte\n" +
                "|  \\xE0[\\xA0-\\xBF][\\x80-\\xBF]         # excluding overlongs\n" +
                "| [\\xE1-\\xEC\\xEE\\xEF][\\x80-\\xBF]{2}  # straight 3-byte\n" +
                "|  \\xED[\\x80-\\x9F][\\x80-\\xBF]         # excluding surrogates\n" +
                "|  \\xF0[\\x90-\\xBF][\\x80-\\xBF]{2}      # planes 1-3\n" +
                "| [\\xF1-\\xF3][\\x80-\\xBF]{3}            # planes 4-15\n" +
                "|  \\xF4[\\x80-\\x8F][\\x80-\\xBF]{2}      # plane 16\n" +
                ")*\\z", Pattern.COMMENTS);

        String phonyString = new String(utf8, "ISO-8859-1");
        return p.matcher(phonyString).matches();
    }

I have tested it and it works fine detecting Strings (True) and Long/Integer (False). I have also tested it as part of AKHQ and it looks good at least for my test.

akhq-with-header-nostring

Might want to do some more testing, but it looks promising.

I can also create a pull-request if you like, question is what kind of value would you want if it can not be converted to if it is not text?

@gschmutz
Copy link
Contributor Author

gschmutz commented Jan 9, 2023

BTW: I have also tested various other options such as StringUtils from commons-codec and Apache Tika but with less success.

@tchiotludo
Copy link
Owner

Everything that will allow us to better representation without hard configuration is better, so for sure, PR will be accepted ! 🚀

@gschmutz
Copy link
Contributor Author

Have created the PR, header values of type Long, Short and Integer are now handled and displayed correctly.

akhq-with-pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers topic data Kafka Topic data
Projects
Status: Done
Development

No branches or pull requests

2 participants