Skip to content

Commit

Permalink
Merge branch 'master' into add_eagleeye
Browse files Browse the repository at this point in the history
  • Loading branch information
khotyn committed Dec 11, 2018
2 parents 0d2c6b3 + 9ccc159 commit 6b27c1f
Show file tree
Hide file tree
Showing 34 changed files with 545 additions and 37 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## 六、文档

Expand Down
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<artifactId>tracer-all-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>tracer-all-parent</name>
<description>Alipay SOFATracer Log Implemented by OpenTracing</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
//客户端、服务端
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -456,6 +474,11 @@ public Builder withTags(Map<String, ?> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> entry : spanContext.getSysBaggage().entrySet()) {
String key = BAGGAGE_SYS_KEY_PREFIX + StringUtils.escapePercentEqualAnd(entry.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Object> tags = new HashMap<String, Object>();
tags.put(SofaTracerConstant.SAMPLER_TYPE_TAG_KEY, TYPE);
Expand Down
Loading

0 comments on commit 6b27c1f

Please sign in to comment.