Skip to content

Commit

Permalink
Fix merging error.
Browse files Browse the repository at this point in the history
  • Loading branch information
songy23 committed May 29, 2019
1 parent 2926f4a commit 61e59d7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;
import io.opentelemetry.trace.AttributeValue;
import io.opentelemetry.trace.Sampler;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanContext;
import io.opentelemetry.trace.SpanId;
import io.opentelemetry.trace.TraceId;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

Expand Down Expand Up @@ -71,7 +74,7 @@ public static ProbabilitySampler create(double probability) {
}

@Override
public final boolean shouldSample(
public final Decision shouldSample(
@Nullable SpanContext parentContext,
@Nullable Boolean hasRemoteParent,
TraceId traceId,
Expand All @@ -80,13 +83,13 @@ public final boolean shouldSample(
@Nullable List<Span> parentLinks) {
// If the parent is sampled keep the sampling decision.
if (parentContext != null && parentContext.getTraceOptions().isSampled()) {
return true;
return new SimpleDecision(true);
}
if (parentLinks != null) {
// If any parent link is sampled keep the sampling decision.
for (Span parentLink : parentLinks) {
if (parentLink.getContext().getTraceOptions().isSampled()) {
return true;
return new SimpleDecision(true);
}
}
}
Expand All @@ -97,11 +100,36 @@ public final boolean shouldSample(
// while allowing for a (very) small chance of *not* sampling if the id == Long.MAX_VALUE.
// This is considered a reasonable tradeoff for the simplicity/performance requirements (this
// code is executed in-line for every Span creation).
return Math.abs(traceId.getLowerLong()) < getIdUpperBound();
return new SimpleDecision(Math.abs(traceId.getLowerLong()) < getIdUpperBound());
}

@Override
public final String getDescription() {
return String.format("ProbabilitySampler{%.6f}", getProbability());
}

/** Sampling decision without attributes. */
private static final class SimpleDecision implements Decision {

private final boolean decision;

/**
* Creates sampling decision without attributes.
*
* @param decision sampling decision
*/
SimpleDecision(boolean decision) {
this.decision = decision;
}

@Override
public boolean isSampled() {
return decision;
}

@Override
public Map<String, AttributeValue> attributes() {
return Collections.emptyMap();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ private static void assertSamplerSamplesWithProbability(
Sampler sampler, SpanContext parent, List<Span> parentLinks, double probability) {
int count = 0; // Count of spans with sampling enabled
for (int i = 0; i < NUM_SAMPLE_TRIES; i++) {
if (sampler.shouldSample(
parent, false, generateRandomTraceId(), generateRandomSpanId(), SPAN_NAME, parentLinks)) {
if (sampler
.shouldSample(
parent,
false,
generateRandomTraceId(),
generateRandomSpanId(),
SPAN_NAME,
parentLinks)
.isSampled()) {
count++;
}
}
Expand Down Expand Up @@ -210,13 +217,15 @@ public void probabilitySampler_SampleBasedOnTraceId() {
},
0);
assertThat(
defaultProbability.shouldSample(
null,
false,
notSampledtraceId,
generateRandomSpanId(),
SPAN_NAME,
Collections.<Span>emptyList()))
defaultProbability
.shouldSample(
null,
false,
notSampledtraceId,
generateRandomSpanId(),
SPAN_NAME,
Collections.<Span>emptyList())
.isSampled())
.isFalse();
// This traceId will be sampled by the ProbabilitySampler because the first 8 bytes as long
// is less than probability * Long.MAX_VALUE;
Expand All @@ -242,13 +251,15 @@ public void probabilitySampler_SampleBasedOnTraceId() {
},
0);
assertThat(
defaultProbability.shouldSample(
null,
false,
sampledtraceId,
generateRandomSpanId(),
SPAN_NAME,
Collections.<Span>emptyList()))
defaultProbability
.shouldSample(
null,
false,
sampledtraceId,
generateRandomSpanId(),
SPAN_NAME,
Collections.<Span>emptyList())
.isSampled())
.isTrue();
}

Expand Down

0 comments on commit 61e59d7

Please sign in to comment.