Skip to content

Commit

Permalink
fix baggage do not pick up to RpcInvokeContext problem (#1402)
Browse files Browse the repository at this point in the history
Co-authored-by: liujianjun.ljj <[email protected]>
  • Loading branch information
EvenLjj and liujianjun.ljj authored May 10, 2024
1 parent ddd63ab commit cd8bb17
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ protected void parseRequestHeader(Map<String, String> headerMap, Object sofaRequ
if (sofaRequest instanceof SofaRequest) {
// 处理 tracer
parseRequestHeader(RemotingConstants.RPC_TRACE_NAME, headerMap, (SofaRequest) sofaRequest);
if (RpcInvokeContext.isBaggageEnable()) {
parseRequestHeader(RemotingConstants.RPC_REQUEST_BAGGAGE, headerMap, (SofaRequest) sofaRequest);
}
Map<String, Object> requestProps = ((SofaRequest) sofaRequest).getRequestProps();
if (requestProps == null) {
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alipay.sofa.rpc.core.request.SofaRequest;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -92,16 +93,24 @@ public void testParseRequestHeader(){
headerMap.put("testKey1","testValue1");
headerMap.put("rpc_trace_context.sofaTraceId", "traceId");
headerMap.put("rpc_trace_context.sofaRpcId", "rpcId");
headerMap.put("rpc_req_baggage.testBaggageKey1", "testBaggageValue1");
headerMap.put("rpc_req_baggage.testBaggageKey2", "testBaggageValue2");
SofaRpcSerialization sofaRpcSerialization = new SofaRpcSerialization();
SofaRequest sofaRequest = new SofaRequest();
sofaRequest.addRequestProp("testKey1", "testValue11");
sofaRequest.addRequestProp("testKey2", "testValue2");
sofaRpcSerialization.parseRequestHeader(headerMap, sofaRequest);
sofaRpcSerialization.parseRequestHeader(headerMap, sofaRequest);
Assert.assertEquals("testValue1", sofaRequest.getRequestProp("testKey1"));
Assert.assertEquals("testValue2", sofaRequest.getRequestProp("testKey2"));
Object traceMap = sofaRequest.getRequestProp(RemotingConstants.RPC_TRACE_NAME);
Assert.assertTrue(traceMap instanceof Map);
Assert.assertEquals("traceId",((Map)traceMap).get("sofaTraceId"));
Assert.assertEquals("rpcId",((Map)traceMap).get("sofaRpcId"));
Object baggageMap = sofaRequest.getRequestProp(RemotingConstants.RPC_REQUEST_BAGGAGE);
Assert.assertTrue(baggageMap instanceof Map);
Assert.assertEquals("testBaggageValue1", ((Map) baggageMap).get("testBaggageKey1"));
Assert.assertEquals("testBaggageValue2", ((Map) baggageMap).get("testBaggageKey2"));

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"rpc.config.order": 999,
"logger.impl": "com.alipay.sofa.rpc.log.SLF4JLoggerImpl"
"logger.impl": "com.alipay.sofa.rpc.log.SLF4JLoggerImpl",
"invoke.baggage.enable": true
}

0 comments on commit cd8bb17

Please sign in to comment.