Java Code Examples for com.alipay.sofa.rpc.core.request.SofaRequest#getRequestProp()

The following examples show how to use com.alipay.sofa.rpc.core.request.SofaRequest#getRequestProp() . 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: SofaRpcProviderInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    SofaRequest sofaRequest = (SofaRequest) allArguments[0];

    AbstractSpan span = null;

    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        final String headKey = next.getHeadKey();
        final Object attachment = sofaRequest.getRequestProp(SKYWALKING_PREFIX + headKey);
        if (attachment != null) {
            next.setHeadValue(attachment.toString());
        } else {
            next.setHeadValue("");
        }
    }
    span = ContextManager.createEntrySpan(generateViewPoint(sofaRequest), contextCarrier);

    span.setComponent(ComponentsDefine.SOFARPC);
    SpanLayer.asRPCFramework(span);
}
 
Example 2
Source File: BoltClientTransport.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
protected InvokeContext createInvokeContext(SofaRequest request) {
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, request.getSerializeType());
    invokeContext.put(RemotingConstants.HEAD_TARGET_SERVICE, request.getTargetServiceUniqueName());
    invokeContext.put(RemotingConstants.HEAD_METHOD_NAME, request.getMethodName());
    String genericType = (String) request.getRequestProp(RemotingConstants.HEAD_GENERIC_TYPE);
    if (genericType != null) {
        invokeContext.put(RemotingConstants.HEAD_GENERIC_TYPE, genericType);
    }
    return invokeContext;
}
 
Example 3
Source File: BaggageResolver.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 从请求里获取透传数据
 *
 * @param context RpcInvokeContext
 * @param request 请求
 * @param init    传入上下文为空时,是否初始化
 */
public static void pickupFromRequest(RpcInvokeContext context, SofaRequest request, boolean init) {
    if (context == null && !init) {
        return;
    }
    // 解析请求 
    Map<String, String> requestBaggage = (Map<String, String>) request
        .getRequestProp(RemotingConstants.RPC_REQUEST_BAGGAGE);
    if (CommonUtils.isNotEmpty(requestBaggage)) {
        if (context == null) {
            context = RpcInvokeContext.getContext();
        }
        context.putAllRequestBaggage(requestBaggage);
    }
}
 
Example 4
Source File: SofaRpcUtils.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
public static String getApplicationName(SofaRequest request) {
    String appName = (String) request.getRequestProp(RemotingConstants.HEAD_APP_NAME);
    return appName == null ? "" : appName;
}
 
Example 5
Source File: RpcSofaTracer.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Override
public void serverReceived(SofaRequest request) {

    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();

    Map<String, String> tags = new HashMap<String, String>();
    //server tags 必须设置
    tags.put(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);

    String spanStrs = (String) request.getRequestProp(RemotingConstants.NEW_RPC_TRACE_NAME);
    SofaTracerSpanContext spanContext = null;
    if (StringUtils.isBlank(spanStrs)) {
        //老
        Object oldInstanceMap = request.getRequestProp(RemotingConstants.RPC_TRACE_NAME);
        spanContext = this.saveSpanContextAndTags(tags, oldInstanceMap);
    } else {
        //新
        spanContext = SofaTracerSpanContext.deserializeFromString(spanStrs);
    }
    SofaTracerSpan serverSpan;
    //使用客户端的进行初始化,如果上游没有,需要新建
    if (spanContext == null) {
        serverSpan = (SofaTracerSpan) this.sofaTracer.buildSpan(request.getInterfaceName())
            .asChildOf(spanContext)
            .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER)
            .start();
    } else {
        //有的话,需要new,采样会正确
        serverSpan = new SofaTracerSpan(this.sofaTracer, System.currentTimeMillis(),
            request.getInterfaceName()
            , spanContext, tags);
    }
    //重新获取
    spanContext = serverSpan.getSofaTracerSpanContext();

    // Record server receive event
    serverSpan.log(LogData.SERVER_RECV_EVENT_VALUE);
    //放到线程上下文
    sofaTraceContext.push(serverSpan);
    //rpc 上下文
    if (RpcInternalContext.isAttachmentEnable()) {
        RpcInternalContext context = RpcInternalContext.getContext();
        context.setAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID, spanContext.getTraceId());
        context.setAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID, spanContext.getSpanId());
    }
}