Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterF778 committed Aug 30, 2024
1 parent f242459 commit 23690a4
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 316 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,96 +5,14 @@

package io.opentelemetry.contrib.sampler.consistent56;

import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.getInvalidRandomValue;
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.isValidThreshold;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.data.LinkData;
import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import java.util.List;

/** Abstract base class for composable consistent samplers. */
public abstract class ComposableSampler extends ConsistentSampler {

@Override
public final SamplingResult shouldSample(
Context parentContext,
String traceId,
String name,
SpanKind spanKind,
Attributes attributes,
List<LinkData> parentLinks) {
Span parentSpan = Span.fromContext(parentContext);
SpanContext parentSpanContext = parentSpan.getSpanContext();

TraceState parentTraceState = parentSpanContext.getTraceState();
String otelTraceStateString = parentTraceState.get(OtelTraceState.TRACE_STATE_KEY);
OtelTraceState otelTraceState = OtelTraceState.parse(otelTraceStateString);

SamplingIntent intent =
getSamplingIntent(parentContext, name, spanKind, attributes, parentLinks);
long threshold = intent.getThreshold();

// determine sampling decision
boolean isSampled;
if (isValidThreshold(threshold)) {
long randomness = getRandomness(otelTraceState, traceId);
isSampled = threshold <= randomness;
} else { // DROP
isSampled = false;
}

SamplingDecision samplingDecision;
if (isSampled) {
samplingDecision = SamplingDecision.RECORD_AND_SAMPLE;
otelTraceState.setThreshold(threshold);
} else {
samplingDecision = SamplingDecision.DROP;
otelTraceState.invalidateThreshold();
}

String newOtTraceState = otelTraceState.serialize();

return new SamplingResult() {

@Override
public SamplingDecision getDecision() {
return samplingDecision;
}

@Override
public Attributes getAttributes() {
return intent.getAttributes();
}

@Override
public TraceState getUpdatedTraceState(TraceState parentTraceState) {
return intent.updateTraceState(parentTraceState).toBuilder()
.put(OtelTraceState.TRACE_STATE_KEY, newOtTraceState)
.build();
}
};
}

private static long getRandomness(OtelTraceState otelTraceState, String traceId) {
if (otelTraceState.hasValidRandomValue()) {
return otelTraceState.getRandomValue();
} else {
return OtelTraceState.parseHex(traceId, 18, 14, getInvalidRandomValue());
}
}

@Override
protected final long getThreshold(long parentThreshold, boolean isRoot) {
// not used by Composable Samplers
throw new UnsupportedOperationException();
}
/** An interface for components to be used by composite consistent probability samplers. */
public interface ComposableSampler {

/**
* Returns the SamplingIntent that is used for the sampling decision. The SamplingIntent includes
Expand All @@ -105,10 +23,13 @@ protected final long getThreshold(long parentThreshold, boolean isRoot) {
* not be used for the calculation of the threshold as the sampled flag depends itself on the
* random value.
*/
protected abstract SamplingIntent getSamplingIntent(
SamplingIntent getSamplingIntent(
Context parentContext,
String name,
SpanKind spanKind,
Attributes attributes,
List<LinkData> parentLinks);

/** Return the string providing a description of the implementation */
String getDescription();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import javax.annotation.concurrent.Immutable;

@Immutable
final class ConsistentAlwaysOffSampler extends ComposableSampler {
final class ConsistentAlwaysOffSampler extends ConsistentSampler {

private static final ConsistentAlwaysOffSampler INSTANCE = new ConsistentAlwaysOffSampler();

Expand All @@ -26,7 +26,7 @@ static ConsistentAlwaysOffSampler getInstance() {
}

@Override
protected SamplingIntent getSamplingIntent(
public SamplingIntent getSamplingIntent(
Context parentContext,
String name,
SpanKind spanKind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import javax.annotation.concurrent.Immutable;

@Immutable
final class ConsistentAlwaysOnSampler extends ComposableSampler {
final class ConsistentAlwaysOnSampler extends ConsistentSampler {

private static final ConsistentAlwaysOnSampler INSTANCE = new ConsistentAlwaysOnSampler();

Expand All @@ -26,7 +26,7 @@ static ConsistentAlwaysOnSampler getInstance() {
}

@Override
protected SamplingIntent getSamplingIntent(
public SamplingIntent getSamplingIntent(
Context parentContext,
String name,
SpanKind spanKind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* uses the minimum threshold value received.
*/
@Immutable
final class ConsistentAnyOf extends ComposableSampler {
final class ConsistentAnyOf extends ConsistentSampler {

private final ComposableSampler[] delegates;

Expand Down Expand Up @@ -53,7 +53,7 @@ final class ConsistentAnyOf extends ComposableSampler {
}

@Override
protected SamplingIntent getSamplingIntent(
public SamplingIntent getSamplingIntent(
Context parentContext,
String name,
SpanKind spanKind,
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import io.opentelemetry.sdk.trace.data.LinkData;
import java.util.List;

public class ConsistentFixedThresholdSampler extends ComposableSampler {
public class ConsistentFixedThresholdSampler extends ConsistentSampler {

private final long threshold;
private final String description;
Expand Down Expand Up @@ -49,7 +49,7 @@ public String getDescription() {
}

@Override
protected SamplingIntent getSamplingIntent(
public SamplingIntent getSamplingIntent(
Context parentContext,
String name,
SpanKind spanKind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* sampling decision is delegated to the root sampler.
*/
@Immutable
final class ConsistentParentBasedSampler extends ComposableSampler {
final class ConsistentParentBasedSampler extends ConsistentSampler {

private final ComposableSampler rootSampler;

Expand All @@ -42,7 +42,7 @@ final class ConsistentParentBasedSampler extends ComposableSampler {
}

@Override
protected SamplingIntent getSamplingIntent(
public SamplingIntent getSamplingIntent(
Context parentContext,
String name,
SpanKind spanKind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
*
* <p>{@code 1 / (adaptationTimeSeconds * targetSpansPerSecondLimit)}
*/
final class ConsistentRateLimitingSampler extends ComposableSampler {
final class ConsistentRateLimitingSampler extends ConsistentSampler {

private static final double NANOS_IN_SECONDS = 1e-9;

Expand Down Expand Up @@ -189,7 +189,7 @@ private State updateState(State oldState, long currentNanoTime, double delegateP
}

@Override
protected SamplingIntent getSamplingIntent(
public SamplingIntent getSamplingIntent(
Context parentContext,
String name,
SpanKind spanKind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* ComposableSamplers into PredicatedSamplers.
*/
@Immutable
final class ConsistentRuleBasedSampler extends ComposableSampler {
final class ConsistentRuleBasedSampler extends ConsistentSampler {

@Nullable private final SpanKind spanKindToMatch;
private final PredicatedSampler[] samplers;
Expand Down Expand Up @@ -57,7 +57,7 @@ private ConsistentRuleBasedSampler(
}

@Override
protected SamplingIntent getSamplingIntent(
public SamplingIntent getSamplingIntent(
Context parentContext,
String name,
SpanKind spanKind,
Expand Down
Loading

0 comments on commit 23690a4

Please sign in to comment.