Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix/triple trace log service without version #1281

Merged
merged 3 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -0,0 +1,45 @@
/*
* 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.bootstrap.triple;

import com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap;
import com.alipay.sofa.rpc.bootstrap.DefaultClientProxyInvoker;
import com.alipay.sofa.rpc.config.ConfigUniqueNameGenerator;

/**
* @author Even
* @date 2022/11/17 10:08 PM
*/
public class TripleClientProxyInvoker extends DefaultClientProxyInvoker {

/**
* 构造执行链
*
* @param bootstrap 调用端配置
*/
public TripleClientProxyInvoker(ConsumerBootstrap bootstrap) {
super(bootstrap);
}

@Override
protected void cacheCommonData() {
// 缓存数据
this.serviceName = ConfigUniqueNameGenerator.getUniqueName(consumerConfig);
this.serializeType = parseSerializeType(consumerConfig.getSerialization());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/
package com.alipay.sofa.rpc.bootstrap.triple;

import com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap;
import com.alipay.sofa.rpc.bootstrap.DefaultConsumerBootstrap;
import com.alipay.sofa.rpc.client.ClientProxyInvoker;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.ext.Extension;

Expand Down Expand Up @@ -48,4 +50,9 @@ public T refer() {
this.getConsumerConfig().setVirtualInterfaceId(serviceName);
return super.refer();
}

@Override
protected ClientProxyInvoker buildClientProxyInvoker(ConsumerBootstrap bootstrap) {
return new TripleClientProxyInvoker(bootstrap);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,10 @@ public static void serverReceived(SofaRequest sofaRequest, ServerServiceDefiniti
.put(TracerCompatibleConstants.RPC_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_RPC_ID));
}

String uniqueId = "";
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION)) {
RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID,
requestHeaders.get(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION));
uniqueId = requestHeaders.get(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION);
RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, uniqueId);
} else {
RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, "");
}
Expand Down Expand Up @@ -262,7 +263,7 @@ public static void serverReceived(SofaRequest sofaRequest, ServerServiceDefiniti
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
if (serverSpan != null) {
//FIXME modify the dep relation
serverSpan.setTag("service", sofaRequest.getTargetServiceUniqueName());
serverSpan.setTag("service", buildLogServiceName(sofaRequest.getInterfaceName(), uniqueId));
serverSpan.setTag("method", methodName);
// 从请求里获取ConsumerTracerFilter额外传递的信息
serverSpan.setTag("remote.app", (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
Expand All @@ -274,6 +275,11 @@ public static void serverReceived(SofaRequest sofaRequest, ServerServiceDefiniti
}
}

private static String buildLogServiceName(String interfaceName, String uniqueId) {
EvenLjj marked this conversation as resolved.
Show resolved Hide resolved
StringBuffer buffer = new StringBuffer(interfaceName).append(":1.0");
return StringUtils.isEmpty(uniqueId) ? buffer.toString() : buffer.append(":").append(uniqueId).toString();
}

/**
* 适配服务端serverSend
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* @author Even
* @date 2022/11/26 3:15 PM
*/
public class TripleTracerAdapterTest {

@Test
public void testBuilderLogServiceName() throws NoSuchMethodException, InvocationTargetException,
IllegalAccessException {
Method buildLogServiceName = TripleTracerAdapter.class.getDeclaredMethod("buildLogServiceName", String.class,
String.class);
buildLogServiceName.setAccessible(true);

String logServiceNameWithUniqueId = (String) buildLogServiceName.invoke(null, "testInterfaceId", "uniqueId");
Assert.assertEquals("testInterfaceId:1.0:uniqueId", logServiceNameWithUniqueId);

String logServiceNameWithOutUniqueId = (String) buildLogServiceName.invoke(null, "testInterfaceId", "");
Assert.assertEquals("testInterfaceId:1.0", logServiceNameWithOutUniqueId);

}

}
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.test.bootstrap.triple;

import com.alipay.sofa.rpc.api.GenericService;
import com.alipay.sofa.rpc.bootstrap.Bootstraps;
import com.alipay.sofa.rpc.bootstrap.ConsumerBootstrap;
import com.alipay.sofa.rpc.bootstrap.triple.TripleClientProxyInvoker;
import com.alipay.sofa.rpc.bootstrap.triple.TripleConsumerBootstrap;
import com.alipay.sofa.rpc.common.MockMode;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import org.junit.Assert;
import org.junit.Test;

/**
* @author Even
* @date 2022/11/22 2:32 PM
*/
public class TripleConsumerBootstrapTest {

@Test
public void test() {
ConsumerConfig<GenericService> consumerConfig = new ConsumerConfig();
consumerConfig.setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE);
consumerConfig.setInterfaceId("testInterfaceId");
consumerConfig.setUniqueId("uniqueId");
consumerConfig.setGeneric(true);
consumerConfig.setMock(true);
consumerConfig.setMockMode(MockMode.LOCAL);
ConsumerBootstrap consumerBootstrap = Bootstraps.from(consumerConfig);
consumerBootstrap.refer();
Assert.assertTrue(consumerBootstrap instanceof TripleConsumerBootstrap);

TripleClientProxyInvoker tripleClientProxyInvoker = new TripleClientProxyInvoker(consumerBootstrap);
SofaRequest sofaRequest = new SofaRequest();
tripleClientProxyInvoker.invoke(sofaRequest);
Assert.assertEquals("testInterfaceId:1.0:uniqueId", sofaRequest.getTargetServiceUniqueName());
}

}