-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ProbabilitySampler in SDK. #337
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright 2019, OpenTelemetry Authors | ||
* | ||
* Licensed 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 io.opentelemetry.sdk.trace.samplers; | ||
|
||
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; | ||
|
||
/** | ||
* We assume the lower 64 bits of the traceId's are randomly distributed around the whole (long) | ||
* range. We convert an incoming probability into an upper bound on that value, such that we can | ||
* just compare the absolute value of the id and the bound to see if we are within the desired | ||
* probability range. Using the low bits of the traceId also ensures that systems that only use 64 | ||
* bit ID's will also work with this sampler. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice comment. Is there any chance the assumption about the random distribution might fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot think of any - @bogdandrutu may have a better example here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be also documented into the specification + @c24t, because we need all the implementations to be consistent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spec issue: open-telemetry/opentelemetry-specification#31. |
||
*/ | ||
@AutoValue | ||
@Immutable | ||
public abstract class ProbabilitySampler implements Sampler { | ||
|
||
ProbabilitySampler() {} | ||
|
||
abstract double getProbability(); | ||
|
||
abstract long getIdUpperBound(); | ||
|
||
/** | ||
* Returns a new {@link ProbabilitySampler}. The probability of sampling a trace is equal to that | ||
* of the specified probability. | ||
* | ||
* @param probability The desired probability of sampling. Must be within [0.0, 1.0]. | ||
* @return a new {@link ProbabilitySampler}. | ||
* @throws IllegalArgumentException if {@code probability} is out of range | ||
*/ | ||
public static ProbabilitySampler create(double probability) { | ||
Preconditions.checkArgument( | ||
probability >= 0.0 && probability <= 1.0, "probability must be in range [0.0, 1.0]"); | ||
long idUpperBound; | ||
// Special case the limits, to avoid any possible issues with lack of precision across | ||
// double/long boundaries. For probability == 0.0, we use Long.MIN_VALUE as this guarantees | ||
// that we will never sample a trace, even in the case where the id == Long.MIN_VALUE, since | ||
// Math.Abs(Long.MIN_VALUE) == Long.MIN_VALUE. | ||
if (probability == 0.0) { | ||
idUpperBound = Long.MIN_VALUE; | ||
} else if (probability == 1.0) { | ||
idUpperBound = Long.MAX_VALUE; | ||
} else { | ||
idUpperBound = (long) (probability * Long.MAX_VALUE); | ||
} | ||
return new AutoValue_ProbabilitySampler(probability, idUpperBound); | ||
} | ||
|
||
@Override | ||
public final Decision shouldSample( | ||
@Nullable SpanContext parentContext, | ||
@Nullable Boolean hasRemoteParent, | ||
TraceId traceId, | ||
SpanId spanId, | ||
String name, | ||
@Nullable List<Span> parentLinks) { | ||
// If the parent is sampled keep the sampling decision. | ||
if (parentContext != null && parentContext.getTraceOptions().isSampled()) { | ||
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 new SimpleDecision(true); | ||
} | ||
} | ||
} | ||
// Always sample if we are within probability range. This is true even for child spans (that | ||
// may have had a different sampling decision made) to allow for different sampling policies, | ||
// and dynamic increases to sampling probabilities for debugging purposes. | ||
// Note use of '<' for comparison. This ensures that we never sample for probability == 0.0, | ||
// 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 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(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@since 0.1.0
is missing here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems none of the SDK classes have the
@since
tag. I'll wait to see our decision on #344.