Search in sources :

Example 1 with Reporter

use of com.alipay.common.tracer.core.reporter.facade.Reporter in project sofa-rpc by sofastack.

the class RestTracerTest method testRestTracer.

@Test
public void testRestTracer() throws InterruptedException, IOException {
    Reporter clientReporter = reflectToTracer();
    memoryReporter = (MemoryReporterImpl) clientReporter;
    ServerConfig restServer = new ServerConfig().setPort(8583).setProtocol(RpcConstants.PROTOCOL_TYPE_REST);
    List<ServerConfig> servers = new ArrayList<ServerConfig>(2);
    servers.add(restServer);
    ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setRegister(false).setServer(servers);
    providerConfig.export();
    // rest服务
    ConsumerConfig<RestService> consumerConfigRest = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setDirectUrl("rest://127.0.0.1:8583").setTimeout(1000).setApplication(new ApplicationConfig().setAppName("TestClientRest"));
    final RestService restServiceRest = consumerConfigRest.refer();
    restServiceRest.get("test");
    final int times = 10;
    final CountDownLatch latch = new CountDownLatch(times);
    final AtomicInteger success = new AtomicInteger(0);
    for (int i = 0; i < times; i++) {
        new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    for (int j = 0; j < times; j++) {
                        final String ok_rest = restServiceRest.get("ok_rest");
                        Assert.assertEquals("serverok_rest", ok_rest);
                        success.incrementAndGet();
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                } finally {
                    latch.countDown();
                }
            }
        }).start();
    }
    latch.await();
    Assert.assertEquals(times * times, success.get());
    TimeUnit.SECONDS.sleep(5);
    List<String> clientDigestContents = memoryReporter.getClientDigestHolder();
    List<String> serverDigestContents = memoryReporter.getServerDigestHolder();
    List<JSONObject> clientDigest = TracerChecker.convertContents2Json(clientDigestContents);
    List<String> clientTraceIds = readTraceId(clientDigest);
    List<JSONObject> serverDigest = TracerChecker.convertContents2Json(serverDigestContents);
    List<String> serverTraceIds = readTraceId(serverDigest);
    Assert.assertTrue(CommonUtils.isNotEmpty(clientTraceIds));
    Assert.assertTrue(CommonUtils.isNotEmpty(serverTraceIds));
    HashSet<String> hashSet = new HashSet<String>(200);
    for (String clientTraceId : clientTraceIds) {
        // will not duplicate
        Assert.assertTrue(!hashSet.contains(clientTraceId));
        hashSet.add(clientTraceId);
        Assert.assertTrue(serverTraceIds.contains(clientTraceId));
    }
    // validate one rpc server and rpc client field
    boolean result = TracerChecker.validateTracerDigest(clientDigest.get(0), "client", RpcConstants.PROTOCOL_TYPE_REST);
    Assert.assertTrue(result);
    result = TracerChecker.validateTracerDigest(serverDigest.get(0), "server", RpcConstants.PROTOCOL_TYPE_REST);
    Assert.assertTrue(result);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Reporter(com.alipay.common.tracer.core.reporter.facade.Reporter) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) JSONObject(com.alibaba.fastjson.JSONObject) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 2 with Reporter

use of com.alipay.common.tracer.core.reporter.facade.Reporter in project sofa-rpc by sofastack.

the class RestTracerTest method reflectToTracer.

/**
 * reflect to tracer
 *
 * @return
 */
protected Reporter reflectToTracer() {
    try {
        Tracer rpcSofaTracer = Tracers.getTracer();
        tracerField = RpcSofaTracer.class.getDeclaredField("sofaTracer");
        tracerField.setAccessible(true);
        // OpenTracing tracer 标准实现
        tracer = (SofaTracer) tracerField.get(rpcSofaTracer);
        Reporter tempReport = tracer.getClientReporter();
        clientReporterField = SofaTracer.class.getDeclaredField("clientReporter");
        clientReporterField.setAccessible(true);
        serverReporterField = SofaTracer.class.getDeclaredField("serverReporter");
        serverReporterField.setAccessible(true);
        if (tempReport instanceof DiskReporterImpl) {
            diskReporter = (DiskReporterImpl) tempReport;
            assertNotNull(diskReporter);
            memoryReporter = new MemoryReporterImpl(null, null, null, null, diskReporter.getStatReporter());
            clientReporterField.set(tracer, memoryReporter);
            serverReporterField.set(tracer, memoryReporter);
        } else {
            memoryReporter = (MemoryReporterImpl) tempReport;
        }
    // 否则说明已经是 memory 了.主要是本地
    } catch (Exception e) {
        e.printStackTrace();
    }
    return memoryReporter;
}
Also used : MemoryReporterImpl(com.alipay.sofa.rpc.tracer.sofatracer.factory.MemoryReporterImpl) DiskReporterImpl(com.alipay.common.tracer.core.reporter.digest.DiskReporterImpl) RpcSofaTracer(com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer) Tracer(com.alipay.sofa.rpc.tracer.Tracer) SofaTracer(com.alipay.common.tracer.core.SofaTracer) RpcSofaTracer(com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer) Reporter(com.alipay.common.tracer.core.reporter.facade.Reporter) IOException(java.io.IOException) SofaTracer(com.alipay.common.tracer.core.SofaTracer) RpcSofaTracer(com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer)

Example 3 with Reporter

use of com.alipay.common.tracer.core.reporter.facade.Reporter in project sofa-rpc by sofastack.

the class RpcSofaTracer method generateReporter.

protected Reporter generateReporter(SofaTracerStatisticReporter statReporter, RpcTracerLogEnum digestRpcTracerLogEnum, SpanEncoder<SofaTracerSpan> spanEncoder) {
    // 构造摘要实例
    String digestLog = digestRpcTracerLogEnum.getDefaultLogName();
    String digestRollingPolicy = SofaTracerConfiguration.getRollingPolicy(digestRpcTracerLogEnum.getRollingKey());
    String digestLogReserveConfig = SofaTracerConfiguration.getLogReserveConfig(digestRpcTracerLogEnum.getLogReverseKey());
    // 构造实例
    Reporter reporter = ReporterFactory.build(digestLog, digestRollingPolicy, digestLogReserveConfig, spanEncoder, statReporter);
    return reporter;
}
Also used : SofaTracerStatisticReporter(com.alipay.common.tracer.core.reporter.stat.SofaTracerStatisticReporter) RpcServerStatJsonReporter(com.alipay.sofa.rpc.tracer.sofatracer.log.stat.RpcServerStatJsonReporter) RpcClientStatJsonReporter(com.alipay.sofa.rpc.tracer.sofatracer.log.stat.RpcClientStatJsonReporter) Reporter(com.alipay.common.tracer.core.reporter.facade.Reporter)

Example 4 with Reporter

use of com.alipay.common.tracer.core.reporter.facade.Reporter in project sofa-rpc by sofastack.

the class RpcClientStatTest method testClientStat.

@Test
@Ignore
public void testClientStat() {
    try {
        Tracer rpcSofaTracer = Tracers.getTracer();
        Field tracerField = null;
        try {
            tracerField = RpcSofaTracer.class.getDeclaredField("sofaTracer");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        tracerField.setAccessible(true);
        SofaTracer tracer = null;
        // OpenTracing tracer 标准实现
        try {
            tracer = (SofaTracer) tracerField.get(rpcSofaTracer);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        Reporter clientReporter = tracer.getClientReporter();
        assertNotNull(clientReporter);
        assertTrue(clientReporter instanceof MemoryReporterImpl);
        memoryReporter = (MemoryReporterImpl) clientReporter;
        final SofaRequest request = new SofaRequest();
        request.setInterfaceName("a");
        request.setTargetServiceUniqueName("app.service:1.0");
        request.setMethodName("method");
        RpcInternalContext context = RpcInternalContext.getContext();
        context.setAttachment(RpcConstants.INTERNAL_KEY_APP_NAME, "client");
        // this will not be used, only in real invoke
        final ProviderInfo providerInfo = new ProviderInfo();
        providerInfo.setStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME, "server");
        context.setProviderInfo(providerInfo);
        for (int i = 0; i < 10; i++) {
            rpcSofaTracer.startRpc(request);
            rpcSofaTracer.clientBeforeSend(request);
            final SofaResponse response = new SofaResponse();
            response.setAppResponse("b");
            rpcSofaTracer.clientReceived(request, response, null);
        }
        Map<StatKey, StatValues> datas = memoryReporter.getStoreDatas();
        LOGGER.info("1" + datas);
        Assert.assertEquals(1, datas.size());
        for (Map.Entry entry : datas.entrySet()) {
            final StatMapKey key = (StatMapKey) entry.getKey();
            final StatValues value = (StatValues) entry.getValue();
            Assert.assertEquals("client,,app.service:1.0,method", key.getKey());
            Assert.assertEquals(10, value.getCurrentValue()[0]);
        }
        request.setTargetServiceUniqueName("app.service:2.0");
        for (int i = 0; i < 20; i++) {
            rpcSofaTracer.startRpc(request);
            rpcSofaTracer.clientBeforeSend(request);
            final SofaResponse response = new SofaResponse();
            response.setAppResponse("b");
            rpcSofaTracer.clientReceived(request, response, null);
        }
        LOGGER.info("2" + datas);
        int i = 0;
        for (Map.Entry entry : datas.entrySet()) {
            if (i == 0) {
                continue;
            }
            final StatMapKey key = (StatMapKey) entry.getKey();
            final StatValues value = (StatValues) entry.getValue();
            Assert.assertEquals("client,,app.service:2.0,method", key.getKey());
            Assert.assertEquals(20, value.getCurrentValue()[0]);
        }
        Assert.assertEquals(2, datas.size());
    } catch (Throwable e) {
        e.printStackTrace();
        Assert.assertTrue(false);
    }
}
Also used : MemoryReporterImpl(com.alipay.sofa.rpc.tracer.sofatracer.factory.MemoryReporterImpl) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Tracer(com.alipay.sofa.rpc.tracer.Tracer) SofaTracer(com.alipay.common.tracer.core.SofaTracer) RpcSofaTracer(com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer) Reporter(com.alipay.common.tracer.core.reporter.facade.Reporter) RpcInternalContext(com.alipay.sofa.rpc.context.RpcInternalContext) StatKey(com.alipay.common.tracer.core.reporter.stat.model.StatKey) Field(java.lang.reflect.Field) StatMapKey(com.alipay.common.tracer.core.reporter.stat.model.StatMapKey) ProviderInfo(com.alipay.sofa.rpc.client.ProviderInfo) RpcSofaTracer(com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer) StatValues(com.alipay.common.tracer.core.reporter.stat.model.StatValues) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse) Map(java.util.Map) SofaTracer(com.alipay.common.tracer.core.SofaTracer) RpcSofaTracer(com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with Reporter

use of com.alipay.common.tracer.core.reporter.facade.Reporter in project sofa-boot by alipay.

the class SofaTracerAutoConfiguration method sofaTracer.

@Bean
@ConditionalOnMissingBean
public Tracer sofaTracer(SofaTracerProperties sofaTracerProperties) throws Exception {
    String reporterName = sofaTracerProperties.getReporterName();
    if (StringUtils.isNotBlank(reporterName)) {
        Reporter reporter = (Reporter) Class.forName(reporterName).newInstance();
        Sampler sampler = SamplerFactory.getSampler();
        return new FlexibleTracer(sampler, reporter);
    }
    Tracer tracer = new FlexibleTracer();
    return tracer;
}
Also used : Tracer(io.opentracing.Tracer) FlexibleTracer(com.alipay.sofa.tracer.plugin.flexible.FlexibleTracer) Reporter(com.alipay.common.tracer.core.reporter.facade.Reporter) Sampler(com.alipay.common.tracer.core.samplers.Sampler) FlexibleTracer(com.alipay.sofa.tracer.plugin.flexible.FlexibleTracer) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

Reporter (com.alipay.common.tracer.core.reporter.facade.Reporter)7 SofaTracer (com.alipay.common.tracer.core.SofaTracer)3 Test (org.junit.Test)3 DiskReporterImpl (com.alipay.common.tracer.core.reporter.digest.DiskReporterImpl)2 Sampler (com.alipay.common.tracer.core.samplers.Sampler)2 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 Tracer (com.alipay.sofa.rpc.tracer.Tracer)2 RpcSofaTracer (com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer)2 MemoryReporterImpl (com.alipay.sofa.rpc.tracer.sofatracer.factory.MemoryReporterImpl)2 RpcClientStatJsonReporter (com.alipay.sofa.rpc.tracer.sofatracer.log.stat.RpcClientStatJsonReporter)2 RpcServerStatJsonReporter (com.alipay.sofa.rpc.tracer.sofatracer.log.stat.RpcServerStatJsonReporter)2 FlexibleTracer (com.alipay.sofa.tracer.plugin.flexible.FlexibleTracer)2 Tracer (io.opentracing.Tracer)2 Field (java.lang.reflect.Field)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 Bean (org.springframework.context.annotation.Bean)2 JSONObject (com.alibaba.fastjson.JSONObject)1 AsyncCommonDigestAppenderManager (com.alipay.common.tracer.core.appender.manager.AsyncCommonDigestAppenderManager)1 SofaTracerStatisticReporter (com.alipay.common.tracer.core.reporter.stat.SofaTracerStatisticReporter)1