Skip to content

Commit 0e68177

Browse files
authored
Add experimental composite samplers (#777)
* Add experimental composite samplers * Only keep parent based traceid * Make dynamic
1 parent 1940841 commit 0e68177

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

custom/build.gradle.kts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ dependencies {
2828
compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling")
2929
compileOnly(libs.bundles.semconv)
3030

31+
implementation(libs.contribConsistentSampling) {
32+
// exclude transitive dependency as it's provided through agent packaging
33+
exclude(group = "io.opentelemetry", module = "opentelemetry-sdk-trace")
34+
exclude(group = "io.opentelemetry", module = "opentelemetry-sdk-extension-autoconfigure-spi")
35+
}
3136
implementation(libs.contribSpanStacktrace) {
3237
// exclude transitive dependency as it's provided through agent packaging
3338
exclude(group = "io.opentelemetry", module = "opentelemetry-sdk")
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package co.elastic.otel.compositesampling;
20+
21+
import com.google.auto.service.AutoService;
22+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
23+
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider;
24+
import io.opentelemetry.sdk.trace.samplers.Sampler;
25+
26+
@AutoService(ConfigurableSamplerProvider.class)
27+
public class CompositeParentBasedTraceIdRatioBasedSamplerProvider
28+
implements ConfigurableSamplerProvider {
29+
30+
@Override
31+
public Sampler createSampler(ConfigProperties config) {
32+
DynamicCompositeParentBasedTraceIdRatioBasedSampler.setRatio(
33+
config.getDouble(
34+
"otel.traces.sampler.arg",
35+
DynamicCompositeParentBasedTraceIdRatioBasedSampler.DEFAULT_TRACEIDRATIO_SAMPLE_RATIO));
36+
return DynamicCompositeParentBasedTraceIdRatioBasedSampler.INSTANCE;
37+
}
38+
39+
@Override
40+
public String getName() {
41+
return "experimental_composite_parentbased_traceidratio";
42+
}
43+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package co.elastic.otel.compositesampling;
20+
21+
import io.opentelemetry.api.common.Attributes;
22+
import io.opentelemetry.api.trace.SpanKind;
23+
import io.opentelemetry.context.Context;
24+
import io.opentelemetry.contrib.sampler.consistent56.ConsistentSampler;
25+
import io.opentelemetry.sdk.trace.data.LinkData;
26+
import io.opentelemetry.sdk.trace.samplers.Sampler;
27+
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
28+
import java.util.List;
29+
30+
public enum DynamicCompositeParentBasedTraceIdRatioBasedSampler implements Sampler {
31+
INSTANCE;
32+
33+
static final double DEFAULT_TRACEIDRATIO_SAMPLE_RATIO = 1.0d;
34+
35+
private static Sampler delegate = newSampler(DEFAULT_TRACEIDRATIO_SAMPLE_RATIO);
36+
37+
public static void setRatio(double ratio) {
38+
delegate = newSampler(ratio);
39+
}
40+
41+
@Override
42+
public SamplingResult shouldSample(
43+
Context parentContext,
44+
String traceId,
45+
String name,
46+
SpanKind spanKind,
47+
Attributes attributes,
48+
List<LinkData> parentLinks) {
49+
return delegate.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks);
50+
}
51+
52+
@Override
53+
public String getDescription() {
54+
return INSTANCE.getDescription();
55+
}
56+
57+
private static Sampler newSampler(double ratio) {
58+
return ConsistentSampler.parentBased(ConsistentSampler.probabilityBased(ratio));
59+
}
60+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ opentelemetryInstrumentationAlphaBom = { group = "io.opentelemetry.instrumentati
3131

3232
opentelemetryProto = { group = "io.opentelemetry.proto", name = "opentelemetry-proto", version.ref = "opentelemetryProto" }
3333

34+
contribConsistentSampling = { group = "io.opentelemetry.contrib", name = "opentelemetry-consistent-sampling", version.ref = "opentelemetryContribAlpha" }
3435
contribResources = { group = "io.opentelemetry.contrib", name = "opentelemetry-resource-providers", version.ref = "opentelemetryContribAlpha" }
3536
contribSpanStacktrace = { group = "io.opentelemetry.contrib", name = "opentelemetry-span-stacktrace", version.ref = "opentelemetryContribAlpha" }
3637
contribInferredSpans = { group = "io.opentelemetry.contrib", name = "opentelemetry-inferred-spans", version.ref = "opentelemetryContribAlpha" }

0 commit comments

Comments
 (0)