Skip to content

Commit ea2ac20

Browse files
authored
RandomSampler provides a static factory, preferred over the constructor (#561)
RandomSampler provides a static factory, preferred over the constructor to advantage of deterministic values (zero and one).
1 parent fcfbe5c commit ea2ac20

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type: improvement
2+
improvement:
3+
description: RandomSampler provides a static factory, preferred over the constructor
4+
to advantage of deterministic values (zero and one).
5+
links:
6+
- https://github.com/palantir/tracing-java/pull/561

tracing-benchmarks/src/jmh/java/com/palantir/tracing/TracingBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class TracingBenchmark {
5454
public enum BenchmarkObservability {
5555
SAMPLE(AlwaysSampler.INSTANCE),
5656
DO_NOT_SAMPLE(() -> false),
57-
UNDECIDED(new RandomSampler(0.01f));
57+
UNDECIDED(RandomSampler.create(0.01f));
5858

5959
private final TraceSampler traceSampler;
6060

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.palantir.tracing;
18+
19+
enum NeverSampler implements TraceSampler {
20+
INSTANCE;
21+
22+
@Override
23+
public boolean sample() {
24+
return false;
25+
}
26+
}

tracing/src/main/java/com/palantir/tracing/RandomSampler.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,29 @@ public final class RandomSampler implements TraceSampler {
2929

3030
private final float rate;
3131

32+
/**
33+
* Prefer the factory method {@link RandomSampler#create(float)}.
34+
*
35+
* @deprecated prefer the factory method
36+
*/
37+
@Deprecated
3238
public RandomSampler(float rate) {
3339
checkArgument(rate >= 0 && rate <= 1, "Rate should be between 0 and 1", SafeArg.of("rate", rate));
3440
this.rate = rate;
3541
}
3642

43+
public static TraceSampler create(float rate) {
44+
checkArgument(rate >= 0 && rate <= 1, "Rate should be between 0 and 1", SafeArg.of("rate", rate));
45+
if (rate == 0) {
46+
return NeverSampler.INSTANCE;
47+
}
48+
49+
if (rate == 1) {
50+
return AlwaysSampler.INSTANCE;
51+
}
52+
return new RandomSampler(rate);
53+
}
54+
3755
@Override
3856
public boolean sample() {
3957
return ThreadLocalRandom.current().nextFloat() < rate;

tracing/src/main/java/com/palantir/tracing/Tracer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private Tracer() {}
6666
private static volatile Consumer<Span> compositeObserver = span -> {};
6767

6868
// Thread-safe since stateless
69-
private static volatile TraceSampler sampler = new RandomSampler(0.01f);
69+
private static volatile TraceSampler sampler = RandomSampler.create(0.01f);
7070

7171
/** Creates a new trace, but does not set it as the current trace. */
7272
private static Trace createTrace(Observability observability, String traceId) {

0 commit comments

Comments
 (0)