use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class SofaRpcMetrics method onEvent.
private void onEvent(ClientEndInvokeEvent event) {
InvokeMeta meta = new InvokeMeta(event.getRequest(), event.getResponse(), getLongAvoidNull(RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE)));
RpcInternalContext context = RpcInternalContext.getContext();
Duration elapsed = meta.elapsed();
Tags tags = meta.tags(this.common);
clientTotal.apply(tags).record(elapsed);
if (!meta.success()) {
clientFail.apply(tags).record(elapsed);
}
requestSize.apply(tags).record(getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE)));
responseSize.apply(tags).record(getLongAvoidNull(context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE)));
}
use of com.alipay.sofa.rpc.context.RpcInternalContext 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.context.RpcInternalContext in project sofa-rpc by sofastack.
the class RestTracerAdapter method beforeSend.
/**
* 存入tracer信息
*
* @param requestContext ClientRequestContext
*/
public static void beforeSend(ClientRequestContext requestContext) {
// tracer信息放入request 发到服务端
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
RpcInternalContext context = RpcInternalContext.getContext();
if (clientSpan != null) {
requestContext.getHeaders().add(RemotingConstants.NEW_RPC_TRACE_NAME, clientSpan.getSofaTracerSpanContext().serializeSpanContext());
}
// 客户端发送自己的应用名
String appName = (String) context.getAttachment(INTERNAL_KEY_APP_NAME);
if (appName != null) {
requestContext.getHeaders().add(RemotingConstants.HEAD_APP_NAME, appName);
}
RestBaggageItemsHandler.encodeBaggageItemToRequest(requestContext.getHeaders());
}
use of com.alipay.sofa.rpc.context.RpcInternalContext in project sofa-rpc by sofastack.
the class TraceClientResponseFilter method filter.
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
if (RpcInternalContext.isAttachmentEnable()) {
// 补充客户端response长度
RpcInternalContext context = RpcInternalContext.getContext();
context.setAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE, responseContext.getLength());
}
RestTracerAdapter.clientReceived(responseContext);
}
use of com.alipay.sofa.rpc.context.RpcInternalContext 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;
}
Aggregations