Search in sources :

Example 1 with Kind

use of brave.Span.Kind in project brave by openzipkin.

the class TracingFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (tracer == null)
        return invoker.invoke(invocation);
    RpcContext rpcContext = RpcContext.getContext();
    Kind kind = rpcContext.isProviderSide() ? Kind.SERVER : Kind.CLIENT;
    final Span span;
    if (kind.equals(Kind.CLIENT)) {
        span = tracer.nextSpan();
        injector.inject(span.context(), invocation.getAttachments());
    } else {
        TraceContextOrSamplingFlags extracted = extractor.extract(invocation.getAttachments());
        span = extracted.context() != null ? tracer.joinSpan(extracted.context()) : tracer.nextSpan(extracted);
    }
    if (!span.isNoop()) {
        span.kind(kind).start();
        String service = invoker.getInterface().getSimpleName();
        String method = RpcUtils.getMethodName(invocation);
        span.kind(kind);
        span.name(service + "/" + method);
        InetSocketAddress remoteAddress = rpcContext.getRemoteAddress();
        Endpoint.Builder remoteEndpoint = Endpoint.newBuilder().port(remoteAddress.getPort());
        if (!remoteEndpoint.parseIp(remoteAddress.getAddress())) {
            remoteEndpoint.parseIp(remoteAddress.getHostName());
        }
        span.remoteEndpoint(remoteEndpoint.build());
    }
    boolean isOneway = false, deferFinish = false;
    try (Tracer.SpanInScope scope = tracer.withSpanInScope(span)) {
        Result result = invoker.invoke(invocation);
        if (result.hasException()) {
            onError(result.getException(), span.customizer());
        }
        isOneway = RpcUtils.isOneway(invoker.getUrl(), invocation);
        // the case on async client invocation
        Future<Object> future = rpcContext.getFuture();
        if (future instanceof FutureAdapter) {
            deferFinish = true;
            ((FutureAdapter) future).getFuture().setCallback(new FinishSpanCallback(span));
        }
        return result;
    } catch (Error | RuntimeException e) {
        onError(e, span.customizer());
        throw e;
    } finally {
        if (isOneway) {
            span.flush();
        } else if (!deferFinish) {
            span.finish();
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Tracer(brave.Tracer) FutureAdapter(com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter) Span(brave.Span) Result(com.alibaba.dubbo.rpc.Result) RpcContext(com.alibaba.dubbo.rpc.RpcContext) Endpoint(zipkin2.Endpoint) Kind(brave.Span.Kind) TraceContextOrSamplingFlags(brave.propagation.TraceContextOrSamplingFlags)

Aggregations

Span (brave.Span)1 Kind (brave.Span.Kind)1 Tracer (brave.Tracer)1 TraceContextOrSamplingFlags (brave.propagation.TraceContextOrSamplingFlags)1 Result (com.alibaba.dubbo.rpc.Result)1 RpcContext (com.alibaba.dubbo.rpc.RpcContext)1 FutureAdapter (com.alibaba.dubbo.rpc.protocol.dubbo.FutureAdapter)1 InetSocketAddress (java.net.InetSocketAddress)1 Endpoint (zipkin2.Endpoint)1