GH-9191: Support byte[] for headers in the MessageReceiverContext

Fixes: #9191

Not all inbound protocol handlers convert header from native representation.
Sometimes they just come in as `byte[]`.

* Fix `MessageReceiverContext` to support `byte[]` for headers used in the observation,
e.g. `B3-*`, `traceparent` etc.

(cherry picked from commit b29ff9a710)
This commit is contained in:
Artem Bilan
2024-06-07 09:59:49 -04:00
committed by Spring Builds
parent 7882609102
commit 460a4b670b

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.integration.support.management.observation;
import java.nio.charset.StandardCharsets;
import io.micrometer.observation.transport.ReceiverContext;
import org.springframework.lang.Nullable;
@@ -35,7 +37,7 @@ public class MessageReceiverContext extends ReceiverContext<Message<?>> {
private final String handlerName;
public MessageReceiverContext(Message<?> message, @Nullable String handlerName) {
super((carrier, key) -> carrier.getHeaders().get(key, String.class));
super(MessageReceiverContext::getHeader);
this.message = message;
this.handlerName = handlerName != null ? handlerName : "unknown";
}
@@ -49,4 +51,12 @@ public class MessageReceiverContext extends ReceiverContext<Message<?>> {
return this.handlerName;
}
@Nullable
private static String getHeader(Message<?> message, String key) {
Object value = message.getHeaders().get(key);
return value instanceof byte[] bytes
? new String(bytes, StandardCharsets.UTF_8)
: (value != null ? value.toString() : null);
}
}