use of com.alipay.common.tracer.core.SofaTracer 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;
}
use of com.alipay.common.tracer.core.SofaTracer 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);
}
}
use of com.alipay.common.tracer.core.SofaTracer in project sofa-rpc by sofastack.
the class RpcSofaTracerTest method testTracerInit.
@Test
public void testTracerInit() throws Exception {
try {
// 注册 digest
AsyncCommonDigestAppenderManager asyncDigestManager = SofaTracerDigestReporterAsyncManager.getSofaTracerDigestReporterAsyncManager();
Field tracerField = RpcSofaTracer.class.getDeclaredField("sofaTracer");
tracerField.setAccessible(true);
// OpenTracing tracer 标准实现
SofaTracer tracer = (SofaTracer) tracerField.get(this.rpcSofaTracer);
assertTrue(tracer != null);
Reporter clientReporter = tracer.getClientReporter();
assertNotNull(clientReporter);
assertTrue(clientReporter instanceof DiskReporterImpl);
DiskReporterImpl clientDisk = (DiskReporterImpl) clientReporter;
assertEquals(clientDisk.getDigestReporterType(), RpcTracerLogEnum.RPC_CLIENT_DIGEST.getDefaultLogName());
assertTrue(clientDisk.getStatReporter() instanceof RpcClientStatJsonReporter);
// 修改为 lazy 初始化了
// assertFalse(asyncDigestManager.isAppenderAndEncoderExist(clientDisk.getDigestReporterType()));
SofaRequest sofaRequest = new SofaRequest();
rpcSofaTracer.startRpc(sofaRequest);
rpcSofaTracer.clientBeforeSend(sofaRequest);
rpcSofaTracer.clientReceived(sofaRequest, new SofaResponse(), null);
// lazy 应该注册成功了
assertTrue(asyncDigestManager.isAppenderAndEncoderExist(clientDisk.getDigestReporterType()));
// print
TimeUnit.SECONDS.sleep(1);
Reporter serverReporter = tracer.getServerReporter();
assertTrue(serverReporter instanceof DiskReporterImpl);
assertNotNull(serverReporter);
DiskReporterImpl serverDisk = (DiskReporterImpl) serverReporter;
assertEquals(serverDisk.getDigestReporterType(), RpcTracerLogEnum.RPC_SERVER_DIGEST.getDefaultLogName());
// assertFalse(asyncDigestManager.isAppenderAndEncoderExist(serverDisk.getDigestReporterType()));
rpcSofaTracer.serverReceived(sofaRequest);
rpcSofaTracer.serverSend(sofaRequest, new SofaResponse(), null);
// print
TimeUnit.SECONDS.sleep(1);
assertTrue(asyncDigestManager.isAppenderAndEncoderExist(serverDisk.getDigestReporterType()));
assertTrue(serverDisk.getStatReporter() instanceof RpcServerStatJsonReporter);
} catch (Throwable e) {
e.printStackTrace();
assertTrue(false);
}
}
Aggregations