use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class RestTracerAdapter method serverSend.
/**
* 适配服务端serverSend
*/
public static void serverSend(NettyHttpResponse response, Throwable throwable) {
try {
SofaRequest sofaRequest = new SofaRequest();
SofaResponse sofaResponse = new SofaResponse();
if (response == null) {
sofaResponse.setErrorMsg("rest path ends with /favicon.ico");
} else if (throwable != null) {
if (response.getStatus() == 500) {
sofaResponse.setAppResponse(throwable);
} else {
sofaResponse.setErrorMsg(throwable.getMessage());
}
Object method = RpcInternalContext.getContext().getAttachment(METHOD_TYPE_STRING);
if (method != null) {
Class[] parameterTypes = ((Method) method).getParameterTypes();
String[] methodTypeString = new String[parameterTypes.length];
for (int i = 0; i < methodTypeString.length; i++) {
methodTypeString[i] = (parameterTypes[i].getName());
}
sofaRequest.setMethodArgSigs(methodTypeString);
}
}
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
RpcInternalContext context = RpcInternalContext.getContext();
if (serverSpan != null) {
serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
}
RestBaggageItemsHandler.encodeBaggageItemsToResponse(response, sofaResponse);
Tracers.serverSend(sofaRequest, sofaResponse, null);
} catch (Throwable t) {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("the process of rest tracer server send occur error ", t);
}
}
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class RpcReferenceContextFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
ConsumerConfig config = (ConsumerConfig) invoker.getConfig();
RpcReferenceContext referenceCtx = new RpcReferenceContext();
referenceCtx.setGeneric(config.isGeneric());
referenceCtx.setInterfaceName(config.getInterfaceId());
referenceCtx.setUniqueId(config.getUniqueId());
referenceCtx.setServiceName(request.getTargetServiceUniqueName());
referenceCtx.setMethodName(request.getMethodName());
RpcInternalContext context = RpcInternalContext.getContext();
ProviderInfo providerInfo = context.getProviderInfo();
if (providerInfo != null) {
referenceCtx.setTargetAppName(providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
referenceCtx.setTargetUrl(providerInfo.getHost() + ":" + providerInfo.getPort());
}
referenceCtx.setProtocol(config.getProtocol());
referenceCtx.setInvokeType(request.getInvokeType());
referenceCtx.setRouteRecord((String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
RpcInvokeContext.getContext().put(RemotingConstants.INVOKE_CTX_RPC_REF_CTX, referenceCtx);
SofaResponse response = invoker.invoke(request);
// 调用后
InetSocketAddress local = context.getLocalAddress();
if (local != null) {
referenceCtx.setClientIP(NetUtils.toIpString(local));
referenceCtx.setClientPort(local.getPort());
}
Long ct = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME);
if (ct != null) {
referenceCtx.setConnEstablishedSpan(ct);
}
Integer qs = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE);
if (qs != null) {
referenceCtx.setRequestSize(qs.longValue());
}
Integer ps = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE);
if (ps != null) {
referenceCtx.setResponseSize(ps.longValue());
}
referenceCtx.setTraceId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID));
referenceCtx.setRpcId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID));
Long ce = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE);
if (ce != null) {
referenceCtx.setCostTime(ce);
}
return response;
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class BoltSendableResponseCallback method sendSofaException.
/**
* A->B(当前)->C的场景下,将远程服务端C的RPc异常异步返回给调用者A
*
* @see SofaResponseCallback#onSofaException(SofaRpcException, String, RequestBase)
*/
@Override
public void sendSofaException(SofaRpcException sofaException) {
checkState();
SofaResponse response = new SofaResponse();
response.setErrorMsg(sofaException.getMessage());
sendSofaResponse(response, sofaException);
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class BoltSendableResponseCallback method sendAppException.
/**
* A->B(当前)->C的场景下,将远程服务端C的业务异常异步返回给调用者A
*
* @see SofaResponseCallback#onAppException(Throwable, String, RequestBase)
*/
@Override
public void sendAppException(Throwable throwable) {
checkState();
SofaResponse response = new SofaResponse();
response.setAppResponse(throwable);
sendSofaResponse(response, null);
}
use of com.alipay.sofa.rpc.core.response.SofaResponse in project sofa-rpc by sofastack.
the class BoltSendableResponseCallback method sendAppResponse.
/**
* A->B(当前)->C的场景下,将远程服务端C的结果异步返回给调用者A
*
* @see SofaResponseCallback#onAppResponse(Object, String, RequestBase)
*/
@Override
public void sendAppResponse(Object appResponse) {
checkState();
SofaResponse response = new SofaResponse();
response.setAppResponse(appResponse);
sendSofaResponse(response, null);
}
Aggregations