Java Code Examples for com.alibaba.dubbo.rpc.RpcContext#isConsumerSide()

The following examples show how to use com.alibaba.dubbo.rpc.RpcContext#isConsumerSide() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlibabaMonitorFilterAdvice.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnterFilterInvoke(@Advice.Argument(1) Invocation invocation,
                                       @Advice.Local("span") Span span,
                                       @Advice.Local("apiClazz") Class<?> apiClazz,
                                       @Advice.Local("transaction") Transaction transaction) {
    RpcContext context = RpcContext.getContext();
    AlibabaDubboAttachmentHelper helper = helperManager.getForClassLoaderOfClass(Invocation.class);
    if (helper == null || tracer == null) {
        return;
    }
    // for consumer side, just create span, more information will be collected in provider side
    AbstractSpan<?> active = tracer.getActive();
    if (context.isConsumerSide() && active != null) {
        span = DubboTraceHelper.createConsumerSpan(tracer, invocation.getInvoker().getInterface(),
            invocation.getMethodName(), context.getRemoteAddress());
        if (span != null) {
            span.propagateTraceContext(invocation, helper);
        }
    } else if (active == null) {
        // for provider side
        transaction = tracer.startChildTransaction(invocation, helper, Invocation.class.getClassLoader());
        if (transaction != null) {
            transaction.activate();
            DubboTraceHelper.fillTransaction(transaction, invocation.getInvoker().getInterface(), invocation.getMethodName());
        }
    }

}
 
Example 2
Source File: CicadaDubboFilter.java    From cicada with MIT License 5 votes vote down vote up
private void invokerBefore(final Invocation invocation, final Span span, final Endpoint endpoint, final long start) {
  final RpcContext context = RpcContext.getContext();
  if (context.isConsumerSide()) {
    if (span.isSample()) {
      tracer.clientSendRecord(span, endpoint, start);

      final RpcInvocation rpcInvocation = (RpcInvocation) invocation;
      rpcInvocation.setAttachment(TracerUtils.PARENT_SPAN_ID, span.getParentId());
      rpcInvocation.setAttachment(TracerUtils.SPAN_ID, span.getId());
      rpcInvocation.setAttachment(TracerUtils.TRACE_ID, span.getTraceId());
    }
  } else if (context.isProviderSide()) {
    tracer.serverReceiveRecord(span, endpoint, start);
  }
}
 
Example 3
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 4 votes vote down vote up
private String spanKind(RpcContext rpcContext) {
    return rpcContext.isConsumerSide() ? Tags.SPAN_KIND_CLIENT : Tags.SPAN_KIND_SERVER;
}
 
Example 4
Source File: CicadaDubboFilter.java    From cicada with MIT License 4 votes vote down vote up
@SuppressWarnings({"PMD.OnlyOneReturn", "PMD.OnlyOneReturn"})
public Result invoke(final Invoker<?> invoker, final Invocation invocation) throws RpcException {
  if (tracer == null) {
    return invoker.invoke(invocation);
  }
  final long startTime = System.currentTimeMillis();
  final RpcContext context = RpcContext.getContext();

  // 传入参数,暂不做处理
  // Object[] arguments = context.getArguments();
  // for (Object argument : arguments) {
  // LOGGER.error("arg:" + argument);
  // }

  final String localIp = IpUtils.getRealIpWithStaticCache();
  final int localPort = context.getLocalPort();
  final Endpoint endpoint = new Endpoint(localIp, localPort);

  final URL url = context.getUrl();
  final String appName = url.getParameter("application");
  final String serviceName = url.getServiceInterface();
  final String methodName = context.getMethodName();

  final boolean isConsumerSide = context.isConsumerSide();
  Span span = null;
  try {
    if (isConsumerSide) { // 是否是消费者
      final Span parentSpan = tracer.getParentSpan();
      if (parentSpan == null) { // 为rootSpan
        // 生成root Span
        span = tracer.newSpan(appName, serviceName, methodName);
      } else {
        span = tracer.newSpan(appName, serviceName, methodName, parentSpan);
      }
    } else if (context.isProviderSide()) {
      final String traceId = invocation.getAttachment(TracerUtils.TRACE_ID);
      final String parentId = invocation.getAttachment(TracerUtils.PARENT_SPAN_ID);
      final String spanId = invocation.getAttachment(TracerUtils.SPAN_ID);
      final boolean sample = traceId != null;
      span = tracer.genSpan(appName, serviceName, methodName, traceId, parentId, spanId, sample);
    } else {
      LOGGER.error("[" + url + "] [notConsumerNorProvider]");
      return invoker.invoke(invocation);
    }

    invokerBefore(invocation, span, endpoint, startTime);
    final Result result = invoker.invoke(invocation);
    final Throwable throwable = result.getException();
    if (throwable != null && !isConsumerSide) {
      span.addException(serviceName, methodName, throwable, endpoint);
    }

    // 返回值
    // Object resultValue = result.getValue();
    // LOGGER.error("return:" + JSON.toJSONString(resultValue));
    return result;
  } catch (final RpcException ex) {
    if (span != null) {
      span.addException(serviceName, methodName, ex, endpoint);
    }
    throw ex;
  } finally {
    if (span != null) {
      final long end = System.currentTimeMillis();
      invokerAfter(endpoint, span, end, isConsumerSide);// 调用后记录annotation
    }
  }
}