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());
}
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());
}
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);
}
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());
}
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;
}
Aggregations