Search in sources :

Example 96 with Result

use of org.apache.dubbo.rpc.Result in project brave by openzipkin.

the class TracingFilter method invoke.

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    if (!isInit)
        return invoker.invoke(invocation);
    TraceContext invocationContext = currentTraceContext.get();
    RpcContext rpcContext = RpcContext.getContext();
    Kind kind = rpcContext.isProviderSide() ? Kind.SERVER : Kind.CLIENT;
    Span span;
    DubboRequest request;
    if (kind.equals(Kind.CLIENT)) {
        // When A service invoke B service, then B service then invoke C service, the parentId of the
        // C service span is A when read from invocation.getAttachments(). This is because
        // AbstractInvoker adds attachments via RpcContext.getContext(), not the invocation.
        // See org.apache.dubbo.rpc.protocol.AbstractInvoker(line 141) from v2.7.3
        Map<String, String> attachments = RpcContext.getContext().getAttachments();
        DubboClientRequest clientRequest = new DubboClientRequest(invoker, invocation, attachments);
        request = clientRequest;
        span = clientHandler.handleSendWithParent(clientRequest, invocationContext);
    } else {
        DubboServerRequest serverRequest = new DubboServerRequest(invoker, invocation);
        request = serverRequest;
        span = serverHandler.handleReceive(serverRequest);
    }
    boolean isSynchronous = true;
    Scope scope = currentTraceContext.newScope(span.context());
    Result result = null;
    Throwable error = null;
    try {
        result = invoker.invoke(invocation);
        error = result.getException();
        CompletableFuture<Object> future = rpcContext.getCompletableFuture();
        if (future != null) {
            isSynchronous = false;
            // NOTE: We don't currently instrument CompletableFuture, so callbacks will not see the
            // invocation context unless they use an executor instrumented by CurrentTraceContext
            // If we later instrument this, take care to use the correct context depending on RPC kind!
            future.whenComplete(FinishSpan.create(this, request, result, span));
        }
        return result;
    } catch (Throwable e) {
        propagateIfFatal(e);
        error = e;
        throw e;
    } finally {
        if (isSynchronous)
            FinishSpan.finish(this, request, result, error, span);
        scope.close();
    }
}
Also used : Span(brave.Span) Result(org.apache.dubbo.rpc.Result) RpcContext(org.apache.dubbo.rpc.RpcContext) Scope(brave.propagation.CurrentTraceContext.Scope) Kind(brave.Span.Kind) CurrentTraceContext(brave.propagation.CurrentTraceContext) TraceContext(brave.propagation.TraceContext)

Example 97 with Result

use of org.apache.dubbo.rpc.Result in project brave by openzipkin.

the class ITTracingFilter_Provider method customParser.

@Test
public void customParser() {
    Tag<DubboResponse> javaValue = new Tag<DubboResponse>("dubbo.result_value") {

        @Override
        protected String parseValue(DubboResponse input, TraceContext context) {
            Result result = input.result();
            if (result == null)
                return null;
            return String.valueOf(result.getValue());
        }
    };
    RpcTracing rpcTracing = RpcTracing.newBuilder(tracing).serverResponseParser((res, context, span) -> {
        RpcResponseParser.DEFAULT.parse(res, context, span);
        if (res instanceof DubboResponse) {
            javaValue.tag((DubboResponse) res, span);
        }
    }).build();
    init().setRpcTracing(rpcTracing);
    String javaResult = client.get().sayHello("jorge");
    assertThat(testSpanHandler.takeRemoteSpan(SERVER).tags()).containsEntry("dubbo.result_value", javaResult);
}
Also used : DubboBootstrap(org.apache.dubbo.config.bootstrap.DubboBootstrap) RpcResponseParser(brave.rpc.RpcResponseParser) RpcContext(org.apache.dubbo.rpc.RpcContext) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ALWAYS_SAMPLE(brave.sampler.Sampler.ALWAYS_SAMPLE) Test(org.junit.Test) TraceContext(brave.propagation.TraceContext) Result(org.apache.dubbo.rpc.Result) RpcTracing(brave.rpc.RpcTracing) B3SingleFormat(brave.propagation.B3SingleFormat) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MutableSpan(brave.handler.MutableSpan) RpcRuleSampler(brave.rpc.RpcRuleSampler) NEVER_SAMPLE(brave.sampler.Sampler.NEVER_SAMPLE) ReferenceConfig(org.apache.dubbo.config.ReferenceConfig) Tag(brave.Tag) SamplingFlags(brave.propagation.SamplingFlags) SERVER(brave.Span.Kind.SERVER) RpcRequestMatchers.methodEquals(brave.rpc.RpcRequestMatchers.methodEquals) RpcRequestMatchers.serviceEquals(brave.rpc.RpcRequestMatchers.serviceEquals) Before(org.junit.Before) TraceContext(brave.propagation.TraceContext) RpcTracing(brave.rpc.RpcTracing) Tag(brave.Tag) Result(org.apache.dubbo.rpc.Result) Test(org.junit.Test)

Aggregations

Result (org.apache.dubbo.rpc.Result)97 Test (org.junit.jupiter.api.Test)74 URL (org.apache.dubbo.common.URL)55 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)39 Invocation (org.apache.dubbo.rpc.Invocation)33 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)31 AppResponse (org.apache.dubbo.rpc.AppResponse)28 Invoker (org.apache.dubbo.rpc.Invoker)27 RpcException (org.apache.dubbo.rpc.RpcException)22 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)7 RpcContext (org.apache.dubbo.rpc.RpcContext)7 BlockMyInvoker (org.apache.dubbo.rpc.support.BlockMyInvoker)6 DemoService (org.apache.dubbo.rpc.support.DemoService)6 List (java.util.List)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Protocol (org.apache.dubbo.rpc.Protocol)5 Person (org.apache.dubbo.rpc.support.Person)5 Method (java.lang.reflect.Method)4 MockProtocol (org.apache.dubbo.rpc.support.MockProtocol)4