diff --git a/README.md b/README.md
index 2e6adbfc..704c080b 100644
--- a/README.md
+++ b/README.md
@@ -61,6 +61,7 @@ SOFATracer 的编译环境的要求为 JDK7 或者 JDK8,需要采用 [Apache M
* [SOFATracer 示例工程(基于日志编程接口 SLF4J 示例打印 traceId)](./tracer-samples/tracer-sample-with-slf4j)
* [SOFATracer 示例工程(基于 DataSource 示例落地日志)](./tracer-samples/tracer-sample-with-h2)
* [SOFATracer 示例工程(基于 HttpClient 示例落地日志)](./tracer-samples/tracer-sample-with-httpclient)
+* [SOFATracer 示例工程(SOFATracer 采样策略)](./tracer-samples/tracer-sample-with-sampler)
## 六、文档
diff --git a/pom.xml b/pom.xml
index 34e9b4d5..9d2e453b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -11,7 +11,6 @@
tracer-all-parent
2.3.0-SNAPSHOT
pom
-
tracer-all-parent
Alipay SOFATracer Log Implemented by OpenTracing
diff --git a/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/C3p0Test.java b/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/C3p0Test.java
index 4b48f4d8..066af552 100644
--- a/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/C3p0Test.java
+++ b/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/C3p0Test.java
@@ -16,6 +16,8 @@
*/
package com.sofa.tracer.plugins.datasource;
+import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.sofa.tracer.plugins.datasource.BaseDataSource;
import com.alipay.sofa.tracer.plugins.datasource.DBType;
import com.alipay.sofa.tracer.plugins.datasource.SmartDataSource;
@@ -42,6 +44,12 @@ public class C3p0Test extends BaseTest {
@Before
public void beforeTestCase() throws Exception {
+
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
+
sqlExecutionMock();
when(comboPooledDataSource.getJdbcUrl())
.thenReturn("jdbc:oracle:thin:@//mockJdbcHost:9336");
diff --git a/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/DruidTest.java b/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/DruidTest.java
index 2ab79972..8f4b5fe5 100644
--- a/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/DruidTest.java
+++ b/sofa-tracer-plugins/sofa-tracer-datasource-plugin/src/test/java/com/sofa/tracer/plugins/datasource/DruidTest.java
@@ -20,6 +20,8 @@
import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.druid.pool.DruidPooledPreparedStatement;
import com.alibaba.druid.pool.DruidPooledStatement;
+import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.sofa.tracer.plugins.datasource.BaseDataSource;
import com.alipay.sofa.tracer.plugins.datasource.DBType;
import com.alipay.sofa.tracer.plugins.datasource.SmartDataSource;
@@ -55,6 +57,12 @@ public DruidTest() {
@Before
public void beforeTestCase() throws Exception {
+
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
+
cn = druidPooledConnection;
ps = druidPooledPreparedStatement;
st = druidPooledStatement;
diff --git a/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/test/java/com/alipay/sofa/tracer/plugins/httpclient/AsyncHttpClientTracerTest.java b/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/test/java/com/alipay/sofa/tracer/plugins/httpclient/AsyncHttpClientTracerTest.java
index d5bf47c7..3ac938d9 100644
--- a/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/test/java/com/alipay/sofa/tracer/plugins/httpclient/AsyncHttpClientTracerTest.java
+++ b/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/test/java/com/alipay/sofa/tracer/plugins/httpclient/AsyncHttpClientTracerTest.java
@@ -21,6 +21,7 @@
import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.utils.StringUtils;
import com.alipay.sofa.tracer.plugins.httpclient.base.AbstractTestBase;
@@ -48,6 +49,10 @@ public class AsyncHttpClientTracerTest extends AbstractTestBase {
public void setUp() throws Exception {
super.setUp();
SofaTracerConfiguration.setProperty(SofaTracerConfiguration.STAT_LOG_INTERVAL, "1");
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
}
@After
diff --git a/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/test/java/com/alipay/sofa/tracer/plugins/httpclient/HttpClientTracerTest.java b/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/test/java/com/alipay/sofa/tracer/plugins/httpclient/HttpClientTracerTest.java
index da1dc605..bbb4dc55 100644
--- a/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/test/java/com/alipay/sofa/tracer/plugins/httpclient/HttpClientTracerTest.java
+++ b/sofa-tracer-plugins/sofa-tracer-httpclient-plugin/src/test/java/com/alipay/sofa/tracer/plugins/httpclient/HttpClientTracerTest.java
@@ -21,6 +21,7 @@
import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.utils.StringUtils;
import com.alipay.sofa.tracer.plugins.httpclient.base.AbstractTestBase;
@@ -47,6 +48,10 @@ public class HttpClientTracerTest extends AbstractTestBase {
public void setUp() throws Exception {
super.setUp();
SofaTracerConfiguration.setProperty(SofaTracerConfiguration.STAT_LOG_INTERVAL, "1");
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
}
@After
diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java
index ff01d8e6..213f9c91 100644
--- a/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java
+++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/SofaTracer.java
@@ -25,6 +25,7 @@
import com.alipay.common.tracer.core.registry.TracerFormatRegistry;
import com.alipay.common.tracer.core.reporter.facade.Reporter;
import com.alipay.common.tracer.core.samplers.Sampler;
+import com.alipay.common.tracer.core.samplers.SamplerFactory;
import com.alipay.common.tracer.core.samplers.SamplingStatus;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.span.SofaTracerSpanReferenceRelationship;
@@ -116,6 +117,10 @@ public void reportSpan(SofaTracerSpan span) {
if (span == null) {
return;
}
+ // //sampler is support & current span is root span
+ if (sampler != null && span.getParentSofaTracerSpan() == null) {
+ span.getSofaTracerSpanContext().setSampled(sampler.sample(span).isSampled());
+ }
//invoke listener
this.invokeReportListeners(span);
//客户端、服务端
@@ -288,24 +293,37 @@ public Span start() {
long begin = this.startTime > 0 ? this.startTime : System.currentTimeMillis();
SofaTracerSpan sofaTracerSpan = new SofaTracerSpan(SofaTracer.this, begin,
this.references, this.operationName, sofaTracerSpanContext, this.tags);
+
+ // calculate isSampled,but do not change parent's sampler behaviour
+ boolean isSampled = calculateSampler(sofaTracerSpan);
+ sofaTracerSpanContext.setSampled(isSampled);
+
return sofaTracerSpan;
}
- private SofaTracerSpanContext createRootSpanContext() {
- //生成 traceId
- String traceId = TraceIdGenerator.generate();
- //默认不采样
+ private boolean calculateSampler(SofaTracerSpan sofaTracerSpan) {
boolean isSampled = false;
- if (sampler != null) {
- SamplingStatus samplingStatus = sampler.sample(this.operationName, traceId);
- if (samplingStatus.isSampled()) {
- isSampled = true;
- //发生采样后,将相关属性记录
- this.tags.putAll(samplingStatus.getTags());
+ if (this.references != null && this.references.size() > 0) {
+ SofaTracerSpanContext preferredReference = preferredReference();
+ isSampled = preferredReference.isSampled();
+ } else {
+ if (sampler != null) {
+ SamplingStatus samplingStatus = sampler.sample(sofaTracerSpan);
+ if (samplingStatus.isSampled()) {
+ isSampled = true;
+ //发生采样后,将相关属性记录
+ this.tags.putAll(samplingStatus.getTags());
+ }
}
}
- return new SofaTracerSpanContext(traceId, ROOT_SPAN_ID, StringUtils.EMPTY_STRING,
- isSampled);
+
+ return isSampled;
+ }
+
+ private SofaTracerSpanContext createRootSpanContext() {
+ //生成 traceId
+ String traceId = TraceIdGenerator.generate();
+ return new SofaTracerSpanContext(traceId, ROOT_SPAN_ID, StringUtils.EMPTY_STRING);
}
private SofaTracerSpanContext createChildContext() {
@@ -456,6 +474,11 @@ public Builder withTags(Map tags) {
}
public SofaTracer build() {
+ try {
+ sampler = SamplerFactory.getSampler();
+ } catch (Exception e) {
+ SelfLog.error("Failed to get tracer sampler strategy;");
+ }
return new SofaTracer(this.tracerType, this.clientReporter, this.serverReporter,
this.sampler, this.tracerTags);
}
diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/configuration/SofaTracerConfiguration.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/configuration/SofaTracerConfiguration.java
index 8a522ffd..ef3b6ba2 100644
--- a/tracer-core/src/main/java/com/alipay/common/tracer/core/configuration/SofaTracerConfiguration.java
+++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/configuration/SofaTracerConfiguration.java
@@ -111,6 +111,15 @@ public class SofaTracerConfiguration {
private static SofaTracerExternalConfiguration sofaTracerExternalConfiguration = null;
+ /***************** 采样配置项 end ***************/
+
+ /** 采样策略名称key */
+ public static final String SAMPLER_STRATEGY_NAME_KEY = "tracer_sampler_strategy_name_key";
+ /** 自定义采样规则类名 */
+ public static final String SAMPLER_STRATEGY_CUSTOM_RULE_CLASS_NAME = "tracer_sampler_strategy_custom_rule_class_name";
+ /** 采样率key */
+ public static final String SAMPLER_STRATEGY_PERCENTAGE_KEY = "tracer_sampler_strategy_percentage_key";
+
static {
InputStream inputStream = null;
try {
@@ -269,4 +278,12 @@ public static String getLogReserveConfig(String logReserveKey) {
public static void setSofaTracerExternalConfiguration(SofaTracerExternalConfiguration sofaTracerExternalConfiguration) {
SofaTracerConfiguration.sofaTracerExternalConfiguration = sofaTracerExternalConfiguration;
}
+
+ public static String getSofaTracerSamplerStrategy() {
+ String samplerName = getProperty(SAMPLER_STRATEGY_NAME_KEY);
+ if (StringUtils.isBlank(samplerName)) {
+ return null;
+ }
+ return samplerName;
+ }
}
\ No newline at end of file
diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/registry/AbstractTextB3Formatter.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/registry/AbstractTextB3Formatter.java
index 4ef623d5..6a5f43ae 100644
--- a/tracer-core/src/main/java/com/alipay/common/tracer/core/registry/AbstractTextB3Formatter.java
+++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/registry/AbstractTextB3Formatter.java
@@ -146,6 +146,7 @@ public void inject(SofaTracerSpanContext spanContext, TextMap carrier) {
carrier.put(SPAN_ID_KEY_HEAD, encodedValue(spanContext.getSpanId()));
carrier.put(PARENT_SPAN_ID_KEY_HEAD, encodedValue(spanContext.getParentId()));
carrier.put(SPAN_ID_KEY_HEAD, encodedValue(spanContext.getSpanId()));
+ carrier.put(SAMPLED_KEY_HEAD, encodedValue(String.valueOf(spanContext.isSampled())));
//System Baggage items
for (Map.Entry entry : spanContext.getSysBaggage().entrySet()) {
String key = BAGGAGE_SYS_KEY_PREFIX + StringUtils.escapePercentEqualAnd(entry.getKey());
diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/AbstractDiskReporter.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/AbstractDiskReporter.java
index 8b222a2a..980c6981 100644
--- a/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/AbstractDiskReporter.java
+++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/reporter/digest/AbstractDiskReporter.java
@@ -89,8 +89,9 @@ protected boolean isDisableDigestLog(SofaTracerSpan span) {
return true;
}
SofaTracerSpanContext sofaTracerSpanContext = (SofaTracerSpanContext) span.context();
- if (sofaTracerSpanContext.isSampled()) {
- return false;
+ // sampled is false; this span will not be report
+ if (!sofaTracerSpanContext.isSampled()) {
+ return true;
}
boolean allDisabled = Boolean.TRUE.toString().equalsIgnoreCase(
SofaTracerConfiguration
diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/Sampler.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/Sampler.java
index 866a7af4..3f126daa 100644
--- a/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/Sampler.java
+++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/Sampler.java
@@ -16,13 +16,14 @@
*/
package com.alipay.common.tracer.core.samplers;
+import com.alipay.common.tracer.core.span.SofaTracerSpan;
+
public interface Sampler {
/**
- * @param operation The operation name set on the span
- * @param traceId The traceId on the span
+ * @param sofaTracerSpan The operation name set on the span
* @return whether or not the new trace should be sampled
*/
- SamplingStatus sample(String operation, String traceId);
+ SamplingStatus sample(SofaTracerSpan sofaTracerSpan);
String getType();
diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SamplerFactory.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SamplerFactory.java
new file mode 100644
index 00000000..4bf55d13
--- /dev/null
+++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SamplerFactory.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alipay.common.tracer.core.samplers;
+
+import com.alipay.common.tracer.core.appender.self.SelfLog;
+import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
+import com.alipay.common.tracer.core.utils.StringUtils;
+
+/**
+ * SamplerFactory
+ *
+ * @author: guolei.sgl
+ * @since: 18/9/11
+ */
+public class SamplerFactory {
+
+ public static SamplerProperties samplerProperties;
+
+ static {
+ samplerProperties = new SamplerProperties();
+ try {
+ float percentage = 100;
+
+ String perStr = SofaTracerConfiguration
+ .getProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY);
+ if (StringUtils.isNotBlank(perStr)) {
+ percentage = Float.parseFloat(perStr);
+ }
+ samplerProperties.setPercentage(percentage);
+ } catch (Exception e) {
+ SelfLog.error("It will be use default percentage value :100;", e);
+ samplerProperties.setPercentage(100);
+ }
+ samplerProperties.setRuleClassName(SofaTracerConfiguration
+ .getProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_CUSTOM_RULE_CLASS_NAME));
+ }
+
+ /**
+ * getSampler by samplerName
+ *
+ * the samplerName is the user configuration
+ * @return Sampler
+ * @throws Exception
+ */
+ public static Sampler getSampler() throws Exception {
+ // User-defined rules have high priority
+ if (StringUtils.isNotBlank(samplerProperties.getRuleClassName())) {
+ return (Sampler) Class.forName(samplerProperties.getRuleClassName()).newInstance();
+ }
+ // default instance
+ return new SofaTracerPercentageBasedSampler(samplerProperties);
+ }
+}
diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SamplerProperties.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SamplerProperties.java
index 6040d7f4..dcaed743 100644
--- a/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SamplerProperties.java
+++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SamplerProperties.java
@@ -25,16 +25,30 @@
public class SamplerProperties {
/**
* Percentage of requests that should be sampled. E.g. 1.0 - 100% requests should be
- * sampled. The precision is whole-numbers only (i.e. there's no support for 0.1% of
+ * sampled. The precision is whole-numbers only (i.e. there's no support for 1.0 of
* the traces).
*/
- private float percentage = 0.1f;
+ private float percentage = 100;
+
+ /**
+ * if use custom rule, you can implements Sample interface and provide this class name
+ */
+ private String ruleClassName;
public float getPercentage() {
- return this.percentage;
+ return percentage;
}
public void setPercentage(float percentage) {
this.percentage = percentage;
}
+
+ public String getRuleClassName() {
+ return ruleClassName;
+ }
+
+ public void setRuleClassName(String ruleClassName) {
+ this.ruleClassName = ruleClassName;
+ }
+
}
diff --git a/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SofaTracerPercentageBasedSampler.java b/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SofaTracerPercentageBasedSampler.java
index 5087302c..ba8e9bf0 100644
--- a/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SofaTracerPercentageBasedSampler.java
+++ b/tracer-core/src/main/java/com/alipay/common/tracer/core/samplers/SofaTracerPercentageBasedSampler.java
@@ -17,6 +17,7 @@
package com.alipay.common.tracer.core.samplers;
import com.alipay.common.tracer.core.constants.SofaTracerConstant;
+import com.alipay.common.tracer.core.span.SofaTracerSpan;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@@ -29,20 +30,20 @@
*/
public class SofaTracerPercentageBasedSampler implements Sampler {
- static final String TYPE = "PercentageBasedSampler";
+ public static final String TYPE = "PercentageBasedSampler";
private final AtomicInteger counter = new AtomicInteger(0);
private final BitSet sampleDecisions;
private final SamplerProperties configuration;
public SofaTracerPercentageBasedSampler(SamplerProperties configuration) {
- int outOf100 = (int) (configuration.getPercentage() * 100.0f);
+ int outOf100 = (int) (configuration.getPercentage());
this.sampleDecisions = randomBitSet(100, outOf100, new Random());
this.configuration = configuration;
}
@Override
- public SamplingStatus sample(String operation, String traceId) {
+ public SamplingStatus sample(SofaTracerSpan sofaTracerSpan) {
SamplingStatus samplingStatus = new SamplingStatus();
Map tags = new HashMap();
tags.put(SofaTracerConstant.SAMPLER_TYPE_TAG_KEY, TYPE);
diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/common/CommonSpanEncoderTest.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/common/CommonSpanEncoderTest.java
index 2cf5c718..0f45b048 100644
--- a/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/common/CommonSpanEncoderTest.java
+++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/reporter/common/CommonSpanEncoderTest.java
@@ -19,9 +19,11 @@
import com.alipay.common.tracer.core.SofaTracer;
import com.alipay.common.tracer.core.TestUtil;
import com.alipay.common.tracer.core.base.AbstractTestBase;
+import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext;
import com.alipay.common.tracer.core.reporter.digest.DiskReporterImpl;
import com.alipay.common.tracer.core.reporter.type.TracerSystemLogEnum;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.tags.SpanTags;
import com.alipay.common.tracer.core.tracertest.encoder.ClientSpanEncoder;
@@ -58,7 +60,11 @@ public class CommonSpanEncoderTest extends AbstractTestBase {
private String appName = "appName";
@Before
- public void setup() {
+ public void setup() throws Exception {
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
DiskReporterImpl clientDigestReporter = new DiskReporterImpl(clientLogType,
new ClientSpanEncoder());
sofaTracer = new SofaTracer.Builder("commonProfileTracerType")
@@ -66,7 +72,7 @@ public void setup() {
}
/**
- * Method: encode(SofaTracerSpan span)4
+ * Method: encode(SofaTracerSpan span)
*/
@Test
public void testEncode() throws Exception {
diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/samplers/SamplerPropertiesTest.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/samplers/SamplerPropertiesTest.java
index e0456c6f..28ffae6d 100644
--- a/tracer-core/src/test/java/com/alipay/common/tracer/core/samplers/SamplerPropertiesTest.java
+++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/samplers/SamplerPropertiesTest.java
@@ -38,12 +38,12 @@ public void before() {
@Test
public void getPercentage() {
- Assert.assertTrue(samplerProperties.getPercentage() == 0.1f);
+ Assert.assertTrue(samplerProperties.getPercentage() == 100);
}
@Test
public void setPercentage() {
- samplerProperties.setPercentage(0.2f);
- Assert.assertTrue(samplerProperties.getPercentage() == 0.2f);
+ samplerProperties.setPercentage(20);
+ Assert.assertTrue(samplerProperties.getPercentage() == 20);
}
}
\ No newline at end of file
diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/samplers/SofaTracerPercentageBasedSamplerTest.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/samplers/SofaTracerPercentageBasedSamplerTest.java
index ed808317..68422e0d 100644
--- a/tracer-core/src/test/java/com/alipay/common/tracer/core/samplers/SofaTracerPercentageBasedSamplerTest.java
+++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/samplers/SofaTracerPercentageBasedSamplerTest.java
@@ -16,6 +16,12 @@
*/
package com.alipay.common.tracer.core.samplers;
+import com.alipay.common.tracer.core.SofaTracer;
+import com.alipay.common.tracer.core.reporter.digest.DiskReporterImpl;
+import com.alipay.common.tracer.core.reporter.facade.Reporter;
+import com.alipay.common.tracer.core.span.SofaTracerSpan;
+import com.alipay.common.tracer.core.tracertest.encoder.ClientSpanEncoder;
+import com.alipay.common.tracer.core.tracertest.encoder.ServerSpanEncoder;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -29,13 +35,27 @@
*/
public class SofaTracerPercentageBasedSamplerTest {
+ private final String tracerType = "SofaTracerSpanTest";
+ private final String clientLogType = "client-log-test.log";
+ private final String serverLogType = "server-log-test.log";
+
SamplerProperties samplerProperties;
SofaTracerPercentageBasedSampler sofaTracerPercentageBasedSampler;
+ private SofaTracer sofaTracer;
+ private SofaTracerSpan sofaTracerSpan;
+
@Before
public void setUp() {
samplerProperties = new SamplerProperties();
sofaTracerPercentageBasedSampler = new SofaTracerPercentageBasedSampler(samplerProperties);
+ Reporter clientReporter = new DiskReporterImpl(clientLogType, new ClientSpanEncoder());
+ Reporter serverReporter = new DiskReporterImpl(serverLogType, new ServerSpanEncoder());
+ sofaTracer = new SofaTracer.Builder(tracerType)
+ .withTag("tracer", "SofaTraceContextHolderTest").withClientReporter(clientReporter)
+ .withServerReporter(serverReporter).build();
+ sofaTracerSpan = (SofaTracerSpan) this.sofaTracer.buildSpan("").start();
+ sofaTracerSpan.getSofaTracerSpanContext().setTraceId("");
}
@After
@@ -46,11 +66,11 @@ public void close() {
@Test
public void sample() {
samplerProperties.setPercentage(0);
- SamplingStatus sampleStatusFalse = sofaTracerPercentageBasedSampler.sample("", "");
+ SamplingStatus sampleStatusFalse = sofaTracerPercentageBasedSampler.sample(sofaTracerSpan);
Assert.assertTrue(!sampleStatusFalse.isSampled());
samplerProperties.setPercentage(100);
- SamplingStatus sampleStatusTrue = sofaTracerPercentageBasedSampler.sample("", "");
+ SamplingStatus sampleStatusTrue = sofaTracerPercentageBasedSampler.sample(sofaTracerSpan);
Assert.assertTrue(sampleStatusTrue.isSampled());
}
diff --git a/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/SofaTracerTest.java b/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/SofaTracerTest.java
index aa50710e..1d4dd2a7 100644
--- a/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/SofaTracerTest.java
+++ b/tracer-core/src/test/java/com/alipay/common/tracer/core/tracertest/SofaTracerTest.java
@@ -24,6 +24,7 @@
import com.alipay.common.tracer.core.reporter.digest.DiskReporterImpl;
import com.alipay.common.tracer.core.reporter.facade.Reporter;
import com.alipay.common.tracer.core.samplers.Sampler;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.core.tracertest.encoder.ClientSpanEncoder;
import com.alipay.common.tracer.core.tracertest.encoder.ServerSpanEncoder;
@@ -38,6 +39,8 @@
import org.junit.Before;
import org.junit.Test;
+import java.io.File;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
@@ -64,6 +67,12 @@ public class SofaTracerTest extends AbstractTestBase {
@Before
public void beforeInstance() throws IOException {
+
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
+
//client
DiskReporterImpl clientReporter = new DiskReporterImpl(
TracerTestLogEnum.RPC_CLIENT.getDefaultLogName(), new ClientSpanEncoder());
@@ -197,10 +206,10 @@ public void testTracerClose() {
Sampler sampler = mock(Sampler.class);
SofaTracer sofaTracer = new SofaTracer.Builder(tracerType).withClientReporter(reporter)
.withSampler(sampler).build();
-
sofaTracer.close();
//确认被调用
verify(reporter).close();
+ sampler.close();
verify(sampler).close();
}
@@ -493,7 +502,7 @@ public void testWithStatsReporterSofaTracerDigestReporter() {
*/
@Test
public void testWithSampler() throws Exception {
- assertTrue(this.sofaTracer.getSampler() == null);
+ assertTrue(this.sofaTracer.getSampler() != null);
}
}
diff --git a/tracer-samples/pom.xml b/tracer-samples/pom.xml
index ca5efa30..3cc17c68 100644
--- a/tracer-samples/pom.xml
+++ b/tracer-samples/pom.xml
@@ -20,6 +20,7 @@
tracer-sample-with-httpclient
tracer-sample-with-h2
tracer-sample-with-resttemplate
+ tracer-sample-with-sampler
diff --git a/tracer-samples/tracer-sample-with-httpclient/src/main/resources/application.properties b/tracer-samples/tracer-sample-with-httpclient/src/main/resources/application.properties
index 7dc15490..194590de 100644
--- a/tracer-samples/tracer-sample-with-httpclient/src/main/resources/application.properties
+++ b/tracer-samples/tracer-sample-with-httpclient/src/main/resources/application.properties
@@ -1,4 +1,7 @@
# Application Name
spring.application.name=HttpClientDemo
# logging path
-logging.path=./logs
\ No newline at end of file
+logging.path=./logs
+
+com.alipay.sofa.tracer.samplerPercentage=0
+com.alipay.sofa.tracer.samplerName=PercentageBasedSampler
\ No newline at end of file
diff --git a/tracer-samples/tracer-sample-with-sampler/README.md b/tracer-samples/tracer-sample-with-sampler/README.md
new file mode 100644
index 00000000..d3de6281
--- /dev/null
+++ b/tracer-samples/tracer-sample-with-sampler/README.md
@@ -0,0 +1,113 @@
+## 使用 SOFATracer 的采样能力
+
+目前 SOFATracer 提供了两种采样模式,一种是基于BitSet实现的基于固定采样率的采样模式;另外一种是提供给用户自定义实现采样的采样模式。下面通过案例来演示如何使用。
+
+本示例基于 tracer-sample-with-springmvc 工程;除 application.properties 之外,其他均相同。
+
+### 基于固定采样率的采样模式
+
+#### 在 application.properties 中增加采样相关配置项
+
+```properties
+#采样率 0~100
+com.alipay.sofa.tracer.samplerPercentage=100
+#采样模式类型名称
+com.alipay.sofa.tracer.samplerName=PercentageBasedSampler
+```
+
+#### 验证方式
+
+* 当采样率设置为100时,每次都会打印摘要日志。
+* 当采样率设置为0时,不打印
+* 当采样率设置为0~100之间时,按概率打印
+
+以请求 10 次来验证下结果。
+
+1、当采样率设置为100时,每次都会打印摘要日志
+
+启动工程,浏览器中输入:http://localhost:8080/springmvc ;并且刷新地址10次,查看日志如下:
+
+```json
+{"time":"2018-11-09 11:54:47.643","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173568757510019269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":68,"current.thread.name":"http-nio-8080-exec-1","baggage":""}
+{"time":"2018-11-09 11:54:50.980","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173569097710029269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":3,"current.thread.name":"http-nio-8080-exec-2","baggage":""}
+{"time":"2018-11-09 11:54:51.542","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173569153910049269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":3,"current.thread.name":"http-nio-8080-exec-4","baggage":""}
+{"time":"2018-11-09 11:54:52.061","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173569205910069269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":2,"current.thread.name":"http-nio-8080-exec-6","baggage":""}
+{"time":"2018-11-09 11:54:52.560","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173569255810089269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":2,"current.thread.name":"http-nio-8080-exec-8","baggage":""}
+{"time":"2018-11-09 11:54:52.977","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173569297610109269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":1,"current.thread.name":"http-nio-8080-exec-10","baggage":""}
+{"time":"2018-11-09 11:54:53.389","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173569338710129269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":2,"current.thread.name":"http-nio-8080-exec-2","baggage":""}
+{"time":"2018-11-09 11:54:53.742","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173569374110149269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":1,"current.thread.name":"http-nio-8080-exec-4","baggage":""}
+{"time":"2018-11-09 11:54:54.142","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173569414010169269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":2,"current.thread.name":"http-nio-8080-exec-6","baggage":""}
+{"time":"2018-11-09 11:54:54.565","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173569456310189269","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":2,"current.thread.name":"http-nio-8080-exec-8","baggage":""}
+```
+
+2、当采样率设置为0时,不打印
+
+启动工程,浏览器中输入:http://localhost:8080/springmvc ;并且刷新地址10次,查看 ./logs/tracerlog/ 目录,没有 spring-mvc-degist.log 日志文件
+
+3、当采样率设置为0~100之间时,按概率打印
+
+这里设置成 20
+
+* 刷新10次请求
+```json
+{"time":"2018-11-09 12:14:29.466","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173686946410159846","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":2,"current.thread.name":"http-nio-8080-exec-5","baggage":""}
+```
+
+* 刷新20次请求
+```json
+{"time":"2018-11-09 12:14:29.466","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173686946410159846","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":2,"current.thread.name":"http-nio-8080-exec-5","baggage":""}
+{"time":"2018-11-09 12:15:21.776","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173692177410319846","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":2,"current.thread.name":"http-nio-8080-exec-2","baggage":""}
+{"time":"2018-11-09 12:15:22.439","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173692243810359846","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":1,"current.thread.name":"http-nio-8080-exec-6","baggage":""}
+{"time":"2018-11-09 12:15:22.817","local.app":"SOFATracerSpringMVC","traceId":"0a0fe8ec154173692281510379846","spanId":"0.1","request.url":"http://localhost:8080/springmvc","method":"GET","result.code":"200","req.size.bytes":-1,"resp.size.bytes":0,"time.cost.milliseconds":2,"current.thread.name":"http-nio-8080-exec-8","baggage":""}
+```
+> 按 20% 进行采样,测试结果仅供参考
+
+### 自定义采样模式
+
+SOFATracer 中提供了一个采样率计算的接口。采样模式需设置为 OpenRulesSampler;当 com.alipay.sofa.tracer.samplerName=OpenRulesSampler时,用户需实现 OpenRulesSampler.Rule 这个抽象类。
+
+#### 在 application.properties 中增加采样相关配置项
+
+
+```properties
+#自定义采样规则实现类全限定名
+com.alipay.sofa.tracer.samplerName.samplerCustomRuleClassName=com.alipay.sofa.tracer.examples.springmvc.sampler.CustomOpenRulesSamplerRuler
+```
+
+#### 自定义采样规则类
+
+```java
+public class CustomOpenRulesSamplerRuler implements Sampler {
+
+ private static final String TYPE = "CustomOpenRulesSamplerRuler";
+
+ @Override
+ public SamplingStatus sample(SofaTracerSpan sofaTracerSpan) {
+ SamplingStatus samplingStatus = new SamplingStatus();
+ Map tags = new HashMap();
+ tags.put(SofaTracerConstant.SAMPLER_TYPE_TAG_KEY, TYPE);
+ tags = Collections.unmodifiableMap(tags);
+ samplingStatus.setTags(tags);
+
+ if (sofaTracerSpan.isServer()) {
+ samplingStatus.setSampled(false);
+ } else {
+ samplingStatus.setSampled(true);
+ }
+ return samplingStatus;
+ }
+
+ @Override
+ public String getType() {
+ return TYPE;
+ }
+
+ @Override
+ public void close() {
+ // do nothing
+ }
+}
+```
+
+在 sample 方法中,用户可以根据当前 sofaTracerSpan 提供的信息来决定是否进行打印。这个案例是通过判断 isServer 来决定是否采样,isServer=true,不采样,否则采样。
+相关实验结果,大家可以自行验证下。
diff --git a/tracer-samples/tracer-sample-with-sampler/pom.xml b/tracer-samples/tracer-sample-with-sampler/pom.xml
new file mode 100644
index 00000000..114ab238
--- /dev/null
+++ b/tracer-samples/tracer-sample-with-sampler/pom.xml
@@ -0,0 +1,17 @@
+
+
+
+ sofa-tracer-samples
+ com.alipay.sofa
+ 2.3.0-SNAPSHOT
+
+
+ 4.0.0
+
+ tracer-sample-with-sampler
+
+
+ ${project.basedir}/../..
+
+
diff --git a/tracer-samples/tracer-sample-with-springmvc/src/main/java/com/alipay/sofa/tracer/examples/springmvc/sampler/CustomOpenRulesSamplerRuler.java b/tracer-samples/tracer-sample-with-springmvc/src/main/java/com/alipay/sofa/tracer/examples/springmvc/sampler/CustomOpenRulesSamplerRuler.java
new file mode 100644
index 00000000..a7306531
--- /dev/null
+++ b/tracer-samples/tracer-sample-with-springmvc/src/main/java/com/alipay/sofa/tracer/examples/springmvc/sampler/CustomOpenRulesSamplerRuler.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alipay.sofa.tracer.examples.springmvc.sampler;
+
+import com.alipay.common.tracer.core.constants.SofaTracerConstant;
+import com.alipay.common.tracer.core.samplers.Sampler;
+import com.alipay.common.tracer.core.samplers.SamplingStatus;
+import com.alipay.common.tracer.core.span.SofaTracerSpan;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ *
+ * CustomOpenRulesSamplerRuler
+ * @author: guolei.sgl
+ * @since: v2.3.0
+ */
+public class CustomOpenRulesSamplerRuler implements Sampler {
+
+ private static final String TYPE = "CustomOpenRulesSamplerRuler";
+
+ @Override
+ public SamplingStatus sample(SofaTracerSpan sofaTracerSpan) {
+ SamplingStatus samplingStatus = new SamplingStatus();
+ Map tags = new HashMap();
+ tags.put(SofaTracerConstant.SAMPLER_TYPE_TAG_KEY, TYPE);
+ tags = Collections.unmodifiableMap(tags);
+ samplingStatus.setTags(tags);
+
+ if (sofaTracerSpan.isServer()) {
+ samplingStatus.setSampled(false);
+ } else {
+ samplingStatus.setSampled(true);
+ }
+ return samplingStatus;
+ }
+
+ @Override
+ public String getType() {
+ return TYPE;
+ }
+
+ @Override
+ public void close() {
+
+ }
+}
diff --git a/tracer-samples/tracer-sample-with-springmvc/src/main/resources/application.properties b/tracer-samples/tracer-sample-with-springmvc/src/main/resources/application.properties
index ac6115ec..cf572725 100644
--- a/tracer-samples/tracer-sample-with-springmvc/src/main/resources/application.properties
+++ b/tracer-samples/tracer-sample-with-springmvc/src/main/resources/application.properties
@@ -18,4 +18,8 @@
# Application Name
spring.application.name=SOFATracerSpringMVC
# logging path
-logging.path=./logs
\ No newline at end of file
+logging.path=./logs
+
+com.alipay.sofa.tracer.samplerPercentage=20
+com.alipay.sofa.tracer.samplerName=PercentageBasedSampler
+#com.alipay.sofa.tracer.samplerCustomRuleClassName=com.alipay.sofa.tracer.examples.springmvc.sampler.CustomOpenRulesSamplerRuler
\ No newline at end of file
diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java
index 360f7054..e8984bc9 100644
--- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java
+++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java
@@ -106,6 +106,20 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
SofaTracerConfiguration.setProperty(
SofaTracerConfiguration.TRACER_SYSTEM_PENETRATE_ATTRIBUTE_MAX_LENGTH,
tempTarget.getBaggageMaxLength());
+
+ //sampler config
+ if (tempTarget.getSamplerName() != null) {
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ tempTarget.getSamplerName());
+ }
+ if (StringUtils.isNotBlank(tempTarget.getSamplerCustomRuleClassName())) {
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_CUSTOM_RULE_CLASS_NAME,
+ tempTarget.getSamplerCustomRuleClassName());
+ }
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY,
+ String.valueOf(tempTarget.getSamplerPercentage()));
}
@Override
diff --git a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/properties/SofaTracerProperties.java b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/properties/SofaTracerProperties.java
index c0627f67..f0f5d9fd 100644
--- a/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/properties/SofaTracerProperties.java
+++ b/tracer-sofa-boot-starter/src/main/java/com/alipay/sofa/tracer/boot/properties/SofaTracerProperties.java
@@ -72,6 +72,10 @@ public class SofaTracerProperties {
private String baggageMaxLength = String
.valueOf(SofaTracerConfiguration.PEN_ATTRS_LENGTH_TRESHOLD);
+ private String samplerName;
+ private float samplerPercentage = 100;
+ private String samplerCustomRuleClassName;
+
public String getDisableDigestLog() {
return disableDigestLog;
}
@@ -119,4 +123,28 @@ public String getBaggageMaxLength() {
public void setBaggageMaxLength(String baggageMaxLength) {
this.baggageMaxLength = baggageMaxLength;
}
+
+ public String getSamplerName() {
+ return samplerName;
+ }
+
+ public void setSamplerName(String samplerName) {
+ this.samplerName = samplerName;
+ }
+
+ public float getSamplerPercentage() {
+ return samplerPercentage;
+ }
+
+ public void setSamplerPercentage(float samplerPercentage) {
+ this.samplerPercentage = samplerPercentage;
+ }
+
+ public String getSamplerCustomRuleClassName() {
+ return samplerCustomRuleClassName;
+ }
+
+ public void setSamplerCustomRuleClassName(String samplerCustomRuleClassName) {
+ this.samplerCustomRuleClassName = samplerCustomRuleClassName;
+ }
}
\ No newline at end of file
diff --git a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/ConfigurationHolderListener.java b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/ConfigurationHolderListener.java
index 6dfa7632..9527b38e 100644
--- a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/ConfigurationHolderListener.java
+++ b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/base/ConfigurationHolderListener.java
@@ -43,5 +43,11 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
sofaTracerProperties.setBaggageMaxLength(SofaTracerConfiguration
.getProperty(SofaTracerConfiguration.TRACER_PENETRATE_ATTRIBUTE_MAX_LENGTH));
ConfigurationHolder.setSofaTracerProperties(sofaTracerProperties);
+ sofaTracerProperties.setSamplerName(SofaTracerConfiguration
+ .getProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY));
+ sofaTracerProperties.setSamplerCustomRuleClassName(SofaTracerConfiguration
+ .getProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_CUSTOM_RULE_CLASS_NAME));
+ sofaTracerProperties.setSamplerPercentage(Float.valueOf(SofaTracerConfiguration
+ .getProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY)));
}
}
\ No newline at end of file
diff --git a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcAsyncTest.java b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcAsyncTest.java
index 9dd70685..859b8d69 100644
--- a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcAsyncTest.java
+++ b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcAsyncTest.java
@@ -16,18 +16,26 @@
*/
package com.alipay.sofa.tracer.boot.springmvc;
+import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
+import com.alipay.common.tracer.core.reporter.digest.manager.SofaTracerDigestReporterAsyncManager;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.sofa.tracer.boot.TestUtil;
import com.alipay.sofa.tracer.boot.base.AbstractTestBase;
import com.alipay.sofa.tracer.boot.base.controller.SampleRestController;
import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcLogEnum;
+import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcTracer;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
+import java.io.File;
+import java.lang.reflect.Field;
+import java.util.HashMap;
import java.util.List;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
/**
@@ -41,6 +49,16 @@ public class SpringMvcAsyncTest extends AbstractTestBase {
@Test
public void testAsyncServlet() throws Exception {
+ //avoid close digest print
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.DISABLE_DIGEST_LOG_KEY,
+ new HashMap());
+
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
+
+ assertNotNull(testRestTemplate);
String restUrl = urlHttpPrefix + "/asyncServlet";
ResponseEntity response = testRestTemplate.getForEntity(restUrl, String.class);
diff --git a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterJsonOutputTest.java b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterJsonOutputTest.java
index 2ebac8dc..eccb12bb 100644
--- a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterJsonOutputTest.java
+++ b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterJsonOutputTest.java
@@ -18,18 +18,25 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
+import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
+import com.alipay.common.tracer.core.reporter.digest.manager.SofaTracerDigestReporterAsyncManager;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.common.tracer.core.reporter.stat.manager.SofaTracerStatisticReporterCycleTimesManager;
import com.alipay.common.tracer.core.reporter.stat.manager.SofaTracerStatisticReporterManager;
import com.alipay.sofa.tracer.boot.TestUtil;
import com.alipay.sofa.tracer.boot.base.AbstractTestBase;
import com.alipay.sofa.tracer.boot.base.controller.SampleRestController;
import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcLogEnum;
+import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcTracer;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.util.Assert;
+import java.io.File;
+import java.lang.reflect.Field;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -46,6 +53,15 @@ public class SpringMvcFilterJsonOutputTest extends AbstractTestBase {
@Test
public void testSofaRestGet() throws Exception {
+ //avoid close digest print
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.DISABLE_DIGEST_LOG_KEY,
+ new HashMap());
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
+
+ assertNotNull(testRestTemplate);
String restUrl = urlHttpPrefix + "/greeting";
int countTimes = 5;
for (int i = 0; i < countTimes; i++) {
diff --git a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterTest.java b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterTest.java
index 3212315b..7ee7f32b 100644
--- a/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterTest.java
+++ b/tracer-sofa-boot-starter/src/test/java/com/alipay/sofa/tracer/boot/springmvc/SpringMvcFilterTest.java
@@ -16,16 +16,23 @@
*/
package com.alipay.sofa.tracer.boot.springmvc;
+import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
+import com.alipay.common.tracer.core.reporter.digest.manager.SofaTracerDigestReporterAsyncManager;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.sofa.tracer.boot.TestUtil;
import com.alipay.sofa.tracer.boot.base.AbstractTestBase;
import com.alipay.sofa.tracer.boot.base.controller.SampleRestController;
import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcLogEnum;
+import com.alipay.sofa.tracer.plugins.springmvc.SpringMvcTracer;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
+import java.io.File;
+import java.lang.reflect.Field;
+import java.util.HashMap;
import java.util.List;
import static org.junit.Assert.assertEquals;
@@ -46,6 +53,15 @@ public class SpringMvcFilterTest extends AbstractTestBase {
@Test
public void testSofaRestGet() throws Exception {
+
+ //avoid close digest print
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.DISABLE_DIGEST_LOG_KEY,
+ new HashMap());
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
+
assertNotNull(testRestTemplate);
String restUrl = urlHttpPrefix + "/greeting";
diff --git a/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/base/AbstractTestBase.java b/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/base/AbstractTestBase.java
index ec0995ce..39c51d71 100644
--- a/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/base/AbstractTestBase.java
+++ b/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/base/AbstractTestBase.java
@@ -203,7 +203,7 @@ public SofaTracerSpan recoverServerSpan(String serverSpanId) {
String traceId = TraceIdGenerator.generate();
//默认不采样
SofaTracerSpanContext spanContext = new SofaTracerSpanContext(traceId, serverSpanId,
- StringUtils.EMPTY_STRING, false);
+ StringUtils.EMPTY_STRING, true);
String callServiceName = "callServiceName";
//create server
diff --git a/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/core/sofatracer/SofaTracerDemoTest.java b/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/core/sofatracer/SofaTracerDemoTest.java
index 52b396c9..384011f6 100644
--- a/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/core/sofatracer/SofaTracerDemoTest.java
+++ b/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/core/sofatracer/SofaTracerDemoTest.java
@@ -16,13 +16,19 @@
*/
package com.alipay.common.tracer.test.core.sofatracer;
+import com.alipay.common.tracer.core.SofaTracer;
+import com.alipay.common.tracer.core.appender.builder.XStringBuilder;
+
+import com.alipay.common.tracer.core.context.span.SofaTracerSpanContext;
import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
+import com.alipay.common.tracer.core.generator.TraceIdGenerator;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.test.TestUtil;
import com.alipay.common.tracer.test.base.AbstractTestBase;
import com.alipay.common.tracer.test.core.sofatracer.type.TracerTestLogEnum;
+import io.opentracing.tag.Tags;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
diff --git a/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/core/sofatracer/SofaTracerStatisticsDemoTest.java b/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/core/sofatracer/SofaTracerStatisticsDemoTest.java
index 4a2d9ee5..ba4c06e2 100644
--- a/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/core/sofatracer/SofaTracerStatisticsDemoTest.java
+++ b/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/core/sofatracer/SofaTracerStatisticsDemoTest.java
@@ -16,8 +16,10 @@
*/
package com.alipay.common.tracer.test.core.sofatracer;
+import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.context.trace.SofaTraceContext;
import com.alipay.common.tracer.core.holder.SofaTraceContextHolder;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.test.TestUtil;
import com.alipay.common.tracer.test.base.AbstractTestBase;
@@ -41,6 +43,12 @@ public class SofaTracerStatisticsDemoTest extends AbstractTestBase {
@Before
public void beforeTest() throws Exception {
+
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
+
File f = customFileLog(TracerTestLogEnum.RPC_SERVER_DIGEST.getDefaultLogName());
if (f.exists()) {
FileUtils.writeStringToFile(f, "");
diff --git a/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/demo/DemoTracerTest.java b/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/demo/DemoTracerTest.java
index e98cc6d7..43b1df9a 100644
--- a/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/demo/DemoTracerTest.java
+++ b/tracer-test/core-test/src/test/java/com/alipay/common/tracer/test/demo/DemoTracerTest.java
@@ -17,7 +17,9 @@
package com.alipay.common.tracer.test.demo;
import com.alipay.common.tracer.core.SofaTracer;
+import com.alipay.common.tracer.core.configuration.SofaTracerConfiguration;
import com.alipay.common.tracer.core.reporter.digest.DiskReporterImpl;
+import com.alipay.common.tracer.core.samplers.SofaTracerPercentageBasedSampler;
import com.alipay.common.tracer.core.span.SofaTracerSpan;
import com.alipay.common.tracer.test.TestUtil;
import com.alipay.common.tracer.test.base.AbstractTestBase;
@@ -28,6 +30,7 @@
import org.junit.Before;
import org.junit.Test;
+import java.io.IOException;
import java.util.List;
import static org.junit.Assert.assertEquals;
@@ -43,7 +46,13 @@ public class DemoTracerTest extends AbstractTestBase {
private SofaTracer sofaTracer;
@Before
- public void beforeInstance() {
+ public void beforeInstance() throws IOException {
+
+ SofaTracerConfiguration.setProperty(SofaTracerConfiguration.SAMPLER_STRATEGY_NAME_KEY,
+ SofaTracerPercentageBasedSampler.TYPE);
+ SofaTracerConfiguration.setProperty(
+ SofaTracerConfiguration.SAMPLER_STRATEGY_PERCENTAGE_KEY, "100");
+
//client
DiskReporterImpl clientReporter = new DiskReporterImpl("client-digest.log",
new ClientSpanEncoder());