forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Composite Samplers prototype (open-telemetry#1443)
Co-authored-by: Otmar Ertl <otmar.ertl@gmail.com>
- Loading branch information
Showing
22 changed files
with
1,264 additions
and
248 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
...mpling/src/main/java/io/opentelemetry/contrib/sampler/consistent56/ComposableSampler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler.consistent56; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.trace.SpanKind; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.data.LinkData; | ||
import java.util.List; | ||
|
||
/** 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 | ||
* the threshold value which will be used for the sampling decision. | ||
* | ||
* <p>NOTE: Keep in mind, that in any case the returned threshold value must not depend directly | ||
* or indirectly on the random value. In particular this means that the parent sampled flag must | ||
* not be used for the calculation of the threshold as the sampled flag depends itself on the | ||
* random value. | ||
*/ | ||
SamplingIntent getSamplingIntent( | ||
Context parentContext, | ||
String name, | ||
SpanKind spanKind, | ||
Attributes attributes, | ||
List<LinkData> parentLinks); | ||
|
||
/** Return the string providing a description of the implementation. */ | ||
String getDescription(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...sampling/src/main/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentAnyOf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler.consistent56; | ||
|
||
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.getInvalidThreshold; | ||
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.isValidThreshold; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.AttributesBuilder; | ||
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 java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* A consistent sampler that queries all its delegate samplers for their sampling threshold, and | ||
* uses the minimum threshold value received. | ||
*/ | ||
@Immutable | ||
final class ConsistentAnyOf extends ConsistentSampler { | ||
|
||
private final ComposableSampler[] delegates; | ||
|
||
private final String description; | ||
|
||
/** | ||
* Constructs a new consistent AnyOf sampler using the provided delegate samplers. | ||
* | ||
* @param delegates the delegate samplers | ||
*/ | ||
ConsistentAnyOf(@Nullable ComposableSampler... delegates) { | ||
if (delegates == null || delegates.length == 0) { | ||
throw new IllegalArgumentException( | ||
"At least one delegate must be specified for ConsistentAnyOf"); | ||
} | ||
|
||
this.delegates = delegates; | ||
|
||
this.description = | ||
Stream.of(delegates) | ||
.map(Object::toString) | ||
.collect(Collectors.joining(",", "ConsistentAnyOf{", "}")); | ||
} | ||
|
||
@Override | ||
public SamplingIntent getSamplingIntent( | ||
Context parentContext, | ||
String name, | ||
SpanKind spanKind, | ||
Attributes attributes, | ||
List<LinkData> parentLinks) { | ||
|
||
SamplingIntent[] intents = new SamplingIntent[delegates.length]; | ||
int k = 0; | ||
long minimumThreshold = getInvalidThreshold(); | ||
for (ComposableSampler delegate : delegates) { | ||
SamplingIntent delegateIntent = | ||
delegate.getSamplingIntent(parentContext, name, spanKind, attributes, parentLinks); | ||
long delegateThreshold = delegateIntent.getThreshold(); | ||
if (isValidThreshold(delegateThreshold)) { | ||
if (isValidThreshold(minimumThreshold)) { | ||
minimumThreshold = Math.min(delegateThreshold, minimumThreshold); | ||
} else { | ||
minimumThreshold = delegateThreshold; | ||
} | ||
} | ||
intents[k++] = delegateIntent; | ||
} | ||
|
||
long resultingThreshold = minimumThreshold; | ||
|
||
return new SamplingIntent() { | ||
@Override | ||
public long getThreshold() { | ||
return resultingThreshold; | ||
} | ||
|
||
@Override | ||
public Attributes getAttributes() { | ||
AttributesBuilder builder = Attributes.builder(); | ||
for (SamplingIntent intent : intents) { | ||
builder = builder.putAll(intent.getAttributes()); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public TraceState updateTraceState(TraceState previousState) { | ||
for (SamplingIntent intent : intents) { | ||
previousState = intent.updateTraceState(previousState); | ||
} | ||
return previousState; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
...main/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentComposedAndSampler.java
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
.../main/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentComposedOrSampler.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.