use of com.alipay.common.tracer.core.context.trace.SofaTraceContext 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.common.tracer.core.context.trace.SofaTraceContext 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.common.tracer.core.context.trace.SofaTraceContext in project sofa-rpc by sofastack.
the class TripleTracerAdapter method serverReceived.
/**
* 适配服务端serverReceived
*/
public static void serverReceived(SofaRequest sofaRequest, ServerServiceDefinition serverServiceDefinition, final ServerCall call, Metadata requestHeaders) {
try {
if (sofaRequest == null) {
sofaRequest = new SofaRequest();
}
Map<String, String> traceMap = new HashMap<String, String>();
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE)) {
sofaRequest.setTargetServiceUniqueName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE));
sofaRequest.setInterfaceName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE));
} else {
String serviceName = serverServiceDefinition.getServiceDescriptor().getName();
sofaRequest.setTargetServiceUniqueName(serviceName);
sofaRequest.setInterfaceName(serviceName);
}
final String serviceName = call.getMethodDescriptor().getServiceName();
sofaRequest.setTargetServiceUniqueName(serviceName);
sofaRequest.setInterfaceName(serviceName);
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TARGET_APP)) {
sofaRequest.setTargetAppName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_APP));
}
// 先取兼容的
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID)) {
traceMap.put(TracerCompatibleConstants.TRACE_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID));
} else if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TRACE_ID)) {
traceMap.put(TracerCompatibleConstants.TRACE_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_TRACE_ID));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID)) {
traceMap.put(TracerCompatibleConstants.RPC_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID));
} else if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_RPC_ID)) {
traceMap.put(TracerCompatibleConstants.RPC_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_RPC_ID));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION)) {
RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION));
} else {
RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, "");
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SAMP_TYPE)) {
traceMap.put(TracerCompatibleConstants.SAMPLING_MARK, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SAMP_TYPE));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_CURRENT_APP)) {
sofaRequest.addRequestProp(HEAD_APP_NAME, requestHeaders.get(TripleHeadKeys.HEAD_KEY_CURRENT_APP));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE)) {
sofaRequest.addRequestProp(RemotingConstants.HEAD_PROTOCOL, requestHeaders.get(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE)) {
sofaRequest.addRequestProp(RemotingConstants.HEAD_INVOKE_TYPE, requestHeaders.get(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE)) {
traceMap.put(TracerCompatibleConstants.PEN_ATTRS_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE));
}
if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE)) {
traceMap.put(TracerCompatibleConstants.PEN_SYS_ATTRS_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE));
}
if (!traceMap.isEmpty()) {
sofaRequest.addRequestProp(RemotingConstants.RPC_TRACE_NAME, traceMap);
}
if (EventBus.isEnable(ServerReceiveEvent.class)) {
EventBus.post(new ServerReceiveEvent(sofaRequest));
}
String remoteIp = "";
SocketAddress socketAddress = call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
if (socketAddress instanceof InetSocketAddress) {
remoteIp = ((InetSocketAddress) socketAddress).getHostName();
}
String methodName;
String fullMethodName = call.getMethodDescriptor().getFullMethodName();
methodName = StringUtils.substringAfter(fullMethodName, serviceName + "/");
sofaRequest.setMethodName(methodName);
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
if (serverSpan != null) {
// FIXME modify the dep relation
serverSpan.setTag("service", sofaRequest.getTargetServiceUniqueName());
serverSpan.setTag("method", methodName);
// 从请求里获取ConsumerTracerFilter额外传递的信息
serverSpan.setTag("remote.app", (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
serverSpan.setTag("protocol", RpcConstants.PROTOCOL_TYPE_TRIPLE);
serverSpan.setTag("remote.ip", remoteIp);
}
} catch (Throwable e) {
LOGGER.warn("triple serverReceived tracer error", e);
}
}
use of com.alipay.common.tracer.core.context.trace.SofaTraceContext in project sofa-rpc by sofastack.
the class ConsumerTracerFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
clientSpan.setTag(RpcSpanTags.INVOKE_TYPE, request.getInvokeType());
RpcInternalContext context = RpcInternalContext.getContext();
clientSpan.setTag(RpcSpanTags.ROUTE_RECORD, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
ProviderInfo providerInfo = context.getProviderInfo();
if (providerInfo != null) {
clientSpan.setTag(RpcSpanTags.REMOTE_APP, providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
clientSpan.setTag(RpcSpanTags.REMOTE_IP, providerInfo.getHost() + ":" + providerInfo.getPort());
}
return invoker.invoke(request);
// 因为异步的场景,所以received不写在这里
}
use of com.alipay.common.tracer.core.context.trace.SofaTraceContext in project sofa-rpc by sofastack.
the class ProviderTracerFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
SofaTracerSpan serverSpan = null;
try {
SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
serverSpan = sofaTraceContext.getCurrentSpan();
if (serverSpan != null) {
RpcInternalContext context = RpcInternalContext.getContext();
serverSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
serverSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
// 客户端地址
serverSpan.setTag(RpcSpanTags.REMOTE_IP, context.getRemoteHostName());
// 从请求里获取ConsumerTracerFilter额外传递的信息
serverSpan.setTag(RpcSpanTags.REMOTE_APP, (String) request.getRequestProp(HEAD_APP_NAME));
serverSpan.setTag(RpcSpanTags.PROTOCOL, (String) request.getRequestProp(HEAD_PROTOCOL));
serverSpan.setTag(RpcSpanTags.INVOKE_TYPE, (String) request.getRequestProp(HEAD_INVOKE_TYPE));
ProviderConfig providerConfig = (ProviderConfig) invoker.getConfig();
serverSpan.setTag(RpcSpanTags.LOCAL_APP, providerConfig.getAppName());
serverSpan.setTag(RpcSpanTags.SERVER_THREAD_POOL_WAIT_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_PROCESS_WAIT_TIME));
}
return invoker.invoke(request);
} finally {
if (serverSpan != null) {
serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
}
}
}
Aggregations