Search in sources :

Example 1 with StatMapKey

use of com.alipay.common.tracer.core.reporter.stat.model.StatMapKey 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 2 with StatMapKey

use of com.alipay.common.tracer.core.reporter.stat.model.StatMapKey in project sofa-rpc by sofastack.

the class AbstractRpcStatJsonReporter method doReportStat.

/**
 * 统计一次 span
 * @param sofaTracerSpan 被统计的一次 span
 */
@Override
public void doReportStat(SofaTracerSpan sofaTracerSpan) {
    // tags
    Map<String, String> tagsWithStr = sofaTracerSpan.getTagsWithStr();
    StatMapKey statKey = new StatMapKey();
    String fromApp = getFromApp(tagsWithStr);
    String toApp = getToApp(tagsWithStr);
    String zone = getZone(tagsWithStr);
    // service name
    String serviceName = tagsWithStr.get(RpcSpanTags.SERVICE);
    // method name
    String methodName = tagsWithStr.get(RpcSpanTags.METHOD);
    statKey.setKey(buildString(new String[] { fromApp, toApp, serviceName, methodName }));
    String resultCode = tagsWithStr.get(RpcSpanTags.RESULT_CODE);
    statKey.setResult(isSuccess(resultCode) ? "Y" : "N");
    statKey.setEnd(buildString(new String[] { getLoadTestMark(sofaTracerSpan), zone }));
    statKey.setLoadTest(TracerUtils.isLoadTest(sofaTracerSpan));
    statKey.addKey(RpcSpanTags.LOCAL_APP, tagsWithStr.get(RpcSpanTags.LOCAL_APP));
    statKey.addKey(RpcSpanTags.REMOTE_APP, tagsWithStr.get(RpcSpanTags.REMOTE_APP));
    statKey.addKey(RpcSpanTags.SERVICE, serviceName);
    statKey.addKey(RpcSpanTags.METHOD, methodName);
    // 次数和耗时,最后一个耗时是单独打印的字段
    long duration = sofaTracerSpan.getEndTime() - sofaTracerSpan.getStartTime();
    long[] values = new long[] { 1, duration };
    this.addStat(statKey, values);
}
Also used : StatMapKey(com.alipay.common.tracer.core.reporter.stat.model.StatMapKey)

Example 3 with StatMapKey

use of com.alipay.common.tracer.core.reporter.stat.model.StatMapKey in project sofa-rpc by sofastack.

the class AbstractRpcStatJsonReporter method print.

@Override
public void print(StatKey statKey, long[] values) {
    if (this.isClosePrint.get()) {
        // 关闭统计日志输出
        return;
    }
    StatMapKey statMapKey = (StatMapKey) statKey;
    buffer.reset();
    buffer.appendBegin("time", Timestamp.currentTime());
    buffer.append("stat.key", this.statKeySplit(statMapKey));
    buffer.append("count", values[0]);
    buffer.append("total.cost.milliseconds", values[1]);
    buffer.append("success", statMapKey.getResult());
    buffer.appendEnd();
    try {
        if (appender instanceof LoadTestAwareAppender) {
            ((LoadTestAwareAppender) appender).append(buffer.toString(), statKey.isLoadTest());
        } else {
            appender.append(buffer.toString());
        }
        // 这里强制刷一次
        appender.flush();
    } catch (Throwable t) {
        SelfLog.error("统计日志<" + statTracerName + ">输出异常", t);
    }
}
Also used : StatMapKey(com.alipay.common.tracer.core.reporter.stat.model.StatMapKey) LoadTestAwareAppender(com.alipay.common.tracer.core.appender.file.LoadTestAwareAppender)

Aggregations

StatMapKey (com.alipay.common.tracer.core.reporter.stat.model.StatMapKey)3 SofaTracer (com.alipay.common.tracer.core.SofaTracer)1 LoadTestAwareAppender (com.alipay.common.tracer.core.appender.file.LoadTestAwareAppender)1 Reporter (com.alipay.common.tracer.core.reporter.facade.Reporter)1 StatKey (com.alipay.common.tracer.core.reporter.stat.model.StatKey)1 StatValues (com.alipay.common.tracer.core.reporter.stat.model.StatValues)1 ProviderInfo (com.alipay.sofa.rpc.client.ProviderInfo)1 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)1 SofaRequest (com.alipay.sofa.rpc.core.request.SofaRequest)1 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)1 Tracer (com.alipay.sofa.rpc.tracer.Tracer)1 RpcSofaTracer (com.alipay.sofa.rpc.tracer.sofatracer.RpcSofaTracer)1 MemoryReporterImpl (com.alipay.sofa.rpc.tracer.sofatracer.factory.MemoryReporterImpl)1 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1