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

The following examples show how to use com.alibaba.dubbo.rpc.RpcContext#getMethodName() . 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: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
/**
 * set rpc server span tags
 * @param invoker
 * @param sofaTracerSpan
 */
private void appendRpcServerSpanTags(Invoker<?> invoker, SofaTracerSpan sofaTracerSpan) {
    if (sofaTracerSpan == null) {
        return;
    }
    RpcContext rpcContext = RpcContext.getContext();
    Map<String, String> tagsStr = sofaTracerSpan.getTagsWithStr();
    tagsStr.put(Tags.SPAN_KIND.getKey(), spanKind(rpcContext));
    String service = invoker.getInterface().getName();
    tagsStr.put(CommonSpanTags.SERVICE, service == null ? BLANK : service);
    String methodName = rpcContext.getMethodName();
    tagsStr.put(CommonSpanTags.METHOD, methodName == null ? BLANK : methodName);
    String app = rpcContext.getUrl().getParameter(Constants.APPLICATION_KEY);
    tagsStr.put(CommonSpanTags.REMOTE_HOST, rpcContext.getRemoteHost());
    tagsStr.put(CommonSpanTags.LOCAL_APP, app == null ? BLANK : app);
    tagsStr.put(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
    String protocol = rpcContext.getUrl().getProtocol();
    tagsStr.put(CommonSpanTags.PROTOCOL, protocol == null ? BLANK : protocol);
    tagsStr.put(CommonSpanTags.LOCAL_HOST, rpcContext.getRemoteHost());
    tagsStr.put(CommonSpanTags.LOCAL_PORT, String.valueOf(rpcContext.getRemotePort()));
}
 
Example 2
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 6 votes vote down vote up
/**
 * set rpc client span tags
 * @param invoker
 * @param sofaTracerSpan
 */
private void appendRpcClientSpanTags(Invoker<?> invoker, SofaTracerSpan sofaTracerSpan) {
    if (sofaTracerSpan == null) {
        return;
    }
    RpcContext rpcContext = RpcContext.getContext();
    Map<String, String> tagsStr = sofaTracerSpan.getTagsWithStr();
    tagsStr.put(Tags.SPAN_KIND.getKey(), spanKind(rpcContext));
    String protocol = rpcContext.getUrl().getProtocol();
    tagsStr.put(CommonSpanTags.PROTOCOL, protocol == null ? BLANK : protocol);
    String service = invoker.getInterface().getName();
    tagsStr.put(CommonSpanTags.SERVICE, service == null ? BLANK : service);
    String methodName = rpcContext.getMethodName();
    tagsStr.put(CommonSpanTags.METHOD, methodName == null ? BLANK : methodName);
    tagsStr.put(CommonSpanTags.CURRENT_THREAD_NAME, Thread.currentThread().getName());
    String app = rpcContext.getUrl().getParameter(Constants.APPLICATION_KEY);
    tagsStr.put(CommonSpanTags.LOCAL_APP, app == null ? BLANK : app);
    tagsStr.put(CommonSpanTags.REMOTE_HOST, rpcContext.getRemoteHost());
    tagsStr.put(CommonSpanTags.REMOTE_PORT, String.valueOf(rpcContext.getRemotePort()));
    tagsStr.put(CommonSpanTags.LOCAL_HOST, rpcContext.getLocalHost());
}
 
Example 3
Source File: DubboSofaTracerFilter.java    From sofa-tracer with Apache License 2.0 4 votes vote down vote up
/**
 * rpc client handler
 * @param rpcContext
 * @param invoker
 * @param invocation
 * @return
 */
private Result doClientFilter(RpcContext rpcContext, Invoker<?> invoker, Invocation invocation) {
    // to build tracer instance
    if (dubboConsumerSofaTracer == null) {
        this.dubboConsumerSofaTracer = DubboConsumerSofaTracer
            .getDubboConsumerSofaTracerSingleton();
    }
    // get methodName
    String methodName = rpcContext.getMethodName();
    // get service interface
    String service = invoker.getInterface().getSimpleName();
    // build a dubbo rpc span
    SofaTracerSpan sofaTracerSpan = dubboConsumerSofaTracer.clientSend(service + "#"
                                                                       + methodName);
    // set tags to span
    appendRpcClientSpanTags(invoker, sofaTracerSpan);
    // do serialized and then transparent transmission to the rpc server
    String serializedSpanContext = sofaTracerSpan.getSofaTracerSpanContext()
        .serializeSpanContext();
    //put into attachments
    invocation.getAttachments().put(CommonSpanTags.RPC_TRACE_NAME, serializedSpanContext);

    boolean isOneWay = false, deferFinish = false;

    // check invoke type
    boolean isAsync = RpcUtils.isAsync(invoker.getUrl(), invocation);

    // set invoke type tag
    if (isAsync) {
        sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "future");
    } else {
        isOneWay = RpcUtils.isOneway(invoker.getUrl(), invocation);
        if (isOneWay) {
            sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "oneway");
        } else {
            sofaTracerSpan.setTag(CommonSpanTags.INVOKE_TYPE, "sync");
        }
    }

    Result result;
    Throwable exception = null;
    String resultCode = SofaTracerConstant.RESULT_CODE_SUCCESS;
    try {
        // do invoke
        result = invoker.invoke(invocation);
        if (result.hasException()) {
            exception = result.getException();
        }
        // the case on async client invocation
        Future<Object> future = rpcContext.getFuture();
        if (future instanceof FutureAdapter) {
            deferFinish = ensureSpanFinishes(future, invocation, invoker);
        }
        return result;
    } catch (RpcException e) {
        exception = e;
        throw e;
    } catch (Throwable t) {
        exception = t;
        throw new RpcException(t);
    } finally {
        if (exception != null) {
            // finish span on exception, delay to clear tl in handleError
            handleError(exception, null);
        } else {
            // sync invoke
            if (isOneWay || !deferFinish) {
                dubboConsumerSofaTracer.clientReceive(resultCode);
            } else {
                // to clean SofaTraceContext
                SofaTraceContext sofaTraceContext = SofaTraceContextHolder
                    .getSofaTraceContext();
                SofaTracerSpan clientSpan = sofaTraceContext.pop();
                if (clientSpan != null) {
                    // Record client send event
                    sofaTracerSpan.log(LogData.CLIENT_SEND_EVENT_VALUE);
                }
                // cache the current span
                TracerSpanMap.put(getTracerSpanMapKey(invoker), sofaTracerSpan);
                if (clientSpan != null && clientSpan.getParentSofaTracerSpan() != null) {
                    //restore parent
                    sofaTraceContext.push(clientSpan.getParentSofaTracerSpan());
                }
            }
        }
    }
}
 
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
    }
  }
}