关于skywalkig抽样数的设置问题
关于agent.sample_n_per_3_secs 配置项的设置
通过配置 Config.Agent.SAMPLE_N_PER_3_SECS 属性,设置 每3秒,收集 TraceSegment的条数。
默认情况下,agent.sample_n_per_3_secs = -1,代表在不超过内存Buffer区(5*300TraceSegment)的前提下,采集所有的Trace信息,不开启抽样服务,即全部收集
#抽样服务源码
org.apache.skywalking.apm.agent.core.sampling.SamplingService
#每隔3秒重置计数器
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 10 seconds.", Config.Agent.SAMPLE_N_PER_3_SECS);
}
private void resetSamplingFactor() {
samplingFactorHolder = new AtomicInteger(0);
}
#如果开启抽样时,未达到采样上线,在并发的情况下,获得“乐观锁”取样成功将会采集这次的Trace信息,否则,不会采集Trace信息。
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;
}
skywalking 全自动探针监控,不需要修改应用程序代码
高性能探针,针对单实例5000tps的应用,在全量采集的情况下,只增加 10% 的CPU开销。
换成取样数来计算,
SAMPLE_N_PER_3_SECS = 15000(5000 * 3 ) 只增加 10% 的CPU开销。
将取样率设置为 SAMPLE_N_PER_3_SECS = 1500
SAMPLE_N_PER_3_SECS = 1500 预计大约会增加 1% 的CPU开销。
那么,具体值视系统或服务的并发情况,可在测试环境下取得经验值的尝试范围将控制在[500 - 1500],即可。
另外,每个Trace包含多个segment,每当缓存SegmentRecord条数大于batchSize(v6.0.0alpha = 1000,v5.0.0-GA = 10000)条才会存入es中,
void onWork(INPUT input) {
if (getCache().currentCollectionSize() >= batchSize) {
try {
if (getCache().trySwitchPointer()) {
getCache().switchPointer();
List<?> collection = buildBatchCollection();
batchDAO.batchPersistence(collection);
}
} finally {
getCache().trySwitchPointerFinally();
}
}
cacheData(input);
}