Skip to content

Commit

Permalink
[#noissue] refactor collector span sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
donghun-cho committed May 14, 2024
1 parent d242ae3 commit 2a60bf1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public class CollectorProperties {
@Value("${collector.span.sampling.type:MOD}")
private String spanSamplingType;
@Value("${collector.span.sampling.mod.sampling-rate:1}")
private long spanSamplingRate;
private long spanModSamplingRate;
@Value("${collector.span.sampling.percent.sampling-rate:100}")
private String spanSamplingPercent;
private String spanPercentSamplingRate;

@Value("${collector.stat.uri:false}")
private boolean uriStatEnable;
Expand Down Expand Up @@ -97,20 +97,20 @@ public void setSpanSamplingType(String spanSamplingType) {
this.spanSamplingType = spanSamplingType;
}

public void setSpanSamplingRate(int spanSamplingRate) {
this.spanSamplingRate = spanSamplingRate;
public void setSpanModSamplingRate(int spanModSamplingRate) {
this.spanModSamplingRate = spanModSamplingRate;

Check warning on line 101 in collector/src/main/java/com/navercorp/pinpoint/collector/config/CollectorProperties.java

View check run for this annotation

Codecov / codecov/patch

collector/src/main/java/com/navercorp/pinpoint/collector/config/CollectorProperties.java#L101

Added line #L101 was not covered by tests
}

public long getSpanSamplingRate() {
return spanSamplingRate;
public long getSpanModSamplingRate() {
return spanModSamplingRate;

Check warning on line 105 in collector/src/main/java/com/navercorp/pinpoint/collector/config/CollectorProperties.java

View check run for this annotation

Codecov / codecov/patch

collector/src/main/java/com/navercorp/pinpoint/collector/config/CollectorProperties.java#L105

Added line #L105 was not covered by tests
}

public String getSpanSamplingPercent() {
return spanSamplingPercent;
public String getSpanPercentSamplingRate() {
return spanPercentSamplingRate;

Check warning on line 109 in collector/src/main/java/com/navercorp/pinpoint/collector/config/CollectorProperties.java

View check run for this annotation

Codecov / codecov/patch

collector/src/main/java/com/navercorp/pinpoint/collector/config/CollectorProperties.java#L109

Added line #L109 was not covered by tests
}

public void setSpanSamplingPercent(String spanSamplingPercent) {
this.spanSamplingPercent = spanSamplingPercent;
public void setSpanPercentSamplingRate(String spanPercentSamplingRate) {
this.spanPercentSamplingRate = spanPercentSamplingRate;

Check warning on line 113 in collector/src/main/java/com/navercorp/pinpoint/collector/config/CollectorProperties.java

View check run for this annotation

Codecov / codecov/patch

collector/src/main/java/com/navercorp/pinpoint/collector/config/CollectorProperties.java#L113

Added line #L113 was not covered by tests
}

public boolean isUriStatEnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ public class SimpleSpanSamplerFactory implements SpanSamplerFactory {
private final Logger logger = LogManager.getLogger(this.getClass());

private final boolean spanSamplerEnable;
private final SamplerType spanSamplerType;
private final long spanModSamplerRate;
private final String spanPercentageSamplerRate;
private final String spanSamplerType;
private final long spanModSamplingRate;
private final String spanPercentSamplingRateStr;

public SimpleSpanSamplerFactory(CollectorProperties collectorProperties) {
Objects.requireNonNull(collectorProperties, "collectorProperties");
this.spanSamplerEnable = collectorProperties.isSpanSamplingEnable();
this.spanSamplerType = SamplerType.of(collectorProperties.getSpanSamplingType());
this.spanModSamplerRate = collectorProperties.getSpanSamplingRate();
this.spanPercentageSamplerRate = collectorProperties.getSpanSamplingPercent();
this.spanSamplerType = collectorProperties.getSpanSamplingType();
this.spanModSamplingRate = collectorProperties.getSpanModSamplingRate();
this.spanPercentSamplingRateStr = collectorProperties.getSpanPercentSamplingRate();
}

@Override
public Sampler<BasicSpan> createBasicSpanSampler() {
if (spanSamplerEnable) {
try {
switch (spanSamplerType) {
switch (SamplerType.of(spanSamplerType)) {
case PERCENT:
return createPercentageSampler(spanPercentageSamplerRate, createBasicFunction());
return createPercentageSampler(spanPercentSamplingRateStr, createBasicSpanSamplingFunction());
case MOD:
return createModSampler(spanModSamplerRate, createBasicFunction());
return createModSampler(spanModSamplingRate, createBasicSpanSamplingFunction());
default:
break;
}
Expand All @@ -44,26 +44,26 @@ public Sampler<BasicSpan> createBasicSpanSampler() {
return TrueSampler.instance();
}

private ToLongFunction<BasicSpan> createBasicFunction() {
private ToLongFunction<BasicSpan> createBasicSpanSamplingFunction() {
return (span -> span.getTransactionId().getTransactionSequence());
}

private Sampler<BasicSpan> createPercentageSampler(String spanSamplerPercentageStr,
private Sampler<BasicSpan> createPercentageSampler(String percentSamplingRateStr,
ToLongFunction<BasicSpan> function) {
long spanSamplerPercentage = PercentRateSampler.parseSamplingRateString(spanSamplerPercentageStr);
if (spanSamplerPercentage == 0) {
return FalseSampler.instance();
} else if (spanSamplerPercentage == PercentRateSampler.MAX) {
long percentSamplingRate = PercentRateSampler.parseSamplingRateString(percentSamplingRateStr);
if (percentSamplingRate >= PercentRateSampler.MAX) {
return TrueSampler.instance();
} else if (percentSamplingRate <= 0) {
return FalseSampler.instance();
}
return new PercentRateSampler<>(spanSamplerPercentage, function);
return new PercentRateSampler<>(percentSamplingRate, function);
}

private Sampler<BasicSpan> createModSampler(long spanModSamplerRate,
private Sampler<BasicSpan> createModSampler(long modSamplingRate,
ToLongFunction<BasicSpan> function) {
if (spanModSamplerRate == 1) {
if (modSamplingRate == 1) {
return TrueSampler.instance();
}
return new ModSampler<>(spanModSamplerRate, function);
return new ModSampler<>(modSamplingRate, function);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,8 @@ pinpoint.banner.configs=spring.active.profile,\
hbase.namespace,\
spring.data.redis.host,\
spring.data.redis.port,\
spring.data.redis.cluster.nodes
spring.data.redis.cluster.nodes,\
collector.span.sampling.enable,\
collector.span.sampling.type,\
collector.span.sampling.mod.sampling-rate,\
collector.span.sampling.percent.sampling-rate
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public void modSamplerConstructorCheckTest() {

@Test
public void percentageSamplerTest() {
long samplingRate = 500; // 5%
String percentSamplingRateStr = "5.0"; // 5%
long samplingRate = PercentRateSampler.parseSamplingRateString(percentSamplingRateStr);

Sampler<Long> sampler = new PercentRateSampler<>(samplingRate, identityFunction);
for (long i = 0L; i < 10L; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void defaultSamplingProperties() {
CollectorProperties mockProperties = mock(CollectorProperties.class);
when(mockProperties.isSpanSamplingEnable()).thenReturn(false);
when(mockProperties.getSpanSamplingType()).thenReturn(SamplerType.MOD.name());
when(mockProperties.getSpanSamplingRate()).thenReturn(1L);
when(mockProperties.getSpanSamplingPercent()).thenReturn("0");
when(mockProperties.getSpanModSamplingRate()).thenReturn(1L);
when(mockProperties.getSpanPercentSamplingRate()).thenReturn("0");

this.mockProperties = mockProperties;
}
Expand All @@ -34,7 +34,7 @@ public void defaultSamplingProperties() {
public void TransactionIdSampleTest() {
when(mockProperties.isSpanSamplingEnable()).thenReturn(true);
when(mockProperties.getSpanSamplingType()).thenReturn(SamplerType.MOD.name());
when(mockProperties.getSpanSamplingRate()).thenReturn(5L);
when(mockProperties.getSpanModSamplingRate()).thenReturn(5L);
SpanSamplerFactory spanSamplerFactory = new SimpleSpanSamplerFactory(mockProperties);
Sampler<BasicSpan> sampler = spanSamplerFactory.createBasicSpanSampler();

Expand Down Expand Up @@ -71,7 +71,7 @@ public void disableTest() {
public void modSamplerTest() {
when(mockProperties.isSpanSamplingEnable()).thenReturn(true);
when(mockProperties.getSpanSamplingType()).thenReturn(SamplerType.MOD.name());
when(mockProperties.getSpanSamplingRate()).thenReturn(5L);
when(mockProperties.getSpanModSamplingRate()).thenReturn(5L);
SpanSamplerFactory spanSamplerFactory = new SimpleSpanSamplerFactory(mockProperties);
Sampler<?> sampler = spanSamplerFactory.createBasicSpanSampler();

Expand All @@ -82,7 +82,7 @@ public void modSamplerTest() {
public void percentageSamplerTest() {
when(mockProperties.isSpanSamplingEnable()).thenReturn(true);
when(mockProperties.getSpanSamplingType()).thenReturn(SamplerType.PERCENT.name());
when(mockProperties.getSpanSamplingPercent()).thenReturn("20");
when(mockProperties.getSpanPercentSamplingRate()).thenReturn("20");
SpanSamplerFactory spanSamplerFactory = new SimpleSpanSamplerFactory(mockProperties);
Sampler<?> sampler = spanSamplerFactory.createBasicSpanSampler();

Expand All @@ -93,7 +93,7 @@ public void percentageSamplerTest() {
public void trueSamplerTest1() {
when(mockProperties.isSpanSamplingEnable()).thenReturn(true);
when(mockProperties.getSpanSamplingType()).thenReturn(SamplerType.MOD.name());
when(mockProperties.getSpanSamplingRate()).thenReturn(1L);
when(mockProperties.getSpanModSamplingRate()).thenReturn(1L);
SpanSamplerFactory spanSamplerFactory = new SimpleSpanSamplerFactory(mockProperties);
Sampler<?> sampler = spanSamplerFactory.createBasicSpanSampler();

Expand All @@ -104,7 +104,7 @@ public void trueSamplerTest1() {
public void trueSamplerTest2() {
when(mockProperties.isSpanSamplingEnable()).thenReturn(true);
when(mockProperties.getSpanSamplingType()).thenReturn(SamplerType.PERCENT.name());
when(mockProperties.getSpanSamplingPercent()).thenReturn("100");
when(mockProperties.getSpanPercentSamplingRate()).thenReturn("100");
SpanSamplerFactory spanSamplerFactory = new SimpleSpanSamplerFactory(mockProperties);
Sampler<?> sampler = spanSamplerFactory.createBasicSpanSampler();

Expand All @@ -115,7 +115,7 @@ public void trueSamplerTest2() {
public void falseSamplerTest1() {
when(mockProperties.isSpanSamplingEnable()).thenReturn(true);
when(mockProperties.getSpanSamplingType()).thenReturn(SamplerType.PERCENT.name());
when(mockProperties.getSpanSamplingPercent()).thenReturn("0");
when(mockProperties.getSpanPercentSamplingRate()).thenReturn("0");
SpanSamplerFactory spanSamplerFactory = new SimpleSpanSamplerFactory(mockProperties);
Sampler<?> sampler = spanSamplerFactory.createBasicSpanSampler();

Expand Down

0 comments on commit 2a60bf1

Please sign in to comment.