首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >聊聊skywalking的SamplingService

聊聊skywalking的SamplingService

原创
作者头像
code4it
修改于 2020-03-01 23:05:26
修改于 2020-03-01 23:05:26
91600
代码可运行
举报
文章被收录于专栏:码匠的流水账码匠的流水账
运行总次数:0
代码可运行

本文主要研究一下skywalking的SamplingService

SamplingService

skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/sampling/SamplingService.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@DefaultImplementor
public class SamplingService implements BootService {
    private static final ILog logger = LogManager.getLogger(SamplingService.class);private volatile boolean on = false;
    private volatile AtomicInteger samplingFactorHolder;
    private volatile ScheduledFuture<?> scheduledFuture;
​
    @Override
    public void prepare() throws Throwable {}
​
    @Override
    public void boot() throws Throwable {
        if (scheduledFuture != null) {
            /**
             * If {@link #boot()} invokes twice, mostly in test cases,
             * cancel the old one.
             */
            scheduledFuture.cancel(true);
        }
        if (Config.Agent.SAMPLE_N_PER_3_SECS > 0) {
            on = true;
            this.resetSamplingFactor();
            ScheduledExecutorService service = Executors
                .newSingleThreadScheduledExecutor(new DefaultNamedThreadFactory("SamplingService"));
            scheduledFuture = service.scheduleAtFixedRate(new RunnableWithExceptionProtection(new Runnable() {
                @Override
                public void run() {
                    resetSamplingFactor();
                }
            }, new RunnableWithExceptionProtection.CallbackWhenException() {
                @Override public void handle(Throwable t) {
                    logger.error("unexpected exception.", t);
                }
            }), 0, 3, TimeUnit.SECONDS);
            logger.debug("Agent sampling mechanism started. Sample {} traces in 3 seconds.", Config.Agent.SAMPLE_N_PER_3_SECS);
        }
    }
​
    @Override
    public void onComplete() throws Throwable {}
​
    @Override
    public void shutdown() throws Throwable {
        if (scheduledFuture != null) {
            scheduledFuture.cancel(true);
        }
    }/**
     * @return true, if sampling mechanism is on, and getDefault the sampling factor successfully.
     */
    public boolean trySampling() {
        if (on) {
            int factor = samplingFactorHolder.get();
            if (factor < Config.Agent.SAMPLE_N_PER_3_SECS) {
                boolean success = samplingFactorHolder.compareAndSet(factor, factor + 1);
                return success;
            } else {
                return false;
            }
        }
        return true;
    }/**
     * Increase the sampling factor by force,
     * to avoid sampling too many traces.
     * If many distributed traces require sampled,
     * the trace beginning at local, has less chance to be sampled.
     */
    public void forceSampled() {
        if (on) {
            samplingFactorHolder.incrementAndGet();
        }
    }private void resetSamplingFactor() {
        samplingFactorHolder = new AtomicInteger(0);
    }
}
  • SamplingService实现了BootService接口,其boot方法在Config.Agent.SAMPLE_N_PER_3_SECS大于0时标记on为true,然后先执行一下resetSamplingFactor,之后注册一个定时任务定时执行resetSamplingFactor;其shutdown方法会cancel掉这个定时任务;resetSamplingFactor方法会重置samplingFactorHolder为0;它还提供了trySampling方法给外部调用,该方法在samplingFactorHolder小于Config.Agent.SAMPLE_N_PER_3_SECS时,执行samplingFactorHolder.compareAndSet(factor, factor + 1);forceSampled方法则在on的前提下执行samplingFactorHolder.incrementAndGet()

ContextManagerExtendService

skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManagerExtendService.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@DefaultImplementor
public class ContextManagerExtendService implements BootService {
    @Override public void prepare() {}
​
    @Override public void boot() {}
​
    @Override public void onComplete() {}
​
    @Override public void shutdown() {}public AbstractTracerContext createTraceContext(String operationName, boolean forceSampling) {
        AbstractTracerContext context;
        int suffixIdx = operationName.lastIndexOf(".");
        if (suffixIdx > -1 && Config.Agent.IGNORE_SUFFIX.contains(operationName.substring(suffixIdx))) {
            context = new IgnoredTracerContext();
        } else {
            SamplingService samplingService = ServiceManager.INSTANCE.findService(SamplingService.class);
            if (forceSampling || samplingService.trySampling()) {
                context = new TracingContext();
            } else {
                context = new IgnoredTracerContext();
            }
        }return context;
    }
}
  • ContextManagerExtendService实现了BootService接口,不过都是空方法,它提供了createTraceContext方法,该方法根据配置决定返回IgnoredTracerContext还是TracingContext;在非Config.Agent.IGNORE_SUFFIX的条件下,在forceSampling或者samplingService.trySampling()返回true时,会返回TracingContext

小结

SamplingService在Config.Agent.SAMPLE_N_PER_3_SECS大于0时会注册一个定时任务定时重置samplingFactorHolder为0;它还提供了它还提供了trySampling方法给外部调用,该方法在samplingFactorHolder小于Config.Agent.SAMPLE_N_PER_3_SECS时,执行samplingFactorHolder.compareAndSet(factor, factor + 1);forceSampled方法则在on的前提下执行samplingFactorHolder.incrementAndGet();ContextManagerExtendService的createTraceContext会通过ServiceManager.INSTANCE.findService然后在forceSampling或者samplingService.trySampling()为true时返回TracingContext,其余的返回IgnoredTracerContext

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
聊聊skywalking的JVMService
skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/boot/BootService.java
code4it
2020/02/24
9410
聊聊skywalking的JVMService
聊聊skywaking的CommandService
skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/commands/CommandService.java
code4it
2020/02/29
6660
聊聊skywaking的CommandService
聊聊skywalking的GRPCStreamServiceStatus
本文主要研究一下skywalking的GRPCStreamServiceStatus
code4it
2020/03/28
7910
聊聊skywalking的GRPCStreamServiceStatus
聊聊skywalking的ServiceAndEndpointRegisterClient
本文主要研究一下skywalking的ServiceAndEndpointRegisterClient
code4it
2020/03/01
1.5K0
聊聊skywalking的ServiceAndEndpointRegisterClient
聊聊skywalking的TraceSegmentServiceClient
本文主要研究一下skywalking的TraceSegmentServiceClient
code4it
2020/03/02
1.3K0
聊聊skywalking的TraceSegmentServiceClient
聊聊SkyWalkingAgent
skywalking-6.6.0/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java
code4it
2020/02/23
1.5K0
聊聊SkyWalkingAgent
聊聊skywalking的spring-annotation-plugin
本文主要研究一下skywalking的spring-annotation-plugin
code4it
2020/03/06
1.1K0
聊聊skywalking的spring-annotation-plugin
聊聊skywalking的AbstractPlatformTransactionManagerInstrumentation
本文主要研究一下skywalking的AbstractPlatformTransactionManagerInstrumentation
code4it
2020/03/04
5280
聊聊skywalking的AbstractPlatformTransactionManagerInstrumentation
聊聊skywalking的spring-cloud-gateway-plugin
本文主要研究一下skywalking的spring-cloud-gateway-plugin
code4it
2020/03/07
1.2K0
聊聊skywalking的spring-cloud-gateway-plugin
聊聊skywalking的CPUProvider
skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/jvm/cpu/CPUProvider.java
code4it
2020/02/25
6780
聊聊skywalking的CPUProvider
聊聊skywalking的ServiceResetCommand
skywalking-6.6.0/apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/command/ServiceResetCommand.java
code4it
2020/03/29
6550
聊聊skywalking的ServiceResetCommand
聊聊skywalking的sharding-sphere-plugin
本文主要研究一下skywalking的sharding-sphere-plugin
code4it
2020/03/16
7930
聊聊skywalking的sharding-sphere-plugin
聊聊skywalking的AbstractClassEnhancePluginDefine
本文主要研究一下skywalking的AbstractClassEnhancePluginDefine
code4it
2020/03/03
5730
聊聊skywalking的AbstractClassEnhancePluginDefine
聊聊skywalking的mysql-plugin
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/mysql-8.x-plugin/src/main/resources/skywalking-plugin.def
code4it
2020/03/15
1.1K0
聊聊skywalking的mysql-plugin
聊聊skywalking的MemoryProvider
skywalking-6.6.0/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/jvm/memory/MemoryProvider.java
code4it
2020/02/26
4840
聊聊skywalking的MemoryProvider
聊聊skywalking的log4j2-activation
skywalking-6.6.0/apm-sniffer/apm-toolkit-activation/apm-toolkit-log4j-2.x-activation/src/main/resources/skywalking-plugin.def
code4it
2020/03/19
7530
聊聊skywalking的log4j2-activation
聊聊skywalking的httpclient-plugin
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/httpClient-4.x-plugin/src/main/resources/skywalking-plugin.def
code4it
2020/03/09
1.1K0
聊聊skywalking的httpclient-plugin
聊聊skywalking的dubbo-2.7.x-plugin
skywalking-6.6.0/apm-sniffer/apm-sdk-plugin/dubbo-2.7.x-plugin/src/main/resources/skywalking-plugin.def
code4it
2020/03/11
1K0
聊聊skywalking的dubbo-2.7.x-plugin
聊聊skywalking的lettuce-plugin
skywalking-6.6.0/apm-sniffer/optional-plugins/lettuce-5.x-plugin/src/main/resources/skywalking-plugin.def
code4it
2020/03/14
1.2K0
聊聊skywalking的lettuce-plugin
聊聊skywalking的spring-webflux-plugin
skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/v5/define/DispatcherHandlerInstrumentation.java
code4it
2020/03/05
1.2K0
聊聊skywalking的spring-webflux-plugin
推荐阅读
相关推荐
聊聊skywalking的JVMService
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档