-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
consistent sampler prototypes using 56 bits of randomness (#1063)
- Loading branch information
Showing
21 changed files
with
2,086 additions
and
1 deletion.
There are no files selected for viewing
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
26 changes: 26 additions & 0 deletions
26
...c/main/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentAlwaysOffSampler.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,26 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler.consistent56; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
@Immutable | ||
final class ConsistentAlwaysOffSampler extends ConsistentSampler { | ||
|
||
ConsistentAlwaysOffSampler(RandomValueGenerator randomValueGenerator) { | ||
super(randomValueGenerator); | ||
} | ||
|
||
@Override | ||
protected long getThreshold(long parentThreshold, boolean isRoot) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "ConsistentAlwaysOffSampler"; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...rc/main/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentAlwaysOnSampler.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,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler.consistent56; | ||
|
||
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.getMaxThreshold; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
@Immutable | ||
final class ConsistentAlwaysOnSampler extends ConsistentSampler { | ||
|
||
ConsistentAlwaysOnSampler(RandomValueGenerator randomValueGenerator) { | ||
super(randomValueGenerator); | ||
} | ||
|
||
@Override | ||
protected long getThreshold(long parentThreshold, boolean isRoot) { | ||
return getMaxThreshold(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "ConsistentAlwaysOnSampler"; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...main/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentComposedAndSampler.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,56 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler.consistent56; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* A consistent sampler composed of two consistent samplers. | ||
* | ||
* <p>This sampler samples if both samplers would sample. | ||
*/ | ||
@Immutable | ||
final class ConsistentComposedAndSampler extends ConsistentSampler { | ||
|
||
private final ConsistentSampler sampler1; | ||
private final ConsistentSampler sampler2; | ||
private final String description; | ||
|
||
ConsistentComposedAndSampler( | ||
ConsistentSampler sampler1, | ||
ConsistentSampler sampler2, | ||
RandomValueGenerator randomValueGenerator) { | ||
super(randomValueGenerator); | ||
this.sampler1 = requireNonNull(sampler1); | ||
this.sampler2 = requireNonNull(sampler2); | ||
this.description = | ||
"ConsistentComposedAndSampler{" | ||
+ "sampler1=" | ||
+ sampler1.getDescription() | ||
+ ",sampler2=" | ||
+ sampler2.getDescription() | ||
+ '}'; | ||
} | ||
|
||
@Override | ||
protected long getThreshold(long parentThreshold, boolean isRoot) { | ||
long threshold1 = sampler1.getThreshold(parentThreshold, isRoot); | ||
long threshold2 = sampler2.getThreshold(parentThreshold, isRoot); | ||
if (ConsistentSamplingUtil.isValidThreshold(threshold1) | ||
&& ConsistentSamplingUtil.isValidThreshold(threshold2)) { | ||
return Math.min(threshold1, threshold2); | ||
} else { | ||
return ConsistentSamplingUtil.getInvalidThreshold(); | ||
} | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
.../main/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentComposedOrSampler.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,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler.consistent56; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* A consistent sampler composed of two consistent samplers. | ||
* | ||
* <p>This sampler samples if any of the two samplers would sample. | ||
*/ | ||
@Immutable | ||
final class ConsistentComposedOrSampler extends ConsistentSampler { | ||
|
||
private final ConsistentSampler sampler1; | ||
private final ConsistentSampler sampler2; | ||
private final String description; | ||
|
||
ConsistentComposedOrSampler( | ||
ConsistentSampler sampler1, | ||
ConsistentSampler sampler2, | ||
RandomValueGenerator randomValueGenerator) { | ||
super(randomValueGenerator); | ||
this.sampler1 = requireNonNull(sampler1); | ||
this.sampler2 = requireNonNull(sampler2); | ||
this.description = | ||
"ConsistentComposedOrSampler{" | ||
+ "sampler1=" | ||
+ sampler1.getDescription() | ||
+ ",sampler2=" | ||
+ sampler2.getDescription() | ||
+ '}'; | ||
} | ||
|
||
@Override | ||
protected long getThreshold(long parentThreshold, boolean isRoot) { | ||
long threshold1 = sampler1.getThreshold(parentThreshold, isRoot); | ||
long threshold2 = sampler2.getThreshold(parentThreshold, isRoot); | ||
if (ConsistentSamplingUtil.isValidThreshold(threshold1)) { | ||
if (ConsistentSamplingUtil.isValidThreshold(threshold2)) { | ||
return Math.max(threshold1, threshold2); | ||
} | ||
return threshold1; | ||
} else { | ||
if (ConsistentSamplingUtil.isValidThreshold(threshold2)) { | ||
return threshold2; | ||
} | ||
return ConsistentSamplingUtil.getInvalidThreshold(); | ||
} | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...n/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentFixedThresholdSampler.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,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler.consistent56; | ||
|
||
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.calculateSamplingProbability; | ||
import static io.opentelemetry.contrib.sampler.consistent56.ConsistentSamplingUtil.checkThreshold; | ||
|
||
public class ConsistentFixedThresholdSampler extends ConsistentSampler { | ||
|
||
private final long threshold; | ||
private final String description; | ||
|
||
protected ConsistentFixedThresholdSampler( | ||
long threshold, RandomValueGenerator randomValueGenerator) { | ||
super(randomValueGenerator); | ||
checkThreshold(threshold); | ||
this.threshold = threshold; | ||
|
||
String thresholdString; | ||
if (threshold == ConsistentSamplingUtil.getMaxThreshold()) { | ||
thresholdString = "max"; | ||
} else { | ||
thresholdString = | ||
ConsistentSamplingUtil.appendLast56BitHexEncodedWithoutTrailingZeros( | ||
new StringBuilder(), threshold) | ||
.toString(); | ||
} | ||
|
||
this.description = | ||
"ConsistentFixedThresholdSampler{threshold=" | ||
+ thresholdString | ||
+ ", sampling probability=" | ||
+ calculateSamplingProbability(threshold) | ||
+ "}"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
@Override | ||
protected long getThreshold(long parentThreshold, boolean isRoot) { | ||
return threshold; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...main/java/io/opentelemetry/contrib/sampler/consistent56/ConsistentParentBasedSampler.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,51 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.sampler.consistent56; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* A consistent sampler that makes the same sampling decision as the parent. For root spans the | ||
* sampling decision is delegated to the root sampler. | ||
*/ | ||
@Immutable | ||
final class ConsistentParentBasedSampler extends ConsistentSampler { | ||
|
||
private final ConsistentSampler rootSampler; | ||
|
||
private final String description; | ||
|
||
/** | ||
* Constructs a new consistent parent based sampler using the given root sampler and the given | ||
* thread-safe random generator. | ||
* | ||
* @param rootSampler the root sampler | ||
* @param randomValueGenerator the function to use for generating the r-value | ||
*/ | ||
ConsistentParentBasedSampler( | ||
ConsistentSampler rootSampler, RandomValueGenerator randomValueGenerator) { | ||
super(randomValueGenerator); | ||
this.rootSampler = requireNonNull(rootSampler); | ||
this.description = | ||
"ConsistentParentBasedSampler{rootSampler=" + rootSampler.getDescription() + '}'; | ||
} | ||
|
||
@Override | ||
protected long getThreshold(long parentThreshold, boolean isRoot) { | ||
if (isRoot) { | ||
return rootSampler.getThreshold(ConsistentSamplingUtil.getInvalidThreshold(), isRoot); | ||
} else { | ||
return parentThreshold; | ||
} | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
} |
Oops, something went wrong.