What is the correct way to read all gRPC headers and trailers in Java, including ":status"? - Stack Overflow

I'm trying to implement an interceptor for logging in gRPC servers and clients.In the interceptor

I'm trying to implement an interceptor for logging in gRPC servers and clients.
In the interceptor I'd like to log all the headers and trailers.
I tried using the following code to read all the key-values from the Metadata and convert them to a string representation to print them to the screen:

    private String metadataToString(final Metadata metadata) {
        if (metadata == null) {
            return "";
        }

        var sb = new StringBuilder();

        for (String key : metadata.keys())
        {
            var metadataKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);

            sb.append(
                String.format(
                    "%s: %s\n",
                    key,
                    metadata.get(metadataKey)));
        }

        return sb.toString();
    }

I tested it in a client interceptor to log the headers and the trailers. This works fine if the gRPC request is successful.
But if the request fails, then apparently the framework automatically adds a trailer with the key :status. And in that case, the code above fails with the following exception:

java.lang.IllegalArgumentException: Invalid character ':' in key name ':status'
        at com.googlemon.base.Preconditions.checkArgument(Preconditions.java:273)
        at io.grpc.Metadata$Key.validateName(Metadata.java:754)
        at io.grpc.Metadata$Key.<init>(Metadata.java:762)
        at io.grpc.Metadata$Key.<init>(Metadata.java:671)
        at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:971)
        at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:966)
        at io.grpc.Metadata$Key.of(Metadata.java:708)
        at io.grpc.Metadata$Key.of(Metadata.java:704)
        at com.travix.supplier.ancillary.service.bundles.interceptors.logging.client.GrpcLoggingClientCallListener.metadataToString(GrpcLoggingClientCallListener.java:81)

What is the reason for this, am I doing something wrong? It seems surprising that the framework would include a key with an illegal value, so I suspect that I implemented it incorrectly.
With some googling I found this piece of code: .java#L66. I tried copying this HTTP_STATUS_LINE_MARSHALLER into my library and create the key with that, but I still got the same exception.
If anyone can tell what I'm doing wrong, and what would be the correct way to implement this, that'd be appreciated!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745273796a4619910.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信