Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix the problem of triple header losing information #1289

Merged
merged 4 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.alipay.sofa.rpc.common.RemotingConstants;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.common.TracerCompatibleConstants;
import com.alipay.sofa.rpc.common.utils.CodecUtils;
import com.alipay.sofa.rpc.common.utils.JSONUtils;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.config.ConfigUniqueNameGenerator;
Expand Down Expand Up @@ -81,6 +82,10 @@ public static void beforeSend(SofaRequest sofaRequest, ConsumerConfig consumerCo
header.put(RemotingConstants.HEAD_METHOD_NAME, sofaRequest.getMethodName());
header.put(RemotingConstants.HEAD_TARGET_SERVICE, sofaRequest.getTargetServiceUniqueName());
header.put(RemotingConstants.HEAD_TARGET_APP, sofaRequest.getTargetAppName());
Map<String, Object> requestProps = sofaRequest.getRequestProps();
if (requestProps != null) {
CodecUtils.flatCopyTo("", requestProps, header);
}
//客户端的启动
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
//获取并不弹出
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.rpc.tracer.sofatracer;

import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.server.triple.TripleHeadKeys;
import io.grpc.Metadata;
import org.junit.Assert;
import org.junit.Test;

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

import static com.alipay.sofa.rpc.common.RemotingConstants.HEAD_TARGET_SERVICE;

/**
* @author Even
* @date 2022/12/29 1:53 PM
*/
public class TripleTracerAdapterTest {

@Test
public void testBeforeSend() {
SofaRequest sofaRequest = new SofaRequest();
sofaRequest.setTargetServiceUniqueName("targetService1");
sofaRequest.addRequestProp("triple.header.key", "triple.header.value");
Map map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
sofaRequest.addRequestProp("triple.header.object", map);
sofaRequest.addRequestProp(HEAD_TARGET_SERVICE, "targetService2");
ConsumerConfig consumerConfig = new ConsumerConfig();
Metadata metadata = new Metadata();
TripleTracerAdapter.beforeSend(sofaRequest, consumerConfig, metadata);
Assert.assertEquals("targetService2", metadata.get(TripleHeadKeys.getKey(HEAD_TARGET_SERVICE)));
Assert.assertEquals("triple.header.value", metadata.get(TripleHeadKeys.getKey("triple.header.key")));
Assert.assertEquals("value1", metadata.get(TripleHeadKeys.getKey("triple.header.object.key1")));
Assert.assertEquals("value2", metadata.get(TripleHeadKeys.getKey("triple.header.object.key2")));
}

}