Search in sources :

Example 16 with RpcContext

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

the class DubboParser method parseRemoteIpAndPort.

static boolean parseRemoteIpAndPort(Span span) {
    RpcContext rpcContext = RpcContext.getContext();
    InetSocketAddress remoteAddress = rpcContext.getRemoteAddress();
    if (remoteAddress == null)
        return false;
    return span.remoteIpAndPort(Platform.get().getHostString(remoteAddress), remoteAddress.getPort());
}
Also used : RpcContext(org.apache.dubbo.rpc.RpcContext) InetSocketAddress(java.net.InetSocketAddress)

Example 17 with RpcContext

use of org.apache.dubbo.rpc.RpcContext 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 18 with RpcContext

use of org.apache.dubbo.rpc.RpcContext in project pinpoint by naver.

the class ApacheDubboProviderInterceptor method recordRequest.

private void recordRequest(SpanRecorder recorder, Object target, Object[] args) {
    final RpcInvocation invocation = (RpcInvocation) args[0];
    final RpcContext rpcContext = RpcContext.getContext();
    // Record rpc name, client address, server address.
    recorder.recordRpcName(invocation.getInvoker().getInterface().getSimpleName() + ":" + invocation.getMethodName());
    recorder.recordEndPoint(rpcContext.getLocalAddressString());
    if (rpcContext.getRemoteHost() != null) {
        recorder.recordRemoteAddress(rpcContext.getRemoteAddressString());
    } else {
        recorder.recordRemoteAddress("Unknown");
    }
    // If this transaction did not begin here, record parent(client who sent this request) information
    if (!recorder.isRoot()) {
        final String parentApplicationName = invocation.getAttachment(ApacheDubboConstants.META_PARENT_APPLICATION_NAME);
        if (parentApplicationName != null) {
            final short parentApplicationType = NumberUtils.parseShort(invocation.getAttachment(ApacheDubboConstants.META_PARENT_APPLICATION_TYPE), ServiceType.UNDEFINED.getCode());
            recorder.recordParentApplication(parentApplicationName, parentApplicationType);
            final String host = invocation.getAttachment(ApacheDubboConstants.META_HOST);
            if (host != null) {
                recorder.recordAcceptorHost(host);
            } else {
                // old version fallback
                final String estimatedLocalHost = getLocalHost(rpcContext);
                if (estimatedLocalHost != null) {
                    recorder.recordAcceptorHost(estimatedLocalHost);
                }
            }
        }
    }
    // clear attachments
    this.clearAttachments(rpcContext);
}
Also used : RpcInvocation(org.apache.dubbo.rpc.RpcInvocation) RpcContext(org.apache.dubbo.rpc.RpcContext)

Aggregations

RpcContext (org.apache.dubbo.rpc.RpcContext)18 RpcInvocation (org.apache.dubbo.rpc.RpcInvocation)5 Test (org.junit.jupiter.api.Test)5 Result (org.apache.dubbo.rpc.Result)4 RpcException (org.apache.dubbo.rpc.RpcException)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AsyncRpcResult (org.apache.dubbo.rpc.AsyncRpcResult)2 Span (brave.Span)1 Kind (brave.Span.Kind)1 CurrentTraceContext (brave.propagation.CurrentTraceContext)1 Scope (brave.propagation.CurrentTraceContext.Scope)1 TraceContext (brave.propagation.TraceContext)1 HessianConnection (com.caucho.hessian.client.HessianConnection)1 ForwardingServerCallListener (io.grpc.ForwardingServerCallListener)1 Metadata (io.grpc.Metadata)1 InetSocketAddress (java.net.InetSocketAddress)1 MetricsService (org.apache.dubbo.monitor.MetricsService)1 Protocol (org.apache.dubbo.rpc.Protocol)1 TimeoutCountDown (org.apache.dubbo.rpc.TimeoutCountDown)1