forked from open-telemetry/opentelemetry-java-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelegatingSampler.java
More file actions
56 lines (46 loc) · 1.47 KB
/
DelegatingSampler.java
File metadata and controls
56 lines (46 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.contrib.dynamic.sampler;
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 io.opentelemetry.sdk.trace.samplers.Sampler;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
public class DelegatingSampler implements Sampler {
private final AtomicReference<Sampler> delegate;
public DelegatingSampler(Sampler initialDelegate) {
this.delegate =
new AtomicReference<>(initialDelegate != null ? initialDelegate : Sampler.alwaysOn());
}
public DelegatingSampler() {
this(Sampler.alwaysOn());
}
public void setDelegate(Sampler sampler) {
delegate.set(sampler != null ? sampler : Sampler.alwaysOn());
}
@Override
public SamplingResult shouldSample(
Context ctx,
String traceId,
String name,
SpanKind kind,
Attributes attrs,
List<LinkData> links) {
return Objects.requireNonNull(delegate.get())
.shouldSample(ctx, traceId, name, kind, attrs, links);
}
@Override
public String getDescription() {
return "delegating/" + Objects.requireNonNull(delegate.get()).getDescription();
}
@Override
public String toString() {
return getDescription();
}
}