@@ -28,17 +28,37 @@ public abstract class ConsistentSampler implements Sampler {
2828 *
2929 * @return a sampler
3030 */
31- public static final ConsistentSampler alwaysOn () {
32- return ConsistentAlwaysOnSampler .getInstance ();
31+ public static ConsistentSampler alwaysOn () {
32+ return alwaysOn (RValueGenerators .getDefault ());
33+ }
34+
35+ /**
36+ * Returns a {@link ConsistentSampler} that samples all spans.
37+ *
38+ * @param rValueGenerator the function to use for generating the r-value
39+ * @return a sampler
40+ */
41+ public static ConsistentSampler alwaysOn (RValueGenerator rValueGenerator ) {
42+ return new ConsistentAlwaysOnSampler (rValueGenerator );
43+ }
44+
45+ /**
46+ * Returns a {@link ConsistentSampler} that does not sample any span.
47+ *
48+ * @return a sampler
49+ */
50+ public static ConsistentSampler alwaysOff () {
51+ return alwaysOff (RValueGenerators .getDefault ());
3352 }
3453
3554 /**
3655 * Returns a {@link ConsistentSampler} that does not sample any span.
3756 *
57+ * @param rValueGenerator the function to use for generating the r-value
3858 * @return a sampler
3959 */
40- public static final ConsistentSampler alwaysOff () {
41- return ConsistentAlwaysOffSampler . getInstance ( );
60+ public static ConsistentSampler alwaysOff (RValueGenerator rValueGenerator ) {
61+ return new ConsistentAlwaysOffSampler ( rValueGenerator );
4262 }
4363
4464 /**
@@ -47,20 +67,21 @@ public static final ConsistentSampler alwaysOff() {
4767 * @param samplingProbability the sampling probability
4868 * @return a sampler
4969 */
50- public static final ConsistentSampler probabilityBased (double samplingProbability ) {
51- return new ConsistentProbabilityBasedSampler (samplingProbability );
70+ public static ConsistentSampler probabilityBased (double samplingProbability ) {
71+ return probabilityBased (samplingProbability , RValueGenerators . getDefault () );
5272 }
5373
5474 /**
5575 * Returns a {@link ConsistentSampler} that samples each span with a fixed probability.
5676 *
5777 * @param samplingProbability the sampling probability
58- * @param randomGenerator a random generator
78+ * @param rValueGenerator the function to use for generating the r-value
5979 * @return a sampler
6080 */
61- static final ConsistentSampler probabilityBased (
62- double samplingProbability , RandomGenerator randomGenerator ) {
63- return new ConsistentProbabilityBasedSampler (samplingProbability , randomGenerator );
81+ public static ConsistentSampler probabilityBased (
82+ double samplingProbability , RValueGenerator rValueGenerator ) {
83+ return new ConsistentProbabilityBasedSampler (
84+ samplingProbability , rValueGenerator , RandomGenerator .getDefault ());
6485 }
6586
6687 /**
@@ -69,20 +90,20 @@ static final ConsistentSampler probabilityBased(
6990 *
7091 * @param rootSampler the root sampler
7192 */
72- public static final ConsistentSampler parentBased (ConsistentSampler rootSampler ) {
73- return new ConsistentParentBasedSampler (rootSampler );
93+ public static ConsistentSampler parentBased (ConsistentSampler rootSampler ) {
94+ return parentBased (rootSampler , RValueGenerators . getDefault () );
7495 }
7596
7697 /**
7798 * Returns a new {@link ConsistentSampler} that respects the sampling decision of the parent span
7899 * or falls-back to the given sampler if it is a root span.
79100 *
80101 * @param rootSampler the root sampler
81- * @param randomGenerator a random generator
102+ * @param rValueGenerator the function to use for generating the r-value
82103 */
83- static final ConsistentSampler parentBased (
84- ConsistentSampler rootSampler , RandomGenerator randomGenerator ) {
85- return new ConsistentParentBasedSampler (rootSampler , randomGenerator );
104+ public static ConsistentSampler parentBased (
105+ ConsistentSampler rootSampler , RValueGenerator rValueGenerator ) {
106+ return new ConsistentParentBasedSampler (rootSampler , rValueGenerator );
86107 }
87108
88109 /**
@@ -93,9 +114,10 @@ static final ConsistentSampler parentBased(
93114 * @param adaptationTimeSeconds the typical time to adapt to a new load (time constant used for
94115 * exponential smoothing)
95116 */
96- public static final ConsistentSampler rateLimited (
117+ public static ConsistentSampler rateLimited (
97118 double targetSpansPerSecondLimit , double adaptationTimeSeconds ) {
98- return new ConsistentRateLimitingSampler (targetSpansPerSecondLimit , adaptationTimeSeconds );
119+ return rateLimited (
120+ targetSpansPerSecondLimit , adaptationTimeSeconds , RValueGenerators .getDefault ());
99121 }
100122
101123 /**
@@ -105,16 +127,37 @@ public static final ConsistentSampler rateLimited(
105127 * @param targetSpansPerSecondLimit the desired spans per second limit
106128 * @param adaptationTimeSeconds the typical time to adapt to a new load (time constant used for
107129 * exponential smoothing)
108- * @param randomGenerator a random generator
130+ * @param rValueGenerator the function to use for generating the r-value
131+ */
132+ public static ConsistentSampler rateLimited (
133+ double targetSpansPerSecondLimit ,
134+ double adaptationTimeSeconds ,
135+ RValueGenerator rValueGenerator ) {
136+ return rateLimited (
137+ targetSpansPerSecondLimit , adaptationTimeSeconds , rValueGenerator , System ::nanoTime );
138+ }
139+
140+ /**
141+ * Returns a new {@link ConsistentSampler} that attempts to adjust the sampling probability
142+ * dynamically to meet the target span rate.
143+ *
144+ * @param targetSpansPerSecondLimit the desired spans per second limit
145+ * @param adaptationTimeSeconds the typical time to adapt to a new load (time constant used for
146+ * exponential smoothing)
147+ * @param rValueGenerator the function to use for generating the r-value
109148 * @param nanoTimeSupplier a supplier for the current nano time
110149 */
111- static final ConsistentSampler rateLimited (
150+ static ConsistentSampler rateLimited (
112151 double targetSpansPerSecondLimit ,
113152 double adaptationTimeSeconds ,
114- RandomGenerator randomGenerator ,
153+ RValueGenerator rValueGenerator ,
115154 LongSupplier nanoTimeSupplier ) {
116155 return new ConsistentRateLimitingSampler (
117- targetSpansPerSecondLimit , adaptationTimeSeconds , randomGenerator , nanoTimeSupplier );
156+ targetSpansPerSecondLimit ,
157+ adaptationTimeSeconds ,
158+ rValueGenerator ,
159+ RandomGenerator .getDefault (),
160+ nanoTimeSupplier );
118161 }
119162
120163 /**
@@ -136,7 +179,8 @@ public ConsistentSampler and(ConsistentSampler otherConsistentSampler) {
136179 if (otherConsistentSampler == this ) {
137180 return this ;
138181 }
139- return new ConsistentComposedAndSampler (this , otherConsistentSampler );
182+ return new ConsistentComposedAndSampler (
183+ this , otherConsistentSampler , RValueGenerators .getDefault ());
140184 }
141185
142186 /**
@@ -158,20 +202,17 @@ public ConsistentSampler or(ConsistentSampler otherConsistentSampler) {
158202 if (otherConsistentSampler == this ) {
159203 return this ;
160204 }
161- return new ConsistentComposedOrSampler (this , otherConsistentSampler );
205+ return new ConsistentComposedOrSampler (
206+ this , otherConsistentSampler , RValueGenerators .getDefault ());
162207 }
163208
164- protected final RandomGenerator randomGenerator ;
165-
166- protected ConsistentSampler (RandomGenerator randomGenerator ) {
167- this .randomGenerator = requireNonNull (randomGenerator );
168- }
209+ private final RValueGenerator rValueGenerator ;
169210
170- protected ConsistentSampler () {
171- this ( RandomGenerator . getDefault () );
211+ protected ConsistentSampler (RValueGenerator rValueGenerator ) {
212+ this . rValueGenerator = requireNonNull ( rValueGenerator );
172213 }
173214
174- private static final boolean isInvariantViolated (
215+ private static boolean isInvariantViolated (
175216 OtelTraceState otelTraceState , boolean isParentSampled ) {
176217 if (otelTraceState .hasValidR () && otelTraceState .hasValidP ()) {
177218 // if valid p- and r-values are given, they must be consistent with the isParentSampled flag
@@ -212,8 +253,7 @@ public final SamplingResult shouldSample(
212253
213254 // generate new r-value if not available
214255 if (!otelTraceState .hasValidR ()) {
215- otelTraceState .setR (
216- Math .min (randomGenerator .numberOfLeadingZerosOfRandomLong (), OtelTraceState .getMaxR ()));
256+ otelTraceState .setR (Math .min (rValueGenerator .generate (traceId ), OtelTraceState .getMaxR ()));
217257 }
218258
219259 // determine and set new p-value that is used for the sampling decision
0 commit comments