diff --git a/custom/build.gradle.kts b/custom/build.gradle.kts index 97a1b3d9..41c365b3 100644 --- a/custom/build.gradle.kts +++ b/custom/build.gradle.kts @@ -24,6 +24,11 @@ dependencies { compileOnly("io.opentelemetry.javaagent:opentelemetry-javaagent-tooling") compileOnly(libs.bundles.semconv) + implementation(libs.contribConsistentSampling) { + // exclude transitive dependency as it's provided through agent packaging + exclude(group = "io.opentelemetry", module = "opentelemetry-sdk-trace") + exclude(group = "io.opentelemetry", module = "opentelemetry-sdk-extension-autoconfigure-spi") + } implementation(libs.contribSpanStacktrace) { // exclude transitive dependency as it's provided through agent packaging exclude(group = "io.opentelemetry", module = "opentelemetry-sdk") diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedTraceIdRatioBasedSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedTraceIdRatioBasedSamplerProvider.java new file mode 100644 index 00000000..21704f5c --- /dev/null +++ b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedTraceIdRatioBasedSamplerProvider.java @@ -0,0 +1,43 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.otel.compositesampling; + +import com.google.auto.service.AutoService; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider; +import io.opentelemetry.sdk.trace.samplers.Sampler; + +@AutoService(ConfigurableSamplerProvider.class) +public class CompositeParentBasedTraceIdRatioBasedSamplerProvider + implements ConfigurableSamplerProvider { + + @Override + public Sampler createSampler(ConfigProperties config) { + DynamicCompositeParentBasedTraceIdRatioBasedSampler.setRatio( + config.getDouble( + "otel.traces.sampler.arg", + DynamicCompositeParentBasedTraceIdRatioBasedSampler.DEFAULT_TRACEIDRATIO_SAMPLE_RATIO)); + return DynamicCompositeParentBasedTraceIdRatioBasedSampler.INSTANCE; + } + + @Override + public String getName() { + return "experimental_composite_parentbased_traceidratio"; + } +} diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/DynamicCompositeParentBasedTraceIdRatioBasedSampler.java b/custom/src/main/java/co/elastic/otel/compositesampling/DynamicCompositeParentBasedTraceIdRatioBasedSampler.java new file mode 100644 index 00000000..3304202b --- /dev/null +++ b/custom/src/main/java/co/elastic/otel/compositesampling/DynamicCompositeParentBasedTraceIdRatioBasedSampler.java @@ -0,0 +1,60 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package co.elastic.otel.compositesampling; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.context.Context; +import io.opentelemetry.contrib.sampler.consistent56.ConsistentSampler; +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; + +public enum DynamicCompositeParentBasedTraceIdRatioBasedSampler implements Sampler { + INSTANCE; + + static final double DEFAULT_TRACEIDRATIO_SAMPLE_RATIO = 1.0d; + + private static Sampler delegate = newSampler(DEFAULT_TRACEIDRATIO_SAMPLE_RATIO); + + public static void setRatio(double ratio) { + delegate = newSampler(ratio); + } + + @Override + public SamplingResult shouldSample( + Context parentContext, + String traceId, + String name, + SpanKind spanKind, + Attributes attributes, + List parentLinks) { + return delegate.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); + } + + @Override + public String getDescription() { + return INSTANCE.getDescription(); + } + + private static Sampler newSampler(double ratio) { + return ConsistentSampler.parentBased(ConsistentSampler.probabilityBased(ratio)); + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3143e887..b1ff75a6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -31,6 +31,7 @@ opentelemetryInstrumentationAlphaBom = { group = "io.opentelemetry.instrumentati opentelemetryProto = { group = "io.opentelemetry.proto", name = "opentelemetry-proto", version.ref = "opentelemetryProto" } +contribConsistentSampling = { group = "io.opentelemetry.contrib", name = "opentelemetry-consistent-sampling", version.ref = "opentelemetryContribAlpha" } contribResources = { group = "io.opentelemetry.contrib", name = "opentelemetry-resource-providers", version.ref = "opentelemetryContribAlpha" } contribSpanStacktrace = { group = "io.opentelemetry.contrib", name = "opentelemetry-span-stacktrace", version.ref = "opentelemetryContribAlpha" } contribInferredSpans = { group = "io.opentelemetry.contrib", name = "opentelemetry-inferred-spans", version.ref = "opentelemetryContribAlpha" }