finished service manager and trace segment testcase
This commit is contained in:
parent
678ad01d49
commit
b78442d1c1
|
|
@ -77,6 +77,12 @@
|
|||
<version>${jetty.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.skywalking</groupId>
|
||||
<artifactId>apm-test-tools</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<extensions>
|
||||
|
|
|
|||
|
|
@ -2,19 +2,6 @@ package org.skywalking.apm.agent.core.context;
|
|||
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
|
||||
/**
|
||||
* {@link TracingContextListener} is a status change listener of {@link TracerContext}.
|
||||
* Add a {@link TracingContextListener} implementation through {@link TracerContext}
|
||||
* <p>
|
||||
* All this class's methods will be called concurrently. Make sure all implementations are thread-safe.
|
||||
* <p>
|
||||
* Created by wusheng on 2017/2/17.
|
||||
*/
|
||||
public interface TracingContextListener {
|
||||
/**
|
||||
* This method will be called, after the {@link TracerContext#finish()}
|
||||
*
|
||||
* @param traceSegment finished {@link TraceSegment}
|
||||
*/
|
||||
void afterFinished(TraceSegment traceSegment);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,98 @@
|
|||
package org.skywalking.apm.agent.core.boot;
|
||||
|
||||
import org.junit.Assert;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
import org.skywalking.apm.agent.core.context.ContextManager;
|
||||
import org.skywalking.apm.agent.core.context.IgnoredTracerContext;
|
||||
import org.skywalking.apm.agent.core.context.TracingContext;
|
||||
import org.skywalking.apm.agent.core.context.TracingContextListener;
|
||||
import org.skywalking.apm.agent.core.jvm.JVMService;
|
||||
import org.skywalking.apm.agent.core.remote.CollectorDiscoveryService;
|
||||
import org.skywalking.apm.agent.core.remote.GRPCChannelListener;
|
||||
import org.skywalking.apm.agent.core.remote.GRPCChannelManager;
|
||||
import org.skywalking.apm.agent.core.remote.TraceSegmentServiceClient;
|
||||
import org.skywalking.apm.agent.core.sampling.SamplingService;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author wusheng
|
||||
*/
|
||||
public class ServiceManagerTest {
|
||||
@Test
|
||||
public void testBoot() {
|
||||
public void testServiceDependencies() throws Exception {
|
||||
ServiceManager.INSTANCE.boot();
|
||||
ContextManager manager = ServiceManager.INSTANCE.findService(ContextManager.class);
|
||||
Assert.assertNotNull(manager);
|
||||
HashMap<Class, BootService> registryService = getFieldValue(ServiceManager.INSTANCE, "bootedServices");
|
||||
|
||||
assertThat(registryService.size(), is(6));
|
||||
|
||||
assertTraceSegmentServiceClient(ServiceManager.INSTANCE.findService(TraceSegmentServiceClient.class));
|
||||
assertContextManager(ServiceManager.INSTANCE.findService(ContextManager.class));
|
||||
assertCollectorDiscoveryService(ServiceManager.INSTANCE.findService(CollectorDiscoveryService.class));
|
||||
assertGRPCChannelManager(ServiceManager.INSTANCE.findService(GRPCChannelManager.class));
|
||||
assertSamplingService(ServiceManager.INSTANCE.findService(SamplingService.class));
|
||||
assertJVMService(ServiceManager.INSTANCE.findService(JVMService.class));
|
||||
|
||||
assertTracingContextListener();
|
||||
assertIgnoreTracingContextListener();
|
||||
}
|
||||
|
||||
private void assertIgnoreTracingContextListener() throws Exception {
|
||||
List<TracingContextListener> LISTENERS = getFieldValue(IgnoredTracerContext.ListenerManager.class, "LISTENERS");
|
||||
assertThat(LISTENERS.size(), is(1));
|
||||
|
||||
assertThat(LISTENERS.contains(ServiceManager.INSTANCE.findService(ContextManager.class)), is(true));
|
||||
}
|
||||
|
||||
private void assertTracingContextListener() throws Exception {
|
||||
List<TracingContextListener> LISTENERS = getFieldValue(TracingContext.ListenerManager.class, "LISTENERS");
|
||||
assertThat(LISTENERS.size(), is(2));
|
||||
|
||||
assertThat(LISTENERS.contains(ServiceManager.INSTANCE.findService(ContextManager.class)), is(true));
|
||||
assertThat(LISTENERS.contains(ServiceManager.INSTANCE.findService(TraceSegmentServiceClient.class)), is(true));
|
||||
}
|
||||
|
||||
private void assertJVMService(JVMService service) {
|
||||
assertNotNull(service);
|
||||
}
|
||||
|
||||
private void assertGRPCChannelManager(GRPCChannelManager service) throws Exception {
|
||||
assertNotNull(service);
|
||||
|
||||
List<GRPCChannelListener> listeners = getFieldValue(service, "listeners");
|
||||
assertEquals(listeners.size(), 1);
|
||||
assertThat(listeners.get(0), is((GRPCChannelListener)ServiceManager.INSTANCE.
|
||||
findService(TraceSegmentServiceClient.class)));
|
||||
}
|
||||
|
||||
private void assertSamplingService(SamplingService service) {
|
||||
assertNotNull(service);
|
||||
}
|
||||
|
||||
private void assertCollectorDiscoveryService(CollectorDiscoveryService service) {
|
||||
assertNotNull(service);
|
||||
}
|
||||
|
||||
private void assertContextManager(ContextManager service) {
|
||||
assertNotNull(service);
|
||||
}
|
||||
|
||||
private void assertTraceSegmentServiceClient(TraceSegmentServiceClient service) {
|
||||
assertNotNull(service);
|
||||
}
|
||||
|
||||
private <T> T getFieldValue(Object instance, String fieldName) throws Exception {
|
||||
Field field = instance.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return (T)field.get(instance);
|
||||
}
|
||||
|
||||
private <T> T getFieldValue(Class clazz, String fieldName) throws Exception {
|
||||
Field field = clazz.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return (T)field.get(clazz);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,27 +1,22 @@
|
|||
package org.skywalking.apm.agent.core.conf;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.skywalking.apm.agent.core.logging.LogLevel;
|
||||
|
||||
/**
|
||||
* @author wusheng
|
||||
*/
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
public class SnifferConfigInitializerTest {
|
||||
|
||||
@Test
|
||||
public void testInitialize() {
|
||||
public void testLoadConfigFromJavaAgentDir() {
|
||||
System.setProperty("applicationCode", "testApp");
|
||||
System.setProperty("servers", "127.0.0.1:8090");
|
||||
SnifferConfigInitializer.initialize();
|
||||
|
||||
Assert.assertEquals("crmApp", Config.Agent.APPLICATION_CODE);
|
||||
Assert.assertEquals("127.0.0.1:8080", Config.Collector.SERVERS);
|
||||
|
||||
Assert.assertNotNull(Config.Logging.DIR);
|
||||
Assert.assertNotNull(Config.Logging.FILE_NAME);
|
||||
Assert.assertNotNull(Config.Logging.MAX_FILE_SIZE);
|
||||
Assert.assertNotNull(Config.Logging.FILE_NAME);
|
||||
Assert.assertEquals(LogLevel.INFO, Config.Logging.LEVEL);
|
||||
assertThat(Config.Agent.APPLICATION_CODE, is("testApp"));
|
||||
assertThat(Config.Collector.SERVERS, is("127.0.0.1:8090"));
|
||||
assertThat(Config.Logging.LEVEL, is(LogLevel.INFO));
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
|
|
|
|||
|
|
@ -0,0 +1,195 @@
|
|||
package org.skywalking.apm.agent.core.context;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.skywalking.apm.agent.core.boot.ServiceManager;
|
||||
import org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
|
||||
import org.skywalking.apm.agent.core.context.tag.Tags;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.LogDataEntity;
|
||||
import org.skywalking.apm.agent.core.context.trace.SpanLayer;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegmentRef;
|
||||
import org.skywalking.apm.agent.core.dictionary.DictionaryUtil;
|
||||
import org.skywalking.apm.agent.core.dictionary.OperationNameDictionary;
|
||||
import org.skywalking.apm.agent.test.helper.AbstractTracingSpanHelper;
|
||||
import org.skywalking.apm.agent.test.helper.SegmentHelper;
|
||||
import org.skywalking.apm.agent.test.helper.TraceSegmentRefHelper;
|
||||
import org.skywalking.apm.agent.test.tools.SegmentStorage;
|
||||
import org.skywalking.apm.agent.test.tools.SegmentStoragePoint;
|
||||
import org.skywalking.apm.agent.test.tools.TracingSegmentRunner;
|
||||
import org.skywalking.apm.network.trace.component.ComponentsDefine;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(TracingSegmentRunner.class)
|
||||
public class ContextManagerTest {
|
||||
|
||||
@SegmentStoragePoint
|
||||
private SegmentStorage tracingData;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() {
|
||||
ServiceManager.INSTANCE.boot();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
RemoteDownstreamConfig.Agent.APPLICATION_ID = 1;
|
||||
RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID = 1;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createSpanWithInvalidateContextCarrier() {
|
||||
ContextCarrier contextCarrier = new ContextCarrier().deserialize("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/");
|
||||
|
||||
AbstractSpan firstEntrySpan = ContextManager.createEntrySpan("/testEntrySpan", contextCarrier);
|
||||
firstEntrySpan.setComponent(ComponentsDefine.TOMCAT);
|
||||
Tags.HTTP.METHOD.set(firstEntrySpan, "GET");
|
||||
Tags.URL.set(firstEntrySpan, "127.0.0.1:8080");
|
||||
SpanLayer.asHttp(firstEntrySpan);
|
||||
|
||||
ContextManager.stopSpan();
|
||||
|
||||
TraceSegment actualSegment = tracingData.getTraceSegments().get(0);
|
||||
assertNull(actualSegment.getRefs());
|
||||
|
||||
List<AbstractTracingSpan> spanList = SegmentHelper.getSpan(actualSegment);
|
||||
assertThat(spanList.size(), is(1));
|
||||
|
||||
AbstractTracingSpan actualEntrySpan = spanList.get(0);
|
||||
assertThat(actualEntrySpan.getOperationName(), is("/testEntrySpan"));
|
||||
assertThat(actualEntrySpan.getSpanId(), is(0));
|
||||
assertThat(AbstractTracingSpanHelper.getParentSpanId(actualEntrySpan), is(-1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMultipleEntrySpan() {
|
||||
ContextCarrier contextCarrier = new ContextCarrier().deserialize("S.1499176688384.581928182.80935.69.1|3|1|#192.168.1.8 :18002|#/portal/|T.1499176688386.581928182.80935.69.2");
|
||||
assertTrue(contextCarrier.isValid());
|
||||
|
||||
AbstractSpan firstEntrySpan = ContextManager.createEntrySpan("/testFirstEntry", contextCarrier);
|
||||
firstEntrySpan.setComponent(ComponentsDefine.TOMCAT);
|
||||
Tags.HTTP.METHOD.set(firstEntrySpan, "GET");
|
||||
Tags.URL.set(firstEntrySpan, "127.0.0.1:8080");
|
||||
SpanLayer.asHttp(firstEntrySpan);
|
||||
|
||||
AbstractSpan secondEntrySpan = ContextManager.createEntrySpan("/testSecondEntry", contextCarrier);
|
||||
secondEntrySpan.setComponent(ComponentsDefine.DUBBO);
|
||||
Tags.URL.set(firstEntrySpan, "dubbo://127.0.0.1:8080");
|
||||
SpanLayer.asRPCFramework(secondEntrySpan);
|
||||
|
||||
ContextCarrier injectContextCarrier = new ContextCarrier();
|
||||
AbstractSpan exitSpan = ContextManager.createExitSpan("/textExitSpan", injectContextCarrier, "127.0.0.1:12800");
|
||||
exitSpan.errorOccurred();
|
||||
exitSpan.log(new RuntimeException("exception"));
|
||||
exitSpan.setComponent(ComponentsDefine.HTTPCLIENT);
|
||||
|
||||
ContextManager.stopSpan();
|
||||
ContextManager.stopSpan();
|
||||
ContextManager.stopSpan();
|
||||
|
||||
assertThat(tracingData.getTraceSegments().size(), is(1));
|
||||
|
||||
TraceSegment actualSegment = tracingData.getTraceSegments().get(0);
|
||||
assertThat(actualSegment.getRefs().size(), is(1));
|
||||
|
||||
TraceSegmentRef ref = actualSegment.getRefs().get(0);
|
||||
assertThat(TraceSegmentRefHelper.getPeerHost(ref), is("192.168.1.8 :18002"));
|
||||
assertThat(ref.getOperationName(), is("/portal/"));
|
||||
assertThat(ref.getOperationId(), is(0));
|
||||
|
||||
List<AbstractTracingSpan> spanList = SegmentHelper.getSpan(actualSegment);
|
||||
assertThat(spanList.size(), is(2));
|
||||
|
||||
AbstractTracingSpan actualEntrySpan = spanList.get(1);
|
||||
assertThat(actualEntrySpan.getOperationName(), is("/testSecondEntry"));
|
||||
assertThat(actualEntrySpan.getSpanId(), is(0));
|
||||
assertThat(AbstractTracingSpanHelper.getParentSpanId(actualEntrySpan), is(-1));
|
||||
|
||||
AbstractTracingSpan actualExitSpan = spanList.get(0);
|
||||
assertThat(actualExitSpan.getOperationName(), is("/textExitSpan"));
|
||||
assertThat(actualExitSpan.getSpanId(), is(1));
|
||||
assertThat(AbstractTracingSpanHelper.getParentSpanId(actualExitSpan), is(0));
|
||||
|
||||
List<LogDataEntity> logs = AbstractTracingSpanHelper.getLogs(actualExitSpan);
|
||||
assertThat(logs.size(), is(1));
|
||||
assertThat(logs.get(0).getLogs().size(), is(4));
|
||||
|
||||
assertThat(injectContextCarrier.getSpanId(), is(1));
|
||||
assertThat(injectContextCarrier.getEntryOperationName(), is("#/portal/"));
|
||||
assertThat(injectContextCarrier.getApplicationId(), is(1));
|
||||
assertThat(injectContextCarrier.getPeerHost(), is("#127.0.0.1:12800"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createMultipleExitSpan() {
|
||||
AbstractSpan entrySpan = ContextManager.createEntrySpan("/testEntrySpan", null);
|
||||
entrySpan.setComponent(ComponentsDefine.TOMCAT);
|
||||
Tags.HTTP.METHOD.set(entrySpan, "GET");
|
||||
Tags.URL.set(entrySpan, "127.0.0.1:8080");
|
||||
SpanLayer.asHttp(entrySpan);
|
||||
|
||||
ContextCarrier firstExitSpanContextCarrier = new ContextCarrier();
|
||||
AbstractSpan firstExitSpan = ContextManager.createExitSpan("/testFirstExit", firstExitSpanContextCarrier, "127.0.0.1:8080");
|
||||
firstExitSpan.setComponent(ComponentsDefine.DUBBO);
|
||||
Tags.URL.set(firstExitSpan, "dubbo://127.0.0.1:8080");
|
||||
SpanLayer.asRPCFramework(firstExitSpan);
|
||||
|
||||
ContextCarrier secondExitSpanContextCarrier = new ContextCarrier();
|
||||
AbstractSpan secondExitSpan = ContextManager.createExitSpan("/testSecondExit", secondExitSpanContextCarrier, "127.0.0.1:9080");
|
||||
secondExitSpan.setComponent(ComponentsDefine.TOMCAT);
|
||||
Tags.HTTP.METHOD.set(secondExitSpan, "GET");
|
||||
Tags.URL.set(secondExitSpan, "127.0.0.1:8080");
|
||||
SpanLayer.asHttp(secondExitSpan);
|
||||
|
||||
ContextManager.stopSpan();
|
||||
ContextManager.stopSpan();
|
||||
ContextManager.stopSpan();
|
||||
|
||||
assertThat(tracingData.getTraceSegments().size(), is(1));
|
||||
TraceSegment actualSegment = tracingData.getTraceSegments().get(0);
|
||||
assertNull(actualSegment.getRefs());
|
||||
|
||||
List<AbstractTracingSpan> spanList = SegmentHelper.getSpan(actualSegment);
|
||||
assertThat(spanList.size(), is(2));
|
||||
|
||||
AbstractTracingSpan actualFirstExitSpan = spanList.get(0);
|
||||
assertThat(actualFirstExitSpan.getOperationName(), is("/testFirstExit"));
|
||||
assertThat(actualFirstExitSpan.getSpanId(), is(1));
|
||||
assertThat(AbstractTracingSpanHelper.getParentSpanId(actualFirstExitSpan), is(0));
|
||||
|
||||
AbstractTracingSpan actualEntrySpan = spanList.get(1);
|
||||
assertThat(actualEntrySpan.getOperationName(), is("/testEntrySpan"));
|
||||
assertThat(actualEntrySpan.getSpanId(), is(0));
|
||||
assertThat(AbstractTracingSpanHelper.getParentSpanId(actualEntrySpan), is(-1));
|
||||
|
||||
assertThat(firstExitSpanContextCarrier.getPeerHost(), is("#127.0.0.1:8080"));
|
||||
assertThat(firstExitSpanContextCarrier.getApplicationId(), is(1));
|
||||
assertThat(firstExitSpanContextCarrier.getSpanId(), is(1));
|
||||
assertThat(firstExitSpanContextCarrier.getEntryOperationName(), is("#/testEntrySpan"));
|
||||
|
||||
assertThat(secondExitSpanContextCarrier.getPeerHost(), is("#127.0.0.1:8080"));
|
||||
assertThat(secondExitSpanContextCarrier.getApplicationId(), is(1));
|
||||
assertThat(secondExitSpanContextCarrier.getSpanId(), is(1));
|
||||
assertThat(secondExitSpanContextCarrier.getEntryOperationName(), is("#/testEntrySpan"));
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
RemoteDownstreamConfig.Agent.APPLICATION_ID = DictionaryUtil.nullValue();
|
||||
RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID = DictionaryUtil.nullValue();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.context;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.skywalking.apm.agent.core.boot.ServiceManager;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.NoopSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
import org.skywalking.apm.agent.core.context.util.TraceSegmentHelper;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2017/2/19.
|
||||
*/
|
||||
public class ContextManagerTestCase {
|
||||
@BeforeClass
|
||||
public static void setup() {
|
||||
ServiceManager.INSTANCE.boot();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelegateToTracerContext() {
|
||||
AbstractSpan span = ContextManager.createLocalSpan("serviceA");
|
||||
span.setComponent("test");
|
||||
|
||||
Assert.assertEquals(span, ContextManager.activeSpan());
|
||||
|
||||
TracingContext.ListenerManager.add(TestTracingContextListener.INSTANCE);
|
||||
ContextManager.stopSpan();
|
||||
|
||||
TraceSegment segment = TestTracingContextListener.INSTANCE.finishedSegmentCarrier[0];
|
||||
|
||||
Assert.assertEquals(span, TraceSegmentHelper.getSpans(segment).get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwitchToIgnoredTracerContext() throws NoSuchFieldException, IllegalAccessException {
|
||||
AbstractSpan span = ContextManager.createLocalSpan("/webresource/jquery.js");
|
||||
span.setComponent("test");
|
||||
|
||||
Assert.assertTrue(span instanceof NoopSpan);
|
||||
Assert.assertTrue(ContextManager.activeSpan() instanceof NoopSpan);
|
||||
|
||||
Field context = ContextManager.class.getDeclaredField("CONTEXT");
|
||||
context.setAccessible(true);
|
||||
AbstractTracerContext tracerContext = ((ThreadLocal<AbstractTracerContext>)context.get(null)).get();
|
||||
|
||||
Assert.assertTrue(tracerContext instanceof IgnoredTracerContext);
|
||||
|
||||
ContextManager.stopSpan();
|
||||
tracerContext = ((ThreadLocal<AbstractTracerContext>)context.get(null)).get();
|
||||
Assert.assertNull(tracerContext);
|
||||
|
||||
// check normal trace again
|
||||
span = ContextManager.createLocalSpan("serviceA");
|
||||
span.setComponent("test");
|
||||
|
||||
tracerContext = ((ThreadLocal<AbstractTracerContext>)context.get(null)).get();
|
||||
Assert.assertTrue(tracerContext instanceof TracingContext);
|
||||
ContextManager.stopSpan();
|
||||
tracerContext = ((ThreadLocal<AbstractTracerContext>)context.get(null)).get();
|
||||
Assert.assertNull(tracerContext);
|
||||
}
|
||||
|
||||
@After
|
||||
public void reset() {
|
||||
TracingContext.ListenerManager.remove(TestTracingContextListener.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
package org.skywalking.apm.agent.core.context;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.skywalking.apm.agent.core.boot.ServiceManager;
|
||||
import org.skywalking.apm.agent.core.conf.Config;
|
||||
import org.skywalking.apm.agent.core.conf.RemoteDownstreamConfig;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.NoopSpan;
|
||||
import org.skywalking.apm.agent.test.tools.SegmentStorage;
|
||||
import org.skywalking.apm.agent.test.tools.SegmentStoragePoint;
|
||||
import org.skywalking.apm.agent.test.tools.TracingSegmentRunner;
|
||||
|
||||
import static junit.framework.TestCase.assertNull;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
@RunWith(TracingSegmentRunner.class)
|
||||
public class IgnoredTracerContextTest {
|
||||
|
||||
@SegmentStoragePoint
|
||||
private SegmentStorage storage;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
RemoteDownstreamConfig.Agent.APPLICATION_ID = 1;
|
||||
RemoteDownstreamConfig.Agent.APPLICATION_INSTANCE_ID = 1;
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoredTraceContextWithSampling() {
|
||||
Config.Agent.SAMPLE_N_PER_3_SECS = 1;
|
||||
ServiceManager.INSTANCE.boot();
|
||||
ContextManager.createLocalSpan("/test1");
|
||||
ContextManager.stopSpan();
|
||||
|
||||
ContextManager.createLocalSpan("/test2");
|
||||
ContextManager.stopSpan();
|
||||
|
||||
ContextManager.createLocalSpan("/test3");
|
||||
ContextManager.stopSpan();
|
||||
|
||||
ContextManager.createLocalSpan("/test4");
|
||||
ContextManager.stopSpan();
|
||||
|
||||
assertThat(storage.getIgnoredTracerContexts().size(), is(3));
|
||||
assertThat(storage.getTraceSegments().size(), is(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoredTraceContextWithExcludeOperationName() {
|
||||
ServiceManager.INSTANCE.boot();
|
||||
AbstractSpan abstractSpan = ContextManager.createEntrySpan("test.js", null);
|
||||
ContextManager.stopSpan();
|
||||
|
||||
assertThat(abstractSpan.getClass().getName(), is(NoopSpan.class.getName()));
|
||||
LinkedList<IgnoredTracerContext> ignoredTracerContexts = storage.getIgnoredTracerContexts();
|
||||
assertThat(ignoredTracerContexts.size(), is(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoredTraceContextWithEmptyOperationName() {
|
||||
ServiceManager.INSTANCE.boot();
|
||||
ContextCarrier contextCarrier = new ContextCarrier();
|
||||
AbstractSpan abstractSpan = ContextManager.createExitSpan("", contextCarrier, "127.0.0.1:2181");
|
||||
ContextManager.stopSpan();
|
||||
|
||||
assertThat(abstractSpan.getClass().getName(), is(NoopSpan.class.getName()));
|
||||
assertNull(contextCarrier.getEntryOperationName());
|
||||
assertThat(contextCarrier.getSpanId(), is(-1));
|
||||
assertNull(contextCarrier.getPeerHost());
|
||||
|
||||
LinkedList<IgnoredTracerContext> ignoredTracerContexts = storage.getIgnoredTracerContexts();
|
||||
assertThat(ignoredTracerContexts.size(), is(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.context;
|
||||
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2017/2/19.
|
||||
*/
|
||||
public enum TestTracingContextListener implements TracingContextListener {
|
||||
INSTANCE;
|
||||
final TraceSegment[] finishedSegmentCarrier = {null};
|
||||
|
||||
@Override
|
||||
public void afterFinished(TraceSegment traceSegment) {
|
||||
finishedSegmentCarrier[0] = traceSegment;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,105 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.context;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.skywalking.apm.agent.core.context.ids.DistributedTraceId;
|
||||
import org.skywalking.apm.agent.core.context.ids.PropagatedTraceId;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
import org.skywalking.apm.agent.core.context.util.TraceSegmentHelper;
|
||||
import org.skywalking.apm.agent.core.context.util.TraceSegmentRefHelper;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2017/2/19.
|
||||
*/
|
||||
public class TracerContextTestCase {
|
||||
@Test
|
||||
public void testSpanLifeCycle() {
|
||||
TracingContext context = new TracingContext();
|
||||
AbstractSpan span = context.createLocalSpan("/serviceA");
|
||||
|
||||
Assert.assertEquals(span, context.activeSpan());
|
||||
|
||||
TracingContext.ListenerManager.add(TestTracingContextListener.INSTANCE);
|
||||
final TraceSegment[] finishedSegmentCarrier = TestTracingContextListener.INSTANCE.finishedSegmentCarrier;
|
||||
context.stopSpan(span);
|
||||
|
||||
TraceSegment traceSegment = finishedSegmentCarrier[0];
|
||||
Assert.assertNotNull(traceSegment);
|
||||
Assert.assertEquals(1, TraceSegmentHelper.getSpans(traceSegment).size());
|
||||
Assert.assertEquals(span, TraceSegmentHelper.getSpans(traceSegment).get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChildOfSpan() {
|
||||
TracingContext context = new TracingContext();
|
||||
AbstractSpan serviceSpan = context.createLocalSpan("/serviceA");
|
||||
AbstractSpan dbSpan = context.createExitSpan("db/preparedStatement/execute", "127.0.0.1:3306");
|
||||
|
||||
Assert.assertEquals(dbSpan, context.activeSpan());
|
||||
|
||||
TracingContext.ListenerManager.add(TestTracingContextListener.INSTANCE);
|
||||
final TraceSegment[] finishedSegmentCarrier = TestTracingContextListener.INSTANCE.finishedSegmentCarrier;
|
||||
|
||||
try {
|
||||
context.stopSpan(serviceSpan);
|
||||
} catch (Throwable t) {
|
||||
Assert.assertTrue(t instanceof IllegalStateException);
|
||||
}
|
||||
|
||||
context.stopSpan(dbSpan);
|
||||
context.stopSpan(serviceSpan);
|
||||
|
||||
TraceSegment traceSegment = finishedSegmentCarrier[0];
|
||||
Assert.assertNotNull(traceSegment);
|
||||
Assert.assertEquals(2, TraceSegmentHelper.getSpans(traceSegment).size());
|
||||
Assert.assertEquals(dbSpan, TraceSegmentHelper.getSpans(traceSegment).get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInject() {
|
||||
TracingContext context = new TracingContext();
|
||||
AbstractSpan serviceSpan = context.createLocalSpan("/serviceA");
|
||||
AbstractSpan dbSpan = context.createExitSpan("db/preparedStatement/execute", "127.0.0.1:3306");
|
||||
|
||||
ContextCarrier carrier = new ContextCarrier();
|
||||
context.inject(carrier);
|
||||
|
||||
Assert.assertEquals("127.0.0.1:3306", carrier.getPeerHost());
|
||||
Assert.assertEquals(1, carrier.getSpanId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtract() {
|
||||
ContextCarrier carrier = new ContextCarrier();
|
||||
carrier.setTraceSegmentId("trace_id_1");
|
||||
carrier.setSpanId(5);
|
||||
carrier.setPeerHost("10.2.3.16:8080");
|
||||
List<DistributedTraceId> ids = new LinkedList<DistributedTraceId>();
|
||||
ids.add(new PropagatedTraceId("Trace.global.id.123"));
|
||||
carrier.setDistributedTraceIds(ids);
|
||||
|
||||
Assert.assertTrue(carrier.isValid());
|
||||
|
||||
TracingContext context = new TracingContext();
|
||||
context.extract(carrier);
|
||||
AbstractSpan span = context.createLocalSpan("/serviceC");
|
||||
|
||||
TracingContext.ListenerManager.add(TestTracingContextListener.INSTANCE);
|
||||
final TraceSegment[] finishedSegmentCarrier = TestTracingContextListener.INSTANCE.finishedSegmentCarrier;
|
||||
|
||||
context.stopSpan(span);
|
||||
|
||||
TraceSegment segment = finishedSegmentCarrier[0];
|
||||
Assert.assertEquals("trace_id_1", TraceSegmentRefHelper.getTraceSegmentId(segment.getRefs().get(0)));
|
||||
Assert.assertEquals(5, TraceSegmentRefHelper.getSpanId(segment.getRefs().get(0)));
|
||||
}
|
||||
|
||||
@After
|
||||
public void reset() {
|
||||
TracingContext.ListenerManager.remove(TestTracingContextListener.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,74 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.context.trace;
|
||||
|
||||
import java.util.List;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.skywalking.apm.agent.core.context.tag.Tags;
|
||||
import org.skywalking.apm.agent.core.context.util.KeyValuePair;
|
||||
import org.skywalking.apm.agent.core.context.util.KeyValuePairReader;
|
||||
import org.skywalking.apm.agent.core.context.util.TraceSegmentHelper;
|
||||
import org.skywalking.apm.agent.core.dictionary.ApplicationDictionary;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(ApplicationDictionary.class)
|
||||
public class SpanTestCase {
|
||||
|
||||
@Test
|
||||
public void testConstructors() {
|
||||
AbstractTracingSpan span1 = new LocalSpan(0, -1, "serviceA");
|
||||
AbstractTracingSpan span2 = new LocalSpan(1, 0, "serviceA");
|
||||
span2.setOperationName("serviceA-2");
|
||||
span1.start();
|
||||
span2.start();
|
||||
Assert.assertEquals("serviceA-2", span2.getOperationName());
|
||||
|
||||
Assert.assertEquals(-1, span1.parentSpanId);
|
||||
Assert.assertEquals(0, span2.parentSpanId);
|
||||
Assert.assertTrue(span1.startTime > 0);
|
||||
Assert.assertTrue(span2.startTime > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFinish() {
|
||||
TraceSegment owner = new TraceSegment();
|
||||
|
||||
AbstractTracingSpan span1 = new LocalSpan(0, -1, "serviceA");
|
||||
|
||||
Assert.assertTrue(span1.endTime == 0);
|
||||
|
||||
span1.finish(owner);
|
||||
Assert.assertEquals(span1, TraceSegmentHelper.getSpans(owner).get(0));
|
||||
Assert.assertTrue(span1.endTime > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetTag() {
|
||||
AbstractTracingSpan span1 = new LocalSpan(0, -1, "serviceA");
|
||||
SpanLayer.asHttp(span1);
|
||||
span1.setComponent("Spring");
|
||||
span1.errorOccurred();
|
||||
Tags.STATUS_CODE.set(span1, "505");
|
||||
Tags.URL.set(span1, "http://127.0.0.1/serviceA");
|
||||
Tags.DB_STATEMENT.set(span1, "select * from users");
|
||||
|
||||
Assert.assertEquals(SpanLayer.HTTP, span1.layer);
|
||||
Assert.assertTrue(span1.errorOccurred);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLogException() throws NoSuchFieldException, IllegalAccessException {
|
||||
AbstractTracingSpan span1 = new LocalSpan(0, -1, "serviceA");
|
||||
Exception exp = new Exception("exception msg");
|
||||
span1.log(exp);
|
||||
|
||||
LogDataEntity logs = span1.logs.get(0);
|
||||
List<KeyValuePair> keyValuePairs = logs.getLogs();
|
||||
|
||||
Assert.assertEquals("java.lang.Exception", KeyValuePairReader.get(keyValuePairs, "error.kind"));
|
||||
Assert.assertEquals("exception msg", KeyValuePairReader.get(keyValuePairs, "message"));
|
||||
Assert.assertNotNull(KeyValuePairReader.get(keyValuePairs, "stack"));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.context.trace;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.skywalking.apm.agent.core.context.util.TraceSegmentHelper;
|
||||
|
||||
public class TraceSegmentTestCase {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRef() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testArchiveSpan() {
|
||||
TraceSegment segment = new TraceSegment();
|
||||
AbstractTracingSpan span1 = new LocalSpan(1, 0, "/serviceA");
|
||||
segment.archive(span1);
|
||||
|
||||
AbstractTracingSpan span2 = new LocalSpan(2, 1, "/db/sql");
|
||||
segment.archive(span2);
|
||||
|
||||
Assert.assertEquals(span1, TraceSegmentHelper.getSpans(segment).get(0));
|
||||
Assert.assertEquals(span2, TraceSegmentHelper.getSpans(segment).get(1));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,15 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.context.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class KeyValuePairReader {
|
||||
public static String get(List<KeyValuePair> pairs, String key) {
|
||||
for (KeyValuePair pair : pairs) {
|
||||
if (pair.getKey().equals(key)) {
|
||||
return pair.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.context.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
|
||||
public class TraceSegmentHelper {
|
||||
|
||||
public static List<AbstractTracingSpan> getSpans(TraceSegment traceSegment) {
|
||||
try {
|
||||
Field field = AbstractTracingSpan.class.getDeclaredField("spans");
|
||||
return (List<AbstractTracingSpan>)field.get(traceSegment);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to get spans", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.context.util;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegmentRef;
|
||||
|
||||
public class TraceSegmentRefHelper {
|
||||
|
||||
public static int getSpanId(TraceSegmentRef ref) {
|
||||
try {
|
||||
Field field = TraceSegmentRef.class.getDeclaredField("spanId");
|
||||
return Integer.valueOf(field.get(ref).toString());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to get span id", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTraceSegmentId(TraceSegmentRef ref) {
|
||||
try {
|
||||
Field field = TraceSegmentRef.class.getDeclaredField("traceSegmentId");
|
||||
return field.get(ref).toString();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to get span id", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,104 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.datacarrier.performance.comparetest;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import org.junit.Test;
|
||||
import org.skywalking.apm.agent.core.datacarrier.DataCarrier;
|
||||
import org.skywalking.apm.agent.core.datacarrier.consumer.IConsumer;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2016/11/24.
|
||||
*/
|
||||
public class VsABQ {
|
||||
private static int totalSize = 100000000;
|
||||
|
||||
/**
|
||||
* 39469
|
||||
*
|
||||
* @throws InterruptedException
|
||||
*/
|
||||
@Test
|
||||
public void testABQ() throws InterruptedException {
|
||||
final ArrayBlockingQueue queue = new ArrayBlockingQueue(5000);
|
||||
|
||||
Thread consumer = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
long startTime = -1;
|
||||
int dataCounter = 0;
|
||||
|
||||
while (true) {
|
||||
ArrayList data = new ArrayList();
|
||||
queue.drainTo(data);
|
||||
if (startTime == -1 && data.size() > 0) {
|
||||
startTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
dataCounter += data.size();
|
||||
if (dataCounter == totalSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("time cost:" + (System.currentTimeMillis() - startTime));
|
||||
}
|
||||
});
|
||||
consumer.start();
|
||||
|
||||
for (int i = 0; i < totalSize; i++) {
|
||||
boolean status = false;
|
||||
while (!status) {
|
||||
try {
|
||||
queue.add(i);
|
||||
status = true;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
consumer.join();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
final DataCarrier<Integer> dataCarrier = new DataCarrier<Integer>(5, 1000);
|
||||
|
||||
dataCarrier.consume(new IConsumer<Integer>() {
|
||||
long startTime = -1;
|
||||
int dataCounter = 0;
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void consume(List<Integer> data) {
|
||||
if (startTime == -1 && data.size() > 0) {
|
||||
startTime = System.currentTimeMillis();
|
||||
}
|
||||
dataCounter += data.size();
|
||||
|
||||
if (dataCounter == totalSize) {
|
||||
System.out.println("cost:" + (System.currentTimeMillis() - startTime));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(List<Integer> data, Throwable t) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onExit() {
|
||||
|
||||
}
|
||||
}, 1);
|
||||
|
||||
for (int i = 0; i < totalSize; i++) {
|
||||
dataCarrier.produce(i);
|
||||
}
|
||||
|
||||
Thread.sleep(10 * 1000L);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.datacarrier.performance.comparetest.disruptor;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2016/11/24.
|
||||
*/
|
||||
public class Data {
|
||||
private long value1;
|
||||
|
||||
public long getValue1() {
|
||||
return value1;
|
||||
}
|
||||
|
||||
public void setValue1(long value1) {
|
||||
this.value1 = value1;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.datacarrier.performance.comparetest.disruptor;
|
||||
|
||||
import com.lmax.disruptor.EventFactory;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2016/11/24.
|
||||
*/
|
||||
public class DataEventFactory implements EventFactory<Data> {
|
||||
|
||||
@Override
|
||||
public Data newInstance() {
|
||||
return new Data();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.datacarrier.performance.comparetest.disruptor;
|
||||
|
||||
import com.lmax.disruptor.EventHandler;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2016/11/24.
|
||||
*/
|
||||
public class DataEventHandler implements EventHandler<Data> {
|
||||
public long counter = 0;
|
||||
|
||||
@Override
|
||||
public void onEvent(Data data, long sequence, boolean endOfBatch) throws Exception {
|
||||
counter++;
|
||||
System.out.println("handler:" + data.getValue1());
|
||||
|
||||
if (counter == TestDisruptor.totalSize) {
|
||||
System.out.println("time cost:" + (System.currentTimeMillis() - TestDisruptor.startTime));
|
||||
TestDisruptor.isEnd = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,25 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.datacarrier.performance.comparetest.disruptor;
|
||||
|
||||
import com.lmax.disruptor.RingBuffer;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2016/11/24.
|
||||
*/
|
||||
public class DataProducer {
|
||||
private final RingBuffer<Data> ringBuffer;
|
||||
|
||||
public DataProducer(RingBuffer<Data> ringBuffer) {
|
||||
this.ringBuffer = ringBuffer;
|
||||
}
|
||||
|
||||
public void onData(Data bb) {
|
||||
long sequence = ringBuffer.next(); // Grab the next sequence
|
||||
try {
|
||||
Data event = ringBuffer.get(sequence); // Get the entry in the Disruptor
|
||||
// for the sequence
|
||||
event.setValue1(bb.getValue1());// Fill with data
|
||||
} finally {
|
||||
ringBuffer.publish(sequence);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.datacarrier.performance.comparetest.disruptor;
|
||||
|
||||
import com.lmax.disruptor.RingBuffer;
|
||||
import com.lmax.disruptor.WorkHandler;
|
||||
import com.lmax.disruptor.dsl.Disruptor;
|
||||
import com.lmax.disruptor.util.DaemonThreadFactory;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2016/11/24.
|
||||
*/
|
||||
public class TestDisruptor {
|
||||
public static int totalSize = 100000000;
|
||||
public static long startTime;
|
||||
public static volatile boolean isEnd = false;
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
// The factory for the event
|
||||
DataEventFactory factory = new DataEventFactory();
|
||||
|
||||
// Specify the size of the ring buffer, must be power of 2.
|
||||
int bufferSize = 1024;
|
||||
|
||||
// Construct the Disruptor
|
||||
Disruptor<Data> disruptor = new Disruptor<Data>(factory, bufferSize, DaemonThreadFactory.INSTANCE);
|
||||
|
||||
disruptor.handleEventsWithWorkerPool(new WorkHandler<Data>(){
|
||||
@Override
|
||||
public void onEvent(Data event) throws Exception {
|
||||
System.out.println("work1:" + event.getValue1());
|
||||
}
|
||||
}, new WorkHandler<Data>(){
|
||||
|
||||
@Override
|
||||
public void onEvent(Data event) throws Exception {
|
||||
System.out.println("work2:" + event.getValue1());
|
||||
}
|
||||
});
|
||||
// Connect the handler
|
||||
disruptor.handleEventsWith(new DataEventHandler());
|
||||
|
||||
// Start the Disruptor, starts all threads running
|
||||
disruptor.start();
|
||||
|
||||
RingBuffer<Data> ringBuffer = disruptor.getRingBuffer();
|
||||
DataProducer producer = new DataProducer(ringBuffer);
|
||||
|
||||
startTime = System.currentTimeMillis();
|
||||
for (int i = 0; i < totalSize; i++) {
|
||||
Data data = new Data();
|
||||
data.setValue1(i);
|
||||
producer.onData(data);
|
||||
|
||||
Thread.sleep(1000L);
|
||||
}
|
||||
|
||||
disruptor.shutdown();
|
||||
|
||||
while(!TestDisruptor.isEnd){
|
||||
Thread.sleep(100L);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,81 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.discovery;
|
||||
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This is a small application, test for http restful service.
|
||||
* Use APACHE HttpClient as discovery, nanohttpd as server.
|
||||
*
|
||||
* @author wusheng
|
||||
*/
|
||||
public class HTTPRestServiceTestApp {
|
||||
public static void main(String[] args) throws Exception {
|
||||
CloseableHttpClient client = null;
|
||||
Server server = null;
|
||||
try {
|
||||
HTTPRestServiceTestApp test = new HTTPRestServiceTestApp();
|
||||
server = test.startServer();
|
||||
client = test.send();
|
||||
} finally {
|
||||
if (client != null) {
|
||||
client.close();
|
||||
}
|
||||
if (server != null) {
|
||||
server.stop();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private CloseableHttpClient send() {
|
||||
CloseableHttpClient httpclient = HttpClients.custom()
|
||||
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
|
||||
.build();
|
||||
HttpPost post = new HttpPost("http://localhost:7000/segments");
|
||||
StringEntity entity = new StringEntity("[{'abc'}]", ContentType.APPLICATION_JSON);
|
||||
post.setEntity(entity);
|
||||
try {
|
||||
CloseableHttpResponse httpResponse = httpclient.execute(post);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return httpclient;
|
||||
}
|
||||
|
||||
private Server startServer() throws Exception {
|
||||
Server server = new Server(7000);
|
||||
|
||||
server.setHandler(new AbstractHandler() {
|
||||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request,
|
||||
HttpServletResponse response) throws IOException, ServletException {
|
||||
BufferedReader br = request.getReader();
|
||||
String str, wholeStr = "";
|
||||
while ((str = br.readLine()) != null) {
|
||||
wholeStr += str;
|
||||
}
|
||||
response.setContentType("text/html; charset=utf-8");
|
||||
response.setStatus(HttpServletResponse.SC_OK);
|
||||
baseRequest.setHandled(true);
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
return server;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,116 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import java.lang.instrument.ClassFileTransformer;
|
||||
import java.lang.instrument.Instrumentation;
|
||||
import net.bytebuddy.agent.ByteBuddyAgent;
|
||||
import net.bytebuddy.agent.builder.AgentBuilder;
|
||||
import net.bytebuddy.description.type.TypeDescription;
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
import net.bytebuddy.dynamic.loading.ByteArrayClassLoader;
|
||||
import net.bytebuddy.dynamic.loading.PackageDefinitionStrategy;
|
||||
import net.bytebuddy.matcher.ElementMatchers;
|
||||
import net.bytebuddy.utility.JavaModule;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.skywalking.apm.agent.core.plugin.utility.ClassFileExtraction;
|
||||
|
||||
import static net.bytebuddy.matcher.ElementMatchers.none;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
public class AbstractClassEnhancePluginDefineTest {
|
||||
static final String WEAVE_CLASS = "org.skywalking.apm.agent.core.plugin.pluginTargetObject";
|
||||
static final String INTERCEPTOR_CLASS = "org.skywalking.apm.agent.core.plugin.MockPluginInterceptor";
|
||||
static final String WEAVE_INSTANCE_METHOD_NAME = "instanceMethod";
|
||||
static final String WEAVE_INSTANCE_WITH_EXCEPTION_METHOD_NAME = "instanceMethodWithException";
|
||||
static final String WEAVE_STATIC_METHOD_NAME = "staticMethod";
|
||||
private ClassLoader classLoader;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(),
|
||||
ClassFileExtraction.of(TargetObject.class),
|
||||
ByteArrayClassLoader.PersistenceHandler.MANIFEST);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void weaveInstanceMethod() throws Exception {
|
||||
ByteBuddyAgent.install();
|
||||
ClassFileTransformer classFileTransformer = new AgentBuilder.Default()
|
||||
.with(AgentBuilder.PoolStrategy.Default.FAST)
|
||||
.ignore(none())
|
||||
.type(ElementMatchers.is(TargetObject.class), ElementMatchers.is(classLoader)).transform(new MockTargetObjectTransformer())
|
||||
.installOnByteBuddyAgent();
|
||||
|
||||
try {
|
||||
Class<?> type = classLoader.loadClass(TargetObject.class.getName());
|
||||
assertThat(type.getDeclaredMethod(WEAVE_INSTANCE_METHOD_NAME).invoke(type.getDeclaredConstructor(String.class).newInstance("a"))
|
||||
, CoreMatchers.<Object>is(WEAVE_INSTANCE_METHOD_NAME + "a"));
|
||||
} finally {
|
||||
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void weaveInstanceMethodWITEXCEPTION() throws Exception {
|
||||
ByteBuddyAgent.install();
|
||||
ClassFileTransformer classFileTransformer = new AgentBuilder.Default()
|
||||
.with(AgentBuilder.PoolStrategy.Default.FAST)
|
||||
.ignore(none())
|
||||
.type(ElementMatchers.is(TargetObject.class), ElementMatchers.is(classLoader)).transform(new MockTargetObjectTransformer())
|
||||
.installOnByteBuddyAgent();
|
||||
|
||||
try {
|
||||
Class<?> type = classLoader.loadClass(TargetObject.class.getName());
|
||||
type.getDeclaredMethod(WEAVE_INSTANCE_WITH_EXCEPTION_METHOD_NAME).invoke(type.getDeclaredConstructor(String.class).newInstance("a"));
|
||||
} finally {
|
||||
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void weaveStaticMethod() throws Exception {
|
||||
ByteBuddyAgent.install();
|
||||
ClassFileTransformer classFileTransformer = new AgentBuilder.Default()
|
||||
.with(AgentBuilder.PoolStrategy.Default.FAST)
|
||||
.ignore(none())
|
||||
.type(ElementMatchers.is(TargetObject.class), ElementMatchers.is(classLoader)).transform(new MockTargetObjectTransformer())
|
||||
.installOnByteBuddyAgent();
|
||||
|
||||
try {
|
||||
Class<?> type = classLoader.loadClass(TargetObject.class.getName());
|
||||
assertThat(type.getDeclaredMethod(WEAVE_STATIC_METHOD_NAME).invoke(type), CoreMatchers.<Object>is(WEAVE_STATIC_METHOD_NAME + "_STATIC"));
|
||||
} finally {
|
||||
ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer);
|
||||
}
|
||||
}
|
||||
|
||||
public static class MockTargetObjectTransformer implements AgentBuilder.Transformer {
|
||||
|
||||
private DynamicType.Builder<?> transformStaticMethod(DynamicType.Builder newBuilder) {
|
||||
MockPluginStaticMethodInstrumentation staticMethodInstrumentation = new MockPluginStaticMethodInstrumentation();
|
||||
return staticMethodInstrumentation.define(WEAVE_CLASS, newBuilder, AbstractClassEnhancePluginDefineTest.class.getClassLoader());
|
||||
}
|
||||
|
||||
private DynamicType.Builder transformInstanceMethod(DynamicType.Builder<?> builder) {
|
||||
MockPluginInstanceMethodInstrumentation instrumentation = new MockPluginInstanceMethodInstrumentation();
|
||||
return instrumentation.define(WEAVE_CLASS, builder, AbstractClassEnhancePluginDefineTest.class.getClassLoader());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DynamicType.Builder<?> transform(DynamicType.Builder<?> builder, TypeDescription typeDescription,
|
||||
ClassLoader classLoader, JavaModule module) {
|
||||
try {
|
||||
DynamicType.Builder newBuilder = transformInstanceMethod(builder);
|
||||
return transformStaticMethod(newBuilder);
|
||||
} catch (Exception exception) {
|
||||
throw new AssertionError(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2017/2/27.
|
||||
*/
|
||||
public class MockAbstractClassEnhancePluginDefine extends AbstractClassEnhancePluginDefine {
|
||||
|
||||
@Override
|
||||
protected DynamicType.Builder<?> enhance(String enhanceOriginClassName, DynamicType.Builder<?> newClassBuilder,
|
||||
ClassLoader classLoader) throws PluginException {
|
||||
return newClassBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
return "NotExistClass";
|
||||
}
|
||||
}
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import net.bytebuddy.description.method.MethodDescription;
|
||||
import net.bytebuddy.matcher.ElementMatcher;
|
||||
import org.skywalking.apm.agent.core.plugin.bytebuddy.AllObjectDefaultMethodsMatch;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
|
||||
|
||||
import static net.bytebuddy.matcher.ElementMatchers.*;
|
||||
|
||||
public class MockPluginInstanceMethodInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
return AbstractClassEnhancePluginDefineTest.WEAVE_CLASS;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
|
||||
return new ConstructorInterceptPoint[] {
|
||||
new ConstructorInterceptPoint() {
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getConstructorMatcher() {
|
||||
return takesArgument(0, String.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConstructorInterceptor() {
|
||||
return AbstractClassEnhancePluginDefineTest.INTERCEPTOR_CLASS;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
|
||||
return new InstanceMethodsInterceptPoint[] {
|
||||
new InstanceMethodsInterceptPoint() {
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
return named(AbstractClassEnhancePluginDefineTest.WEAVE_INSTANCE_METHOD_NAME).and(not(AllObjectDefaultMethodsMatch.INSTANCE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodsInterceptor() {
|
||||
return AbstractClassEnhancePluginDefineTest.INTERCEPTOR_CLASS;
|
||||
}
|
||||
|
||||
@Override public boolean isOverrideArgs() {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
new InstanceMethodsInterceptPoint() {
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
return named(AbstractClassEnhancePluginDefineTest.WEAVE_INSTANCE_WITH_EXCEPTION_METHOD_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodsInterceptor() {
|
||||
return AbstractClassEnhancePluginDefineTest.INTERCEPTOR_CLASS;
|
||||
}
|
||||
|
||||
@Override public boolean isOverrideArgs() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor;
|
||||
|
||||
public class MockPluginInterceptor implements InstanceMethodsAroundInterceptor, StaticMethodsAroundInterceptor, InstanceConstructorInterceptor {
|
||||
|
||||
@Override
|
||||
public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {
|
||||
objInst.setSkyWalkingDynamicField(allArguments[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeMethod(Class clazz, String methodName, Object[] allArguments, Class<?>[] parameterTypes,
|
||||
MethodInterceptResult result) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object afterMethod(Class clazz, String methodName, Object[] allArguments, Class<?>[] parameterTypes,
|
||||
Object ret) {
|
||||
return ret + "_STATIC";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMethodException(Class clazz, String methodName, Object[] allArguments, Class<?>[] parameterTypes,
|
||||
Throwable t) {
|
||||
|
||||
}
|
||||
|
||||
@Override public void beforeMethod(EnhancedInstance objInst, String methodName, Object[] allArguments,
|
||||
Class<?>[] argumentsTypes, MethodInterceptResult result) throws Throwable {
|
||||
|
||||
}
|
||||
|
||||
@Override public Object afterMethod(EnhancedInstance objInst, String methodName, Object[] allArguments,
|
||||
Class<?>[] argumentsTypes, Object ret) throws Throwable {
|
||||
return ret + String.valueOf(objInst.getSkyWalkingDynamicField());
|
||||
}
|
||||
|
||||
@Override public void handleMethodException(EnhancedInstance objInst, String methodName, Object[] allArguments,
|
||||
Class<?>[] argumentsTypes, Throwable t) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import net.bytebuddy.description.method.MethodDescription;
|
||||
import net.bytebuddy.matcher.ElementMatcher;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassStaticMethodsEnhancePluginDefine;
|
||||
|
||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
||||
|
||||
public class MockPluginStaticMethodInstrumentation extends ClassStaticMethodsEnhancePluginDefine {
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
return AbstractClassEnhancePluginDefineTest.WEAVE_CLASS;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() {
|
||||
return new StaticMethodsInterceptPoint[] {
|
||||
new StaticMethodsInterceptPoint() {
|
||||
@Override
|
||||
public ElementMatcher<MethodDescription> getMethodsMatcher() {
|
||||
return named(AbstractClassEnhancePluginDefineTest.WEAVE_STATIC_METHOD_NAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMethodsInterceptor() {
|
||||
return AbstractClassEnhancePluginDefineTest.INTERCEPTOR_CLASS;
|
||||
}
|
||||
|
||||
@Override public boolean isOverrideArgs() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author wusheng
|
||||
*/
|
||||
public class PluginBootstrapTest {
|
||||
@Test
|
||||
public void testLoadPlugins() {
|
||||
PluginBootstrap bootstrap = new PluginBootstrap();
|
||||
List<AbstractClassEnhancePluginDefine> defines = bootstrap.loadPlugins();
|
||||
|
||||
Assert.assertEquals(1, defines.size());
|
||||
Assert.assertEquals(MockAbstractClassEnhancePluginDefine.class, defines.get(0).getClass());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.powermock.api.support.membermodification.MemberModifier;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2017/2/27.
|
||||
*/
|
||||
public class PluginCfgTest {
|
||||
@Test
|
||||
public void testLoad() throws IOException {
|
||||
String data = "TestA=com.test.classA\r\nTestB=com.test.ClassB";
|
||||
final byte[] dataBytes = data.getBytes();
|
||||
PluginCfg.INSTANCE.load(new InputStream() {
|
||||
int index = 0;
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if (index == dataBytes.length) {
|
||||
return -1;
|
||||
}
|
||||
return dataBytes[index++];
|
||||
}
|
||||
});
|
||||
|
||||
List<PluginDefine> list = PluginCfg.INSTANCE.getPluginClassList();
|
||||
Assert.assertEquals(2, list.size());
|
||||
Assert.assertEquals("com.test.classA", list.get(0).getDefineClass());
|
||||
Assert.assertEquals("com.test.ClassB", list.get(1).getDefineClass());
|
||||
}
|
||||
|
||||
@Before
|
||||
@After
|
||||
public void clear() throws IllegalAccessException {
|
||||
MemberModifier.field(PluginCfg.class, "pluginClassList").set(PluginCfg.INSTANCE, new ArrayList<String>());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.skywalking.apm.agent.core.conf.Config;
|
||||
import org.skywalking.apm.agent.core.plugin.exception.IllegalPluginDefineException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PluginDefineTest {
|
||||
|
||||
private static final String TEST_PLUGIN = "test_plugin";
|
||||
private static final String TEST_DEFINE_CLASS = "test_define_class";
|
||||
|
||||
@Test(expected = IllegalPluginDefineException.class)
|
||||
public void testIllegalPluginDefine() throws IllegalPluginDefineException {
|
||||
PluginDefine.build("illegal_plugin_define");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalPluginDefineException.class)
|
||||
public void testEmptyPluginDefine() throws IllegalPluginDefineException {
|
||||
PluginDefine.build("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOffStatePlugin() throws IllegalPluginDefineException {
|
||||
PluginDefine pluginDefine = PluginDefine.build(PluginDefine.PLUGIN_OFF_PREFIX + TEST_PLUGIN + "=" + TEST_DEFINE_CLASS);
|
||||
assertFalse(pluginDefine.enable());
|
||||
assertEquals(TEST_DEFINE_CLASS, pluginDefine.getDefineClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultStatePlugin() throws IllegalPluginDefineException {
|
||||
PluginDefine pluginDefine = PluginDefine.build(TEST_PLUGIN + "=" + TEST_DEFINE_CLASS);
|
||||
assertTrue(pluginDefine.enable());
|
||||
assertEquals(TEST_DEFINE_CLASS, pluginDefine.getDefineClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testForceEnablePlugin() throws IllegalPluginDefineException {
|
||||
Config.Plugin.FORCE_ENABLE_PLUGINS.add(TEST_PLUGIN);
|
||||
PluginDefine pluginDefine = PluginDefine.build(PluginDefine.PLUGIN_OFF_PREFIX + TEST_PLUGIN + "=" + TEST_DEFINE_CLASS);
|
||||
assertTrue(pluginDefine.enable());
|
||||
assertEquals(TEST_DEFINE_CLASS, pluginDefine.getDefineClass());
|
||||
Config.Plugin.FORCE_ENABLE_PLUGINS.clear();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,59 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Created by wusheng on 2017/2/27.
|
||||
*/
|
||||
public class PluginFinderTest {
|
||||
@Test
|
||||
public void testFind() {
|
||||
ArrayList<AbstractClassEnhancePluginDefine> defines = new ArrayList<AbstractClassEnhancePluginDefine>();
|
||||
defines.add(new NewTestPlugin());
|
||||
defines.add(new NewTestPlugin2());
|
||||
PluginFinder finder = new PluginFinder(defines);
|
||||
|
||||
Assert.assertNotNull(finder.find("test.NewClass"));
|
||||
Assert.assertTrue(finder.exist("test.NewClass"));
|
||||
}
|
||||
|
||||
@Test(expected = PluginException.class)
|
||||
public void testCanNotFind() {
|
||||
ArrayList<AbstractClassEnhancePluginDefine> defines = new ArrayList<AbstractClassEnhancePluginDefine>();
|
||||
defines.add(new NewTestPlugin());
|
||||
PluginFinder finder = new PluginFinder(defines);
|
||||
|
||||
finder.find("test.NewClass2");
|
||||
}
|
||||
|
||||
public class NewTestPlugin extends AbstractClassEnhancePluginDefine {
|
||||
|
||||
@Override
|
||||
protected DynamicType.Builder<?> enhance(String enhanceOriginClassName, DynamicType.Builder<?> newClassBuilder,
|
||||
ClassLoader classLoader) throws PluginException {
|
||||
return newClassBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
return "test.NewClass";
|
||||
}
|
||||
}
|
||||
|
||||
public class NewTestPlugin2 extends AbstractClassEnhancePluginDefine {
|
||||
|
||||
@Override
|
||||
protected DynamicType.Builder<?> enhance(String enhanceOriginClassName, DynamicType.Builder<?> newClassBuilder,
|
||||
ClassLoader classLoader) throws PluginException {
|
||||
return newClassBuilder;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String enhanceClassName() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author wusheng
|
||||
*/
|
||||
public class PluginResourcesResolverTest {
|
||||
@Test
|
||||
public void testGetResources() {
|
||||
PluginResourcesResolver resolver = new PluginResourcesResolver();
|
||||
|
||||
Assert.assertTrue(resolver.getResources().size() > 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin;
|
||||
|
||||
public class TargetObject {
|
||||
|
||||
private String value;
|
||||
|
||||
public TargetObject(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String instanceMethod() {
|
||||
return "instanceMethod";
|
||||
}
|
||||
|
||||
public String instanceMethodWithException() {
|
||||
throw new RuntimeException("test exception");
|
||||
}
|
||||
|
||||
public static String staticMethod() {
|
||||
return "staticMethod";
|
||||
}
|
||||
}
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin.bytebuddy;
|
||||
|
||||
import net.bytebuddy.description.method.MethodDescription;
|
||||
import net.bytebuddy.description.method.ParameterDescription;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* @author wusheng
|
||||
*/
|
||||
public class ArgumentTypeNameMatchTest {
|
||||
@Test
|
||||
public void testMatches() throws IllegalAccessException {
|
||||
MethodDescription methodDescription = Mockito.mock(MethodDescription.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
ParameterDescription parameterDescription = Mockito.mock(ParameterDescription.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
when(methodDescription.getParameters().get(0)).thenReturn(parameterDescription);
|
||||
when(methodDescription.getParameters().size()).thenReturn(1);
|
||||
when(parameterDescription.getType().asErasure().getName()).thenReturn("org.skywalking.TestClass");
|
||||
|
||||
ArgumentTypeNameMatch matcher = ((ArgumentTypeNameMatch) ArgumentTypeNameMatch.takesArgumentWithType(0, "org.skywalking.TestClass"));
|
||||
Assert.assertTrue(matcher.matches(methodDescription));
|
||||
|
||||
ArgumentTypeNameMatch matcher2 = ((ArgumentTypeNameMatch) ArgumentTypeNameMatch.takesArgumentWithType(0, "org.skywalking.TestClass2"));
|
||||
Assert.assertFalse(matcher2.matches(methodDescription));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMatchesWithNoParameters() {
|
||||
MethodDescription methodDescription = Mockito.mock(MethodDescription.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
ParameterDescription parameterDescription = Mockito.mock(ParameterDescription.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
when(methodDescription.getParameters().size()).thenReturn(0);
|
||||
|
||||
ArgumentTypeNameMatch matcher2 = ((ArgumentTypeNameMatch) ArgumentTypeNameMatch.takesArgumentWithType(0, "org.skywalking.TestClass"));
|
||||
Assert.assertFalse(matcher2.matches(methodDescription));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin.loader;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.skywalking.apm.agent.core.plugin.interceptor.loader.InterceptorInstanceLoader;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
/**
|
||||
* @author wusheng
|
||||
*/
|
||||
public class InterceptorInstanceLoaderTest {
|
||||
@Test
|
||||
public void load() throws ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
|
||||
ClassLoader mockClassLoader = new ClassLoader() {
|
||||
@Override
|
||||
public Class<?> loadClass(String name) throws ClassNotFoundException {
|
||||
return super.loadClass(name);
|
||||
}
|
||||
};
|
||||
Object obj = InterceptorInstanceLoader.load("org.skywalking.apm.agent.core.plugin.loader.NeverUsedTestClass", mockClassLoader);
|
||||
Assert.assertTrue(obj != null);
|
||||
|
||||
Object obj2 = InterceptorInstanceLoader.load("org.skywalking.apm.agent.core.plugin.loader.NeverUsedTestClass", mockClassLoader);
|
||||
Assert.assertTrue(obj != null);
|
||||
Assert.assertEquals(obj, obj2);
|
||||
|
||||
Object obj3 = InterceptorInstanceLoader.load("org.skywalking.apm.agent.core.plugin.loader.NeverUsedTestClass", InterceptorInstanceLoaderTest.class.getClassLoader());
|
||||
Assert.assertTrue(obj3 != null);
|
||||
|
||||
Object obj4 = InterceptorInstanceLoader.load("org.skywalking.apm.agent.core.plugin.loader.NeverUsedTestClass", InterceptorInstanceLoaderTest.class.getClassLoader());
|
||||
Assert.assertTrue(obj4 != null);
|
||||
Assert.assertEquals(obj3, obj4);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin.loader;
|
||||
|
||||
/**
|
||||
* This class is only used in {@link InterceptorInstanceLoaderTest},
|
||||
* never be created manually.
|
||||
*
|
||||
* @author wusheng
|
||||
*/
|
||||
public class NeverUsedTestClass {
|
||||
}
|
||||
|
|
@ -1,108 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.plugin.utility;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import net.bytebuddy.ClassFileVersion;
|
||||
import net.bytebuddy.asm.AsmVisitorWrapper;
|
||||
import net.bytebuddy.description.field.FieldDescription;
|
||||
import net.bytebuddy.description.field.FieldList;
|
||||
import net.bytebuddy.description.method.MethodDescription;
|
||||
import net.bytebuddy.description.method.MethodList;
|
||||
import net.bytebuddy.description.type.TypeDescription;
|
||||
import net.bytebuddy.implementation.Implementation;
|
||||
import net.bytebuddy.implementation.auxiliary.AuxiliaryType;
|
||||
import net.bytebuddy.implementation.bytecode.StackManipulation;
|
||||
import net.bytebuddy.jar.asm.ClassReader;
|
||||
import net.bytebuddy.jar.asm.ClassWriter;
|
||||
import net.bytebuddy.pool.TypePool;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
public class ClassFileExtraction {
|
||||
|
||||
private static final int CA = 0xCA, FE = 0xFE, BA = 0xBA, BE = 0xBE;
|
||||
|
||||
public static Map<String, byte[]> of(Class<?>... type) throws IOException {
|
||||
Map<String, byte[]> result = new HashMap<String, byte[]>();
|
||||
for (Class<?> aType : type) {
|
||||
result.put(aType.getName(), extract(aType));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static byte[] extract(Class<?> type, AsmVisitorWrapper asmVisitorWrapper) throws IOException {
|
||||
ClassReader classReader = new ClassReader(type.getName());
|
||||
ClassWriter classWriter = new ClassWriter(classReader, AsmVisitorWrapper.NO_FLAGS);
|
||||
classReader.accept(asmVisitorWrapper.wrap(new TypeDescription.ForLoadedType(type),
|
||||
classWriter,
|
||||
new IllegalContext(),
|
||||
TypePool.Empty.INSTANCE,
|
||||
new FieldList.Empty<FieldDescription.InDefinedShape>(),
|
||||
new MethodList.Empty<MethodDescription>(),
|
||||
AsmVisitorWrapper.NO_FLAGS,
|
||||
AsmVisitorWrapper.NO_FLAGS), AsmVisitorWrapper.NO_FLAGS);
|
||||
return classWriter.toByteArray();
|
||||
}
|
||||
|
||||
public static byte[] extract(Class<?> type) throws IOException {
|
||||
return extract(type, new AsmVisitorWrapper.Compound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClassFileExtraction() throws Exception {
|
||||
byte[] binaryFoo = extract(Foo.class);
|
||||
assertThat(binaryFoo.length > 4, is(true));
|
||||
assertThat(binaryFoo[0], is(new Integer(CA).byteValue()));
|
||||
assertThat(binaryFoo[1], is(new Integer(FE).byteValue()));
|
||||
assertThat(binaryFoo[2], is(new Integer(BA).byteValue()));
|
||||
assertThat(binaryFoo[3], is(new Integer(BE).byteValue()));
|
||||
}
|
||||
|
||||
private static class Foo {
|
||||
/* empty */
|
||||
}
|
||||
|
||||
private static class IllegalContext implements Implementation.Context {
|
||||
|
||||
@Override
|
||||
public TypeDescription register(AuxiliaryType auxiliaryType) {
|
||||
throw new AssertionError("Did not expect method call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public FieldDescription.InDefinedShape cache(StackManipulation fieldValue, TypeDescription fieldType) {
|
||||
throw new AssertionError("Did not expect method call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeDescription getInstrumentedType() {
|
||||
throw new AssertionError("Did not expect method call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClassFileVersion getClassFileVersion() {
|
||||
throw new AssertionError("Did not expect method call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodDescription.InDefinedShape registerAccessorFor(
|
||||
Implementation.SpecialMethodInvocation specialMethodInvocation, AccessType accessType) {
|
||||
throw new AssertionError("Did not expect method call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodDescription.InDefinedShape registerGetterFor(FieldDescription fieldDescription,
|
||||
AccessType accessType) {
|
||||
throw new AssertionError("Did not expect method call");
|
||||
}
|
||||
|
||||
@Override
|
||||
public MethodDescription.InDefinedShape registerSetterFor(FieldDescription fieldDescription,
|
||||
AccessType accessType) {
|
||||
throw new AssertionError("Did not expect method call");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,78 +0,0 @@
|
|||
package org.skywalking.apm.agent.core.sampling;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.skywalking.apm.agent.core.boot.ServiceManager;
|
||||
import org.skywalking.apm.agent.core.conf.Config;
|
||||
import org.skywalking.apm.agent.core.context.ContextManager;
|
||||
import org.skywalking.apm.agent.core.context.TracingContext;
|
||||
import org.skywalking.apm.agent.core.context.TracingContextListener;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
|
||||
/**
|
||||
* @author wusheng
|
||||
*/
|
||||
public class SamplingTracerContextTestCase {
|
||||
private int finishedTracerCounter = 0;
|
||||
|
||||
private TracingContextListener listener = new TracingContextListener() {
|
||||
@Override
|
||||
public void afterFinished(TraceSegment traceSegment) {
|
||||
if (!traceSegment.isIgnore()) {
|
||||
finishedTracerCounter++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Config.Agent.SAMPLE_N_PER_3_SECS = 5;
|
||||
ServiceManager.INSTANCE.boot();
|
||||
TracingContext.ListenerManager.add(listener);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSample5InALoop() throws InterruptedException {
|
||||
for (int i = 0; i < 11; i++) {
|
||||
AbstractSpan span = ContextManager.createLocalSpan("serviceA");
|
||||
span.setComponent("test");
|
||||
ContextManager.stopSpan();
|
||||
}
|
||||
|
||||
/**
|
||||
* Considering the reset cycle, in ci-env, may sample 5-7 trace through 1 or 2 cycle.
|
||||
*/
|
||||
Assert.assertTrue(finishedTracerCounter >= 2);
|
||||
Assert.assertTrue(finishedTracerCounter <= 3);
|
||||
Thread.sleep(10 * 1000L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSample5InLoopWithMultiSpans() {
|
||||
finishedTracerCounter = 0;
|
||||
for (int i = 0; i < 11; i++) {
|
||||
AbstractSpan span = ContextManager.createLocalSpan("serviceA");
|
||||
span.setComponent("test");
|
||||
AbstractSpan span2 = ContextManager.createLocalSpan("serviceB");
|
||||
span.setComponent("test2");
|
||||
ContextManager.stopSpan();
|
||||
ContextManager.stopSpan();
|
||||
}
|
||||
|
||||
/**
|
||||
* Considering the reset cycle, in ci-env, may sample 5-7 trace through 1 or 2 cycle.
|
||||
*/
|
||||
Assert.assertTrue(finishedTracerCounter >= 5);
|
||||
Assert.assertTrue(finishedTracerCounter <= 7);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
Config.Agent.SAMPLE_N_PER_3_SECS = -1;
|
||||
TracingContext.ListenerManager.remove(listener);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.skywalking</groupId>
|
||||
<artifactId>apm-sniffer</artifactId>
|
||||
<version>3.2-2017</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>apm-test-tools</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>apm-test-tools</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.skywalking</groupId>
|
||||
<artifactId>apm-agent-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package org.skywalking.apm.agent.test.helper;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.LogDataEntity;
|
||||
import org.skywalking.apm.agent.core.context.util.KeyValuePair;
|
||||
|
||||
public class AbstractTracingSpanHelper {
|
||||
public static int getParentSpanId(AbstractTracingSpan tracingSpan) {
|
||||
try {
|
||||
return FieldGetter.getParentFieldValue(tracingSpan, "parentSpanId");
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return -9999;
|
||||
}
|
||||
|
||||
public static List<LogDataEntity> getLogs(AbstractTracingSpan tracingSpan) {
|
||||
try {
|
||||
return FieldGetter.getParentFieldValue(tracingSpan, "logs");
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package org.skywalking.apm.agent.test.helper;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
public class FieldGetter {
|
||||
public static <T> T getValue(Object instance,
|
||||
String fieldName) throws IllegalAccessException, NoSuchFieldException {
|
||||
Field field = instance.getClass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return (T)field.get(instance);
|
||||
}
|
||||
|
||||
public static <T> T getParentFieldValue(Object instance,
|
||||
String fieldName) throws IllegalAccessException, NoSuchFieldException {
|
||||
Field field = instance.getClass().getSuperclass().getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
return (T)field.get(instance);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
package org.skywalking.apm.agent.test.helper;
|
||||
|
||||
import java.util.List;
|
||||
import org.skywalking.apm.agent.core.context.trace.AbstractTracingSpan;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
|
||||
public class SegmentHelper {
|
||||
|
||||
public static List<AbstractTracingSpan> getSpan(TraceSegment traceSegment) {
|
||||
try {
|
||||
return FieldGetter.getValue(traceSegment, "spans");
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package org.skywalking.apm.agent.test.helper;
|
||||
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegmentRef;
|
||||
|
||||
public class TraceSegmentRefHelper {
|
||||
public static String getPeerHost(TraceSegmentRef ref) {
|
||||
try {
|
||||
return FieldGetter.getValue(ref, "peerHost");
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package org.skywalking.apm.agent.test.tools;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.skywalking.apm.agent.core.context.IgnoredTracerContext;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
|
||||
public class SegmentStorage {
|
||||
private LinkedList<TraceSegment> traceSegments;
|
||||
private LinkedList<IgnoredTracerContext> ignoredTracerContexts;
|
||||
|
||||
SegmentStorage() {
|
||||
traceSegments = new LinkedList<TraceSegment>();
|
||||
ignoredTracerContexts = new LinkedList<IgnoredTracerContext>();
|
||||
}
|
||||
|
||||
void addTraceSegment(TraceSegment segment) {
|
||||
traceSegments.add(segment);
|
||||
}
|
||||
|
||||
public List<TraceSegment> getTraceSegments() {
|
||||
return traceSegments;
|
||||
}
|
||||
|
||||
void addIgnoreTraceContext(IgnoredTracerContext context) {
|
||||
this.ignoredTracerContexts.add(context);
|
||||
}
|
||||
|
||||
public LinkedList<IgnoredTracerContext> getIgnoredTracerContexts() {
|
||||
return ignoredTracerContexts;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
package org.skywalking.apm.agent.test.tools;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface SegmentStoragePoint {
|
||||
}
|
||||
|
|
@ -0,0 +1,86 @@
|
|||
package org.skywalking.apm.agent.test.tools;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runner.notification.Failure;
|
||||
import org.junit.runner.notification.RunNotifier;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
import org.skywalking.apm.agent.core.context.IgnoreTracerContextListener;
|
||||
import org.skywalking.apm.agent.core.context.IgnoredTracerContext;
|
||||
import org.skywalking.apm.agent.core.context.TracingContext;
|
||||
import org.skywalking.apm.agent.core.context.TracingContextListener;
|
||||
import org.skywalking.apm.agent.core.context.trace.TraceSegment;
|
||||
|
||||
public class TracingSegmentRunner extends BlockJUnit4ClassRunner {
|
||||
private TracingContextListener tracingContextListener;
|
||||
private IgnoreTracerContextListener ignoreTracerContextListener;
|
||||
private Field field;
|
||||
private Object targetObject;
|
||||
private SegmentStorage tracingData;
|
||||
|
||||
public TracingSegmentRunner(Class<?> klass) throws InitializationError {
|
||||
super(klass);
|
||||
for (Field field : klass.getDeclaredFields()) {
|
||||
if (field.isAnnotationPresent(SegmentStoragePoint.class) && field.getType().equals(SegmentStorage.class)) {
|
||||
this.field = field;
|
||||
this.field.setAccessible(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object createTest() throws Exception {
|
||||
targetObject = super.createTest();
|
||||
return targetObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(RunNotifier notifier) {
|
||||
notifier.addListener(new RunListener());
|
||||
super.run(notifier);
|
||||
}
|
||||
|
||||
class RunListener extends org.junit.runner.notification.RunListener {
|
||||
@Override
|
||||
public void testStarted(Description description) throws Exception {
|
||||
if (field != null) {
|
||||
try {
|
||||
tracingData = new SegmentStorage();
|
||||
field.set(targetObject, tracingData);
|
||||
} catch (IllegalAccessException e) {
|
||||
}
|
||||
}
|
||||
tracingContextListener = new TracingContextListener() {
|
||||
@Override
|
||||
public void afterFinished(TraceSegment traceSegment) {
|
||||
tracingData.addTraceSegment(traceSegment);
|
||||
}
|
||||
};
|
||||
|
||||
ignoreTracerContextListener = new IgnoreTracerContextListener() {
|
||||
@Override
|
||||
public void afterFinished(IgnoredTracerContext tracerContext) {
|
||||
tracingData.addIgnoreTraceContext(tracerContext);
|
||||
}
|
||||
};
|
||||
TracingContext.ListenerManager.add(tracingContextListener);
|
||||
IgnoredTracerContext.ListenerManager.add(ignoreTracerContextListener);
|
||||
super.testStarted(description);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testFinished(Description description) throws Exception {
|
||||
super.testFinished(description);
|
||||
TracingContext.ListenerManager.remove(tracingContextListener);
|
||||
IgnoredTracerContext.ListenerManager.remove(ignoreTracerContextListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void testFailure(Failure failure) throws Exception {
|
||||
super.testFailure(failure);
|
||||
TracingContext.ListenerManager.remove(tracingContextListener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -17,6 +17,7 @@
|
|||
<module>apm-agent-core</module>
|
||||
<module>apm-sdk-plugin</module>
|
||||
<module>apm-toolkit-activation</module>
|
||||
<module>apm-assert-tools</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
|
|
|
|||
Loading…
Reference in New Issue