From 5f6dd33ce1a7b0844a73a9b35a41f453be38b617 Mon Sep 17 00:00:00 2001 From: wusheng Date: Wed, 22 Mar 2017 10:46:33 +0800 Subject: [PATCH] Fix jedis missing tag: span.kind. --- .../skywalking/api/boot/ServiceManager.java | 33 +++++++++-------- .../api/sampling/SamplingService.java | 7 ++-- .../api/sampling/SamplingServiceTest.java | 36 +++++++++++++++++++ .../jedis/v2/JedisMethodInterceptor.java | 1 + 4 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/sampling/SamplingServiceTest.java diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/boot/ServiceManager.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/boot/ServiceManager.java index 1ff1c9820..ea5af0fe6 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/boot/ServiceManager.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/boot/ServiceManager.java @@ -17,36 +17,35 @@ public enum ServiceManager { INSTANCE; private static ILog logger = LogManager.getLogger(StatusBootService.class); - private volatile boolean isStarted = false; - private Map bootedServices; + private Map bootedServices = new HashMap(); public void boot() { - if (!isStarted) { + bootedServices = loadAllServices(); + } + + private Map loadAllServices() { + HashMap bootedServices = new HashMap(); + Iterator serviceIterator = load().iterator(); + while (serviceIterator.hasNext()) { + BootService bootService = serviceIterator.next(); try { - bootedServices = new HashMap(); - Iterator serviceIterator = load().iterator(); - while (serviceIterator.hasNext()) { - BootService bootService = serviceIterator.next(); - try { - bootService.bootUp(); - bootedServices.put(bootService.getClass(), bootService); - } catch (Throwable e) { - logger.error(e, "ServiceManager try to start [{}] fail.", bootService.getClass().getName()); - } - } - } finally { - isStarted = true; + bootService.bootUp(); + bootedServices.put(bootService.getClass(), bootService); + } catch (Throwable e) { + logger.error(e, "ServiceManager try to start [{}] fail.", bootService.getClass().getName()); } } + return bootedServices; } /** * Find a {@link BootService} implementation, which is already started. + * * @param serviceClass class name. * @param {@link BootService} implementation class. * @return {@link BootService} instance */ - public T findService(Class serviceClass){ + public T findService(Class serviceClass) { return (T)bootedServices.get(serviceClass); } diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/sampling/SamplingService.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/sampling/SamplingService.java index 6967c203d..f1701dd37 100644 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/sampling/SamplingService.java +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/api/sampling/SamplingService.java @@ -21,7 +21,7 @@ public class SamplingService implements BootService { private volatile boolean on = false; private volatile int rate = 0; - private volatile int rollingSeed = 0; + private volatile int rollingSeed = 1; @Override public void bootUp() throws Throwable { @@ -39,9 +39,10 @@ public class SamplingService implements BootService { public void trySampling(TraceSegment segment) { if (on) { - if (rollingSeed++ != rate) { + if (rollingSeed % rate != 0) { segment.setSampled(false); } + rollingSeed++; } } @@ -59,7 +60,7 @@ public class SamplingService implements BootService { if(on) { if (!segment.isSampled() && carrier.isSampled()) { segment.setSampled(true); - this.rollingSeed = 0; + this.rollingSeed = 1; } } } diff --git a/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/sampling/SamplingServiceTest.java b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/sampling/SamplingServiceTest.java new file mode 100644 index 000000000..6fedcb121 --- /dev/null +++ b/skywalking-sniffer/skywalking-api/src/test/java/com/a/eye/skywalking/api/sampling/SamplingServiceTest.java @@ -0,0 +1,36 @@ +package com.a.eye.skywalking.api.sampling; + +import com.a.eye.skywalking.api.boot.ServiceManager; +import com.a.eye.skywalking.api.conf.Config; +import com.a.eye.skywalking.trace.TraceSegment; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author wusheng + */ +public class SamplingServiceTest { + @Test + public void test50Percent(){ + Config.Agent.SAMPLING_RATE = 5000; + ServiceManager.INSTANCE.boot(); + + TraceSegment segment = new TraceSegment(); + Assert.assertTrue(segment.isSampled()); + + SamplingService service = ServiceManager.INSTANCE.findService(SamplingService.class); + service.trySampling(segment); + Assert.assertFalse(segment.isSampled()); + + segment = new TraceSegment(); + service.trySampling(segment); + Assert.assertTrue(segment.isSampled()); + } + + @AfterClass + public static void clear(){ + Config.Agent.SAMPLING_RATE = 10000; + ServiceManager.INSTANCE.boot(); + } +} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java index 4cb17f016..914823ccf 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java @@ -50,6 +50,7 @@ public class JedisMethodInterceptor extends NoCocurrencyAceessObject implements Span span = ContextManager.createSpan("Jedis/" + interceptorContext.methodName()); Tags.COMPONENT.set(span, REDIS_COMPONENT); Tags.DB_TYPE.set(span, REDIS_COMPONENT); + Tags.SPAN_KIND.set(span, Tags.SPAN_KIND_CLIENT); tagPeer(span, context); Tags.SPAN_LAYER.asDB(span); if (StringUtil.isEmpty(context.get(KEY_OF_REDIS_HOST, String.class))) {