Skip to content
Open
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
6 changes: 6 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,11 @@
<artifactId>sofa-rpc-tracer-opentracing-triple</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-profile-jfr</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-metrics-micrometer</artifactId>
Expand Down Expand Up @@ -574,6 +579,7 @@
<include>com.alipay.sofa:sofa-rpc-tracer-opentracing</include>
<include>com.alipay.sofa:sofa-rpc-tracer-opentracing-resteasy</include>
<include>com.alipay.sofa:sofa-rpc-tracer-opentracing-triple</include>
<include>com.alipay.sofa:sofa-rpc-profile-jfr</include>
<include>com.alipay.sofa:sofa-rpc-config-apollo</include>
<include>com.alipay.sofa:sofa-rpc-config-zk</include>
<include>com.alipay.sofa:sofa-rpc-config-nacos</include>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.message.ResponseFuture;
import com.alipay.sofa.rpc.profile.Profiles;
import com.alipay.sofa.rpc.transport.ClientTransport;

import java.lang.reflect.Method;
Expand Down Expand Up @@ -406,7 +407,9 @@ protected ProviderInfo select(SofaRequest message, List<ProviderInfo> invokedPro
List<ProviderInfo> providerInfos = routerChain.route(message, null);
RpcInternalContext context = RpcInternalContext.peekContext();
RpcInvokeContext rpcInvokeContext = RpcInvokeContext.getContext();
rpcInvokeContext.put(RpcConstants.INTERNAL_KEY_CLIENT_ROUTER_TIME_NANO, System.nanoTime()-routerStartTime);
long routerTime = System.nanoTime() - routerStartTime;
rpcInvokeContext.put(RpcConstants.INTERNAL_KEY_CLIENT_ROUTER_TIME_NANO, routerTime);
Profiles.recordPhase(context, RpcConstants.INTERNAL_KEY_CLIENT_ROUTER_TIME_NANO, routerTime);
//保存一下原始地址,为了打印
List<ProviderInfo> originalProviderInfos;

Expand Down Expand Up @@ -459,7 +462,10 @@ protected ProviderInfo select(SofaRequest message, List<ProviderInfo> invokedPro
// 再进行负载均衡筛选
long loadBalanceStartTime = System.nanoTime();
providerInfo = loadBalancer.select(message, providerInfos);
rpcInvokeContext.put(RpcConstants.INTERNAL_KEY_CLIENT_BALANCER_TIME_NANO, System.nanoTime()-loadBalanceStartTime);
long loadBalancerTime = System.nanoTime() - loadBalanceStartTime;
rpcInvokeContext.put(RpcConstants.INTERNAL_KEY_CLIENT_BALANCER_TIME_NANO, loadBalancerTime);
Profiles.recordPhase(context, RpcConstants.INTERNAL_KEY_CLIENT_BALANCER_TIME_NANO,
loadBalancerTime);

ClientTransport transport = selectByProvider(message, providerInfo);
if (transport != null) {
Expand Down Expand Up @@ -578,16 +584,19 @@ private void calculateConsumerFilterTime() {
Long invokerEndTime = (Long) RpcInvokeContext.getContext().get(
RpcConstants.INTERNAL_KEY_CONSUMER_INVOKE_END_TIME_NANO);
if (filterStartTime != null && filterEndTime != null && invokerStartTime != null && invokerEndTime != null) {
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_CLIENT_FILTER_TIME_NANO,
filterEndTime - filterStartTime - (invokerEndTime - invokerStartTime));
long filterTime = filterEndTime - filterStartTime - (invokerEndTime - invokerStartTime);
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_CLIENT_FILTER_TIME_NANO, filterTime);
Profiles.recordPhase(RpcConstants.INTERNAL_KEY_CLIENT_FILTER_TIME_NANO, filterTime);
}
}

@Override
public SofaResponse sendMsg(ProviderInfo providerInfo, SofaRequest request) throws SofaRpcException {
long start = System.nanoTime();
ClientTransport clientTransport = connectionHolder.getAvailableClientTransport(providerInfo);
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME_NANO, System.nanoTime() - start);
long connectionTime = System.nanoTime() - start;
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME_NANO, connectionTime);
Profiles.recordPhase(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME_NANO, connectionTime);
if (clientTransport != null && clientTransport.isAvailable()) {
return doSendMsg(providerInfo, clientTransport, request);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public class RpcOptions {
* 默认Tracer实现
*/
public static final String DEFAULT_TRACER = "default.tracer";
/**
* 默认Profile实现
*/
public static final String DEFAULT_PROFILE = "default.profile";
/**
* 默认filter实现
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.alipay.sofa.rpc.log.LogCodes;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.profile.Profiles;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -138,8 +139,9 @@ public SofaResponse invoke(SofaRequest request) throws SofaRpcException {
RpcInternalContext.getContext().setAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE,
endTime - startTime);
}
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE_NANO,
System.nanoTime() - bizStartTime);
long businessTime = System.nanoTime() - bizStartTime;
RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE_NANO, businessTime);
Profiles.recordPhase(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE_NANO, businessTime);

RpcInvokeContext.getContext().put(RpcConstants.INTERNAL_KEY_PROVIDER_INVOKE_END_TIME_NANO,
System.nanoTime());
Expand Down
111 changes: 111 additions & 0 deletions core/api/src/main/java/com/alipay/sofa/rpc/profile/Profile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.profile;

import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.ext.Extensible;

/**
* RPC调用过程性能数据采集SPI。实现必须快速返回,不得影响RPC主链路。
*
* @author <a href="mailto:13622993145@163.com">weilhaung</a>
* @since 5.14.4
*/
@Extensible
public abstract class Profile {

/**
* 当前是否需要采集数据。
*
* @return 是否启用
*/
public abstract boolean isEnabled();

/**
* 客户端开始一次RPC调用。
*
* @param request 调用请求
*/
public abstract void startRpc(SofaRequest request);

/**
* 客户端发送请求前。
*
* @param request 调用请求
*/
public abstract void clientBeforeSend(SofaRequest request);

/**
* 客户端发送请求后。
*
* @param request 调用请求
*/
public abstract void clientAfterSend(SofaRequest request);

/**
* 客户端收到响应或异常。重试场景可能调用多次。
*
* @param request 调用请求
* @param response 调用响应
* @param throwable 调用异常
*/
public abstract void clientReceived(SofaRequest request, SofaResponse response, Throwable throwable);

/**
* 客户端结束一次RPC调用。
*
* @param request 调用请求
* @param response 调用响应
* @param throwable 调用异常
*/
public abstract void clientEnd(SofaRequest request, SofaResponse response, Throwable throwable);

/**
* 服务端收到请求。
*
* @param request 调用请求
*/
public abstract void serverReceived(SofaRequest request);

/**
* 服务端发送响应或异常。
*
* @param request 调用请求
* @param response 调用响应
* @param throwable 调用异常
*/
public abstract void serverSend(SofaRequest request, SofaResponse response, Throwable throwable);

/**
* 记录当前RPC调用的一个阶段耗时。阶段数据必须绑定到传入的调用级内部上下文,
* 不能依赖调用线程的共享上下文。
*
* @param context RPC调用级内部上下文
* @param phase 阶段标识
* @param elapsedNanos 阶段耗时,单位纳秒
*/
public void recordPhase(RpcInternalContext context, String phase, long elapsedNanos) {
// 默认不记录,具体Profile实现按需处理。
}

/**
* 调用结束时检查并清理未完成的采集状态。
*/
public abstract void checkState();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.profile;

/**
* Profile公共常量。
*
* @author <a href="mailto:13622993145@163.com">weilhaung</a>
* @since 5.14.4
*/
public final class ProfileConstants {

/**
* 客户端与服务端Profile记录的关联标识。
*/
public static final String PROFILE_ID_KEY = "sofa-rpc-profile-id";

private ProfileConstants() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.profile;

import com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException;
import com.alipay.sofa.rpc.ext.ExtensionClass;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.log.LogCodes;

/**
* Profile扩展工厂。
*
* @author <a href="mailto:13622993145@163.com">weilhaung</a>
* @since 5.14.4
*/
public final class ProfileFactory {

private ProfileFactory() {
}

/**
* 根据扩展别名获取Profile实例。
*
* @param profileName 扩展别名
* @return Profile实例
*/
public static synchronized Profile getProfile(String profileName) {
try {
ExtensionClass<Profile> ext = ExtensionLoaderFactory.getExtensionLoader(Profile.class)
.getExtensionClass(profileName);
if (ext == null) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT,
Profile.class.getName(), profileName));
}
return ext.getExtInstance();
} catch (SofaRpcRuntimeException e) {
throw e;
} catch (Throwable e) {
throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT,
Profile.class.getName(), profileName), e);
}
}
}
Loading
Loading