Search in sources :

Example 1 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class TripleHessianInvokeTest method testInvoke.

@Test
public void testInvoke() throws InterruptedException {
    RpcRunningState.setDebugMode(true);
    ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
    ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
    int port = getPort();
    ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
    TripleHessianInterfaceImpl ref = new TripleHessianInterfaceImpl();
    ProviderConfig<TripleHessianInterface> providerConfig = getProviderConfig().setApplication(serverApp).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(TripleHessianInterface.class.getName()).setRef(ref).setServer(serverConfig).setRegister(false);
    providerConfig.export();
    ConsumerConfig<TripleHessianInterface> consumerConfig = new ConsumerConfig<TripleHessianInterface>();
    consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setRegister(false).setApplication(clientApp);
    TripleHessianInterface helloService = consumerConfig.refer();
    LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
    helloService.call();
    Assert.assertEquals("call", ref.getFlag());
    // test Pressure Mark
    boolean isLoadTest = helloService.testPressureMark("name");
    Assert.assertFalse(isLoadTest);
    SofaTracerSpan currentSpan = SofaTraceContextHolder.getSofaTraceContext().getCurrentSpan();
    Map<String, String> bizBaggage = currentSpan.getSofaTracerSpanContext().getBizBaggage();
    bizBaggage.put("mark", "T");
    Assert.assertTrue(TracerUtils.isLoadTest(currentSpan));
    isLoadTest = helloService.testPressureMark("name");
    Assert.assertTrue(isLoadTest);
    String s = helloService.call1();
    Assert.assertEquals("call1", ref.getFlag());
    Assert.assertEquals("call1", s);
    Request request = new Request();
    int age = RandomUtils.nextInt();
    request.setAge(age);
    String call2 = "call2";
    request.setFlag(call2);
    Response response = helloService.call2(request);
    Assert.assertEquals(age, response.getAge());
    Assert.assertEquals(call2, response.getFlag());
    Response response1 = helloService.call2(null);
    Assert.assertNull(response1);
    providerConfig.unExport();
    serverConfig.destroy();
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) Test(org.junit.Test)

Example 2 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class TripleHessianInvokeTest method before.

@Before
public void before() {
    SofaTracerSpan span = (SofaTracerSpan) tracer.buildSpan("test").start();
    SofaTraceContextHolder.getSofaTraceContext().push(span);
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) Before(org.junit.Before)

Example 3 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class RestTracerAdapter method serverSend.

/**
 * 适配服务端serverSend
 */
public static void serverSend(NettyHttpResponse response, Throwable throwable) {
    try {
        SofaRequest sofaRequest = new SofaRequest();
        SofaResponse sofaResponse = new SofaResponse();
        if (response == null) {
            sofaResponse.setErrorMsg("rest path ends with /favicon.ico");
        } else if (throwable != null) {
            if (response.getStatus() == 500) {
                sofaResponse.setAppResponse(throwable);
            } else {
                sofaResponse.setErrorMsg(throwable.getMessage());
            }
            Object method = RpcInternalContext.getContext().getAttachment(METHOD_TYPE_STRING);
            if (method != null) {
                Class[] parameterTypes = ((Method) method).getParameterTypes();
                String[] methodTypeString = new String[parameterTypes.length];
                for (int i = 0; i < methodTypeString.length; i++) {
                    methodTypeString[i] = (parameterTypes[i].getName());
                }
                sofaRequest.setMethodArgSigs(methodTypeString);
            }
        }
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
        RpcInternalContext context = RpcInternalContext.getContext();
        if (serverSpan != null) {
            serverSpan.setTag(RpcSpanTags.SERVER_BIZ_TIME, (Number) context.getAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE));
        }
        RestBaggageItemsHandler.encodeBaggageItemsToResponse(response, sofaResponse);
        Tracers.serverSend(sofaRequest, sofaResponse, null);
    } catch (Throwable t) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("the process of rest tracer server send occur error ", t);
        }
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 4 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class RestTracerAdapter method beforeSend.

/**
 * 存入tracer信息
 *
 * @param requestContext ClientRequestContext
 */
public static void beforeSend(ClientRequestContext requestContext) {
    // tracer信息放入request 发到服务端
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
    RpcInternalContext context = RpcInternalContext.getContext();
    if (clientSpan != null) {
        requestContext.getHeaders().add(RemotingConstants.NEW_RPC_TRACE_NAME, clientSpan.getSofaTracerSpanContext().serializeSpanContext());
    }
    // 客户端发送自己的应用名
    String appName = (String) context.getAttachment(INTERNAL_KEY_APP_NAME);
    if (appName != null) {
        requestContext.getHeaders().add(RemotingConstants.HEAD_APP_NAME, appName);
    }
    RestBaggageItemsHandler.encodeBaggageItemToRequest(requestContext.getHeaders());
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext)

Example 5 with SofaTracerSpan

use of com.alipay.common.tracer.core.span.SofaTracerSpan in project sofa-rpc by sofastack.

the class TripleTracerAdapter method serverReceived.

/**
 * 适配服务端serverReceived
 */
public static void serverReceived(SofaRequest sofaRequest, ServerServiceDefinition serverServiceDefinition, final ServerCall call, Metadata requestHeaders) {
    try {
        if (sofaRequest == null) {
            sofaRequest = new SofaRequest();
        }
        Map<String, String> traceMap = new HashMap<String, String>();
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE)) {
            sofaRequest.setTargetServiceUniqueName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE));
            sofaRequest.setInterfaceName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_SERVICE));
        } else {
            String serviceName = serverServiceDefinition.getServiceDescriptor().getName();
            sofaRequest.setTargetServiceUniqueName(serviceName);
            sofaRequest.setInterfaceName(serviceName);
        }
        final String serviceName = call.getMethodDescriptor().getServiceName();
        sofaRequest.setTargetServiceUniqueName(serviceName);
        sofaRequest.setInterfaceName(serviceName);
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TARGET_APP)) {
            sofaRequest.setTargetAppName(requestHeaders.get(TripleHeadKeys.HEAD_KEY_TARGET_APP));
        }
        // 先取兼容的
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID)) {
            traceMap.put(TracerCompatibleConstants.TRACE_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID));
        } else if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_TRACE_ID)) {
            traceMap.put(TracerCompatibleConstants.TRACE_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_TRACE_ID));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID)) {
            traceMap.put(TracerCompatibleConstants.RPC_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID));
        } else if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_RPC_ID)) {
            traceMap.put(TracerCompatibleConstants.RPC_ID_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_RPC_ID));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION)) {
            RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION));
        } else {
            RpcInvokeContext.getContext().put(TripleContants.SOFA_UNIQUE_ID, "");
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SAMP_TYPE)) {
            traceMap.put(TracerCompatibleConstants.SAMPLING_MARK, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SAMP_TYPE));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_CURRENT_APP)) {
            sofaRequest.addRequestProp(HEAD_APP_NAME, requestHeaders.get(TripleHeadKeys.HEAD_KEY_CURRENT_APP));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE)) {
            sofaRequest.addRequestProp(RemotingConstants.HEAD_PROTOCOL, requestHeaders.get(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE)) {
            sofaRequest.addRequestProp(RemotingConstants.HEAD_INVOKE_TYPE, requestHeaders.get(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE)) {
            traceMap.put(TracerCompatibleConstants.PEN_ATTRS_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE));
        }
        if (requestHeaders.containsKey(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE)) {
            traceMap.put(TracerCompatibleConstants.PEN_SYS_ATTRS_KEY, requestHeaders.get(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE));
        }
        if (!traceMap.isEmpty()) {
            sofaRequest.addRequestProp(RemotingConstants.RPC_TRACE_NAME, traceMap);
        }
        if (EventBus.isEnable(ServerReceiveEvent.class)) {
            EventBus.post(new ServerReceiveEvent(sofaRequest));
        }
        String remoteIp = "";
        SocketAddress socketAddress = call.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
        if (socketAddress instanceof InetSocketAddress) {
            remoteIp = ((InetSocketAddress) socketAddress).getHostName();
        }
        String methodName;
        String fullMethodName = call.getMethodDescriptor().getFullMethodName();
        methodName = StringUtils.substringAfter(fullMethodName, serviceName + "/");
        sofaRequest.setMethodName(methodName);
        SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
        SofaTracerSpan serverSpan = sofaTraceContext.getCurrentSpan();
        if (serverSpan != null) {
            // FIXME modify the dep relation
            serverSpan.setTag("service", sofaRequest.getTargetServiceUniqueName());
            serverSpan.setTag("method", methodName);
            // 从请求里获取ConsumerTracerFilter额外传递的信息
            serverSpan.setTag("remote.app", (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
            serverSpan.setTag("protocol", RpcConstants.PROTOCOL_TYPE_TRIPLE);
            serverSpan.setTag("remote.ip", remoteIp);
        }
    } catch (Throwable e) {
        LOGGER.warn("triple serverReceived tracer error", e);
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) HashMap(java.util.HashMap) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) InetSocketAddress(java.net.InetSocketAddress) ServerReceiveEvent(com.alipay.sofa.rpc.event.ServerReceiveEvent) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)35 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)25 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)12 Test (org.junit.Test)12 SofaTracerSpanContext (com.alipay.common.tracer.core.context.span.SofaTracerSpanContext)6 RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)6 HashMap (java.util.HashMap)6 Metadata (io.grpc.Metadata)4 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)3 Context (io.grpc.Context)3 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)2 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 FlexibleTracer (com.alipay.sofa.tracer.plugin.flexible.FlexibleTracer)2 InetSocketAddress (java.net.InetSocketAddress)2 Before (org.junit.Before)2 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)1 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)1 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)1