Search in sources :

Example 21 with SofaTracerSpan

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

the class ZipkinSofaTracerSpanRemoteReporterTest method testDoServerReport.

@Test
public void testDoServerReport() throws Exception {
    // sr TL
    SofaTracerSpan sofaTracerServerSpan = this.remoteTracer.serverReceive();
    sofaTracerServerSpan.setOperationName("mockOperationName");
    // ss TL
    this.remoteTracer.serverSend("0");
    // 异步汇报,所以 sleep 1s
    Thread.sleep(1000);
    // assert
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    assertTrue(sofaTraceContext.isEmpty());
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) Test(org.junit.Test)

Example 22 with SofaTracerSpan

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

the class ZipkinSofaTracerSpanRemoteReporterTest method testDoReport.

/**
 * Method: doReport(SofaTracerSpan span)
 */
@Test
public void testDoReport() throws Exception {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    // sr TL
    SofaTracerSpan sofaTracerServerSpan = this.remoteTracer.serverReceive();
    sofaTracerServerSpan.setOperationName("ServerReveive0");
    // assert
    assertEquals(sofaTracerServerSpan, sofaTraceContext.getCurrentSpan());
    // cs
    SofaTracerSpan clientSpan = this.remoteTracer.clientSend("ClientSend0");
    // assert
    assertEquals(clientSpan, sofaTraceContext.getCurrentSpan());
    // mock sync remote call
    this.mockRemoteCall();
    // cr
    this.remoteTracer.clientReceive("0");
    assertEquals(sofaTracerServerSpan, sofaTraceContext.getCurrentSpan());
    // ss TL
    this.remoteTracer.serverSend("0");
    // 异步汇报,所以 sleep 1s
    Thread.sleep(1000);
    // assert
    assertTrue(sofaTraceContext.isEmpty());
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) Test(org.junit.Test)

Example 23 with SofaTracerSpan

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

the class ZipkinSofaTracerSpanRemoteReporterTest method mockRemoteCall.

public void mockRemoteCall() throws Exception {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan originSofaTracerSpan = sofaTraceContext.getCurrentSpan();
    SofaTracerSpanContext spanContext = originSofaTracerSpan.getSofaTracerSpanContext();
    sofaTraceContext.clear();
    // sr
    SofaTracerSpan srSpan = this.remoteTracer.serverReceive(spanContext);
    srSpan.setOperationName("ServerReceive1");
    assertEquals(srSpan, sofaTraceContext.getCurrentSpan());
    // ss
    this.remoteTracer.serverSend("0");
    assertTrue(sofaTraceContext.getThreadLocalSpanSize() == 0);
    // mock restore
    sofaTraceContext.push(originSofaTracerSpan);
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) SofaTracerSpanContext(com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)

Example 24 with SofaTracerSpan

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

the class ZipkinSofaTracerSpanRemoteReporterTest method testDoClientReport.

@Test
public void testDoClientReport() throws Exception {
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    sofaTraceContext.clear();
    // cs
    SofaTracerSpan clientSpan = this.remoteTracer.clientSend("testDoClientReport");
    assertTrue(clientSpan != null);
    // cr
    this.remoteTracer.clientReceive("0");
    // assert
    assertTrue(sofaTraceContext.isEmpty());
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) Test(org.junit.Test)

Example 25 with SofaTracerSpan

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

the class ServerReqHeaderInterceptor method interceptCall.

@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(final ServerCall<ReqT, RespT> call, final Metadata requestHeaders, ServerCallHandler<ReqT, RespT> next) {
    final ServerServiceDefinition serverServiceDefinition = this.getServerServiceDefinition();
    SofaResponse sofaResponse = new SofaResponse();
    final Throwable[] throwable = { null };
    SofaRequest sofaRequest = new SofaRequest();
    TripleTracerAdapter.serverReceived(sofaRequest, serverServiceDefinition, call, requestHeaders);
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
    Context ctxWithSpan = Context.current().withValue(TracingContextKey.getKey(), serverSpan).withValue(TracingContextKey.getSpanContextKey(), serverSpan.context()).withValue(TracingContextKey.getKeySofaRequest(), sofaRequest);
    // 这里和下面不在一个线程
    if (RpcRunningState.isDebugMode()) {
        LOGGER.info("[1]header received from client:" + requestHeaders);
    }
    ServerCall<ReqT, RespT> realCall = new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {

        @Override
        public void sendHeaders(Metadata responseHeaders) {
            if (RpcRunningState.isDebugMode()) {
                LOGGER.info("[4]send response header:{}", responseHeaders);
            }
            super.sendHeaders(responseHeaders);
        }

        // 服务端发完了
        @Override
        public void sendMessage(RespT message) {
            if (RpcRunningState.isDebugMode()) {
                LOGGER.info("[5]send response message:{}", message);
            }
            super.sendMessage(message);
            sofaResponse.setAppResponse(message);
        }

        @Override
        public void close(Status status, Metadata trailers) {
            if (RpcRunningState.isDebugMode()) {
                LOGGER.info("[6]send response message:{},trailers:{}", status, trailers);
            }
            super.close(status, trailers);
        }
    };
    ServerCall.Listener<ReqT> listenerWithContext = Contexts.interceptCall(ctxWithSpan, realCall, requestHeaders, next);
    ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT> result = new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(listenerWithContext) {

        // 完成的时候走到这里
        @Override
        public void onComplete() {
            // 和代码执行不一定在一个线程池
            super.onComplete();
            if (RpcRunningState.isDebugMode()) {
                LOGGER.info("[7]server processed done received from client:" + requestHeaders);
            }
            TripleTracerAdapter.serverReceived(sofaRequest, serverServiceDefinition, call, requestHeaders);
            // 进行一下补偿
            SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
            SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
            SofaTracerSpan originalSpan = (SofaTracerSpan) TracingContextKey.getKey().get(ctxWithSpan);
            serverSpan.setStartTime(originalSpan.getStartTime());
            serverSpan.setTag("remote.ip", originalSpan.getTagsWithStr().get("remote.ip"));
            long endTime = RpcRuntimeContext.now();
            serverSpan.setTag("biz.impl.time", endTime - originalSpan.getStartTime());
            TripleTracerAdapter.serverSend(sofaRequest, requestHeaders, sofaResponse, throwable[0]);
        }

        // 客户端发完了
        @Override
        public void onHalfClose() {
            try {
                doOnHalfClose();
            } finally {
                RpcInvokeContext.removeContext();
            }
        }

        private void doOnHalfClose() {
            if (RpcRunningState.isDebugMode()) {
                LOGGER.info("[2]body received done from client:" + requestHeaders);
            }
            // 服务端收到所有信息
            TripleTracerAdapter.serverReceived(sofaRequest, serverServiceDefinition, call, requestHeaders);
            try {
                super.onHalfClose();
            } catch (Throwable t) {
                // 统一处理异常
                StatusRuntimeException exception = fromThrowable(t);
                // 调用 call.close() 发送 Status 和 metadata
                // 这个方式和 onError()本质是一样的
                call.close(exception.getStatus(), exception.getTrailers());
                throwable[0] = t;
            }
        }

        private StatusRuntimeException fromThrowable(Throwable t) {
            final Metadata trailers = new Metadata();
            return new StatusRuntimeException(Status.UNKNOWN, trailers);
        }
    };
    return result;
}
Also used : RpcRuntimeContext(com.alipay.sofa.rpc.context.RpcRuntimeContext) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) Context(io.grpc.Context) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) Status(io.grpc.Status) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) Metadata(io.grpc.Metadata) SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) ForwardingServerCallListener(io.grpc.ForwardingServerCallListener) ForwardingServerCall(io.grpc.ForwardingServerCall) ServerCall(io.grpc.ServerCall) ServerServiceDefinition(io.grpc.ServerServiceDefinition) StatusRuntimeException(io.grpc.StatusRuntimeException) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

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