Search in sources :

Example 26 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class TripleTracerAdapter method beforeSend.

/**
 * 存入tracer信息
 *
 * @param sofaRequest   SofaRequest
 * @param requestHeader Metadata
 */
public static void beforeSend(SofaRequest sofaRequest, ConsumerConfig consumerConfig, Metadata requestHeader) {
    // 客户端设置请求服务端的Header
    // tracer信息放入request 发到服务端
    Map<String, String> header = new HashMap<String, String>();
    header.put(RemotingConstants.HEAD_METHOD_NAME, sofaRequest.getMethodName());
    header.put(RemotingConstants.HEAD_TARGET_SERVICE, sofaRequest.getTargetServiceUniqueName());
    header.put(RemotingConstants.HEAD_TARGET_APP, sofaRequest.getTargetAppName());
    // 客户端的启动
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    // 获取并不弹出
    SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
    if (clientSpan == null) {
        SelfLog.warn("ClientSpan is null.Before call interface=" + sofaRequest.getInterfaceName() + ",method=" + sofaRequest.getMethodName());
    } else {
        SofaTracerSpanContext sofaTracerSpanContext = clientSpan.getSofaTracerSpanContext();
        header.put(TripleHeadKeys.HEAD_KEY_TRACE_ID.name(), sofaTracerSpanContext.getTraceId());
        header.put(TripleHeadKeys.HEAD_KEY_RPC_ID.name(), sofaTracerSpanContext.getSpanId());
        header.put(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID.name(), sofaTracerSpanContext.getTraceId());
        header.put(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID.name(), sofaTracerSpanContext.getSpanId());
        header.put(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE.name(), sofaTracerSpanContext.getBizSerializedBaggage());
        header.put(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE.name(), sofaTracerSpanContext.getSysSerializedBaggage());
    }
    // 获取 RPC 上下文
    RpcInvokeContext internalContext = RpcInvokeContext.getContext();
    String route = (String) internalContext.get(USERID_KEY);
    if (StringUtils.isNotBlank(route)) {
        Map<String, String> map = new HashMap<>();
        map.put(USERID_KEY, route);
        header.put(TripleHeadKeys.HEAD_KEY_UNIT_INFO.name(), JSONUtils.toJSONString(map));
    }
    if (StringUtils.isNotEmpty(consumerConfig.getUniqueId())) {
        header.put(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION.name(), consumerConfig.getUniqueId());
    }
    header.put(TripleHeadKeys.HEAD_KEY_META_TYPE.name(), "rpc");
    header.put(TripleHeadKeys.HEAD_KEY_CURRENT_APP.name(), (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
    header.put(TripleHeadKeys.HEAD_KEY_CONSUMER_APP.name(), (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
    header.put(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE.name(), (String) sofaRequest.getRequestProp(RemotingConstants.HEAD_PROTOCOL));
    header.put(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE.name(), (String) sofaRequest.getRequestProp(RemotingConstants.HEAD_INVOKE_TYPE));
    final String source = consumerConfig.getParameter("interworking.source");
    if (StringUtils.isNotBlank(source)) {
        header.put(TripleHeadKeys.HEAD_KEY_SOURCE_TENANTID.name(), source);
    }
    final String target = consumerConfig.getParameter("interworking.target");
    if (StringUtils.isNotBlank(target)) {
        header.put(TripleHeadKeys.HEAD_KEY_TARGET_TENANTID.name(), target);
    }
    for (Map.Entry<String, String> entry : header.entrySet()) {
        if (StringUtils.isNotBlank(entry.getValue())) {
            requestHeader.put(TripleHeadKeys.getKey(entry.getKey()), entry.getValue());
        }
    }
    // set custom headers
    try {
        Set<Map.Entry<String, String>> customHeader = RpcInvokeContext.getContext().getCustomHeader().entrySet();
        for (Map.Entry<String, String> entry : customHeader) {
            if (StringUtils.isNotBlank(entry.getValue())) {
                requestHeader.put(TripleHeadKeys.getKey(entry.getKey()), entry.getValue());
            }
        }
    } finally {
        RpcInvokeContext.getContext().clearCustomHeader();
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) HashMap(java.util.HashMap) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) SofaTracerSpanContext(com.alipay.common.tracer.core.context.span.SofaTracerSpanContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class PressureMarkTransformFilterTest method testProviderPressure.

@Test
public void testProviderPressure() {
    Metadata metadata = new Metadata();
    metadata.put(HEAD_KEY_TRAFFIC_TYPE, "pressure");
    Context context = Context.current().withValue(TracingContextKey.getKeyMetadata(), metadata);
    context.attach();
    PressureMarkTransformFilter filter = new PressureMarkTransformFilter();
    filter.invoke(invoker, request);
    SofaTracerSpan currentSpan = SofaTraceContextHolder.getSofaTraceContext().getCurrentSpan();
    Assert.assertTrue(TracerUtils.isLoadTest(currentSpan));
}
Also used : Context(io.grpc.Context) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) Metadata(io.grpc.Metadata) Test(org.junit.Test)

Example 28 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class RpcSofaTracer method startRpc.

@Override
public void startRpc(SofaRequest request) {
    // 客户端的启动
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan serverSpan = sofaTraceContext.pop();
    SofaTracerSpan clientSpan = (SofaTracerSpan) this.sofaTracer.buildSpan(request.getInterfaceName()).asChildOf(serverSpan).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).start();
    if (RpcInternalContext.isAttachmentEnable()) {
        RpcInternalContext context = RpcInternalContext.getContext();
        clientSpan.setTag(RpcSpanTags.LOCAL_APP, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_APP_NAME));
        clientSpan.setTag(RpcSpanTags.PROTOCOL, (String) context.getAttachment(RpcConstants.INTERNAL_KEY_PROTOCOL_NAME));
        SofaTracerSpanContext spanContext = clientSpan.getSofaTracerSpanContext();
        if (spanContext != null) {
            context.setAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID, spanContext.getTraceId());
            context.setAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID, spanContext.getSpanId());
        }
    }
    clientSpan.setTag(RpcSpanTags.SERVICE, request.getTargetServiceUniqueName());
    clientSpan.setTag(RpcSpanTags.METHOD, request.getMethodName());
    clientSpan.setTag(RpcSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
    // 需要主动缓存自己的 serverSpan,原因是:asChildOf 关注的是 spanContext
    clientSpan.setParentSofaTracerSpan(serverSpan);
    // push
    sofaTraceContext.push(clientSpan);
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaTracerSpanContext(com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)

Example 29 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class RpcSofaTracer method profile.

@Override
public void profile(String profileApp, String code, String message) {
    // 注意 profile 需要打印 traceId 等信息,所以要打开 tracer 才可以
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan sofaTracerSpan = sofaTraceContext.getCurrentSpan();
    if (sofaTracerSpan != null) {
        sofaTracerSpan.profile(profileApp, code, message);
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext)

Example 30 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class RpcSofaTracer method clientAsyncReceivedPrepare.

@Override
public void clientAsyncReceivedPrepare() {
    // 新的线程
    RpcInternalContext rpcInternalContext = RpcInternalContext.getContext();
    SofaTracerSpan clientSpan = (SofaTracerSpan) rpcInternalContext.getAttachment(RpcConstants.INTERNAL_KEY_TRACER_SPAN);
    if (clientSpan == null) {
        return;
    }
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    sofaTraceContext.push(clientSpan);
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Aggregations

SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)35 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)25 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)12 Test (org.junit.Test)12 SofaTracerSpanContext (com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)6 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)6 HashMap (java.util.HashMap)6 Metadata (io.grpc.Metadata)4 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)3 Context (io.grpc.Context)3 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 FlexibleTracer (com.alipay.sofa.tracer.plugin.flexible.FlexibleTracer)2 InetSocketAddress (java.net.InetSocketAddress)2 Before (org.junit.Before)2 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1