Skip to content

Commit 460a4b6

Browse files
artembilanspring-builds
authored andcommitted
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 b29ff9a)
1 parent 7882609 commit 460a4b6

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

spring-integration-core/src/main/java/org/springframework/integration/support/management/observation/MessageReceiverContext.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.integration.support.management.observation;
1818

19+
import java.nio.charset.StandardCharsets;
20+
1921
import io.micrometer.observation.transport.ReceiverContext;
2022

2123
import org.springframework.lang.Nullable;
@@ -35,7 +37,7 @@ public class MessageReceiverContext extends ReceiverContext<Message<?>> {
3537
private final String handlerName;
3638

3739
public MessageReceiverContext(Message<?> message, @Nullable String handlerName) {
38-
super((carrier, key) -> carrier.getHeaders().get(key, String.class));
40+
super(MessageReceiverContext::getHeader);
3941
this.message = message;
4042
this.handlerName = handlerName != null ? handlerName : "unknown";
4143
}
@@ -49,4 +51,12 @@ public String getHandlerName() {
4951
return this.handlerName;
5052
}
5153

54+
@Nullable
55+
private static String getHeader(Message<?> message, String key) {
56+
Object value = message.getHeaders().get(key);
57+
return value instanceof byte[] bytes
58+
? new String(bytes, StandardCharsets.UTF_8)
59+
: (value != null ? value.toString() : null);
60+
}
61+
5262
}

0 commit comments

Comments
 (0)