From 455179c751c07f18205c18d4d2c23a589b6f58ca Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Fri, 22 Aug 2025 13:54:19 +0900 Subject: [PATCH 1/3] Add experimental composite samplers --- custom/build.gradle.kts | 5 +++ .../CompositeAlwaysOffSamplerProvider.java | 38 ++++++++++++++++ .../CompositeAlwaysOnSamplerProvider.java | 38 ++++++++++++++++ ...teParentBasedAlwaysOffSamplerProvider.java | 39 +++++++++++++++++ ...iteParentBasedAlwaysOnSamplerProvider.java | 39 +++++++++++++++++ ...BasedTraceIdRatioBasedSamplerProvider.java | 43 +++++++++++++++++++ ...ositeTraceIdRatioBasedSamplerProvider.java | 41 ++++++++++++++++++ gradle/libs.versions.toml | 1 + 8 files changed, 244 insertions(+) create mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOffSamplerProvider.java create mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOnSamplerProvider.java create mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOffSamplerProvider.java create mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOnSamplerProvider.java create mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedTraceIdRatioBasedSamplerProvider.java create mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeTraceIdRatioBasedSamplerProvider.java diff --git a/custom/build.gradle.kts b/custom/build.gradle.kts index 97a1b3d98..41c365b33 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/CompositeAlwaysOffSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOffSamplerProvider.java new file mode 100644 index 000000000..2d031dcc1 --- /dev/null +++ b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOffSamplerProvider.java @@ -0,0 +1,38 @@ +/* + * 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.contrib.sampler.consistent56.ConsistentSampler; +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 CompositeAlwaysOffSamplerProvider implements ConfigurableSamplerProvider { + @Override + public Sampler createSampler(ConfigProperties config) { + return ConsistentSampler.alwaysOff(); + } + + @Override + public String getName() { + return "experimental_composite_always_off"; + } +} diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOnSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOnSamplerProvider.java new file mode 100644 index 000000000..bbea9a20d --- /dev/null +++ b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOnSamplerProvider.java @@ -0,0 +1,38 @@ +/* + * 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.contrib.sampler.consistent56.ConsistentSampler; +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 CompositeAlwaysOnSamplerProvider implements ConfigurableSamplerProvider { + @Override + public Sampler createSampler(ConfigProperties config) { + return ConsistentSampler.alwaysOn(); + } + + @Override + public String getName() { + return "experimental_composite_always_on"; + } +} diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOffSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOffSamplerProvider.java new file mode 100644 index 000000000..57597548a --- /dev/null +++ b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOffSamplerProvider.java @@ -0,0 +1,39 @@ +/* + * 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.contrib.sampler.consistent56.ConsistentSampler; +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 CompositeParentBasedAlwaysOffSamplerProvider implements ConfigurableSamplerProvider { + + @Override + public Sampler createSampler(ConfigProperties config) { + return ConsistentSampler.parentBased(ConsistentSampler.alwaysOff()); + } + + @Override + public String getName() { + return "experimental_composite_parentbased_always_off"; + } +} diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOnSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOnSamplerProvider.java new file mode 100644 index 000000000..6e40262cd --- /dev/null +++ b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOnSamplerProvider.java @@ -0,0 +1,39 @@ +/* + * 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.contrib.sampler.consistent56.ConsistentSampler; +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 CompositeParentBasedAlwaysOnSamplerProvider implements ConfigurableSamplerProvider { + + @Override + public Sampler createSampler(ConfigProperties config) { + return ConsistentSampler.parentBased(ConsistentSampler.alwaysOn()); + } + + @Override + public String getName() { + return "experimental_composite_parentbased_always_on"; + } +} 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 000000000..e912052ce --- /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.contrib.sampler.consistent56.ConsistentSampler; +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 { + private static final double DEFAULT_TRACEIDRATIO_SAMPLE_RATIO = 1.0d; + + @Override + public Sampler createSampler(ConfigProperties config) { + return ConsistentSampler.parentBased( + ConsistentSampler.probabilityBased( + config.getDouble("otel.traces.sampler.arg", DEFAULT_TRACEIDRATIO_SAMPLE_RATIO))); + } + + @Override + public String getName() { + return "experimental_composite_parentbased_traceidratio"; + } +} diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeTraceIdRatioBasedSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeTraceIdRatioBasedSamplerProvider.java new file mode 100644 index 000000000..47838dff4 --- /dev/null +++ b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeTraceIdRatioBasedSamplerProvider.java @@ -0,0 +1,41 @@ +/* + * 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.contrib.sampler.consistent56.ConsistentSampler; +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 CompositeTraceIdRatioBasedSamplerProvider implements ConfigurableSamplerProvider { + private static final double DEFAULT_TRACEIDRATIO_SAMPLE_RATIO = 1.0d; + + @Override + public Sampler createSampler(ConfigProperties config) { + return ConsistentSampler.probabilityBased( + config.getDouble("otel.traces.sampler.arg", DEFAULT_TRACEIDRATIO_SAMPLE_RATIO)); + } + + @Override + public String getName() { + return "experimental_composite_traceidratio"; + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5130e5cf2..b437820b3 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" } From f791922dd309d8dbf97cee3590abf912017cd5a8 Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Fri, 12 Sep 2025 16:23:58 +0900 Subject: [PATCH 2/3] Only keep parent based traceid --- .../CompositeAlwaysOffSamplerProvider.java | 38 ----------------- .../CompositeAlwaysOnSamplerProvider.java | 38 ----------------- ...teParentBasedAlwaysOffSamplerProvider.java | 39 ------------------ ...iteParentBasedAlwaysOnSamplerProvider.java | 39 ------------------ ...ositeTraceIdRatioBasedSamplerProvider.java | 41 ------------------- 5 files changed, 195 deletions(-) delete mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOffSamplerProvider.java delete mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOnSamplerProvider.java delete mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOffSamplerProvider.java delete mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOnSamplerProvider.java delete mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/CompositeTraceIdRatioBasedSamplerProvider.java diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOffSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOffSamplerProvider.java deleted file mode 100644 index 2d031dcc1..000000000 --- a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOffSamplerProvider.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.contrib.sampler.consistent56.ConsistentSampler; -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 CompositeAlwaysOffSamplerProvider implements ConfigurableSamplerProvider { - @Override - public Sampler createSampler(ConfigProperties config) { - return ConsistentSampler.alwaysOff(); - } - - @Override - public String getName() { - return "experimental_composite_always_off"; - } -} diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOnSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOnSamplerProvider.java deleted file mode 100644 index bbea9a20d..000000000 --- a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeAlwaysOnSamplerProvider.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.contrib.sampler.consistent56.ConsistentSampler; -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 CompositeAlwaysOnSamplerProvider implements ConfigurableSamplerProvider { - @Override - public Sampler createSampler(ConfigProperties config) { - return ConsistentSampler.alwaysOn(); - } - - @Override - public String getName() { - return "experimental_composite_always_on"; - } -} diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOffSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOffSamplerProvider.java deleted file mode 100644 index 57597548a..000000000 --- a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOffSamplerProvider.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.contrib.sampler.consistent56.ConsistentSampler; -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 CompositeParentBasedAlwaysOffSamplerProvider implements ConfigurableSamplerProvider { - - @Override - public Sampler createSampler(ConfigProperties config) { - return ConsistentSampler.parentBased(ConsistentSampler.alwaysOff()); - } - - @Override - public String getName() { - return "experimental_composite_parentbased_always_off"; - } -} diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOnSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOnSamplerProvider.java deleted file mode 100644 index 6e40262cd..000000000 --- a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedAlwaysOnSamplerProvider.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.contrib.sampler.consistent56.ConsistentSampler; -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 CompositeParentBasedAlwaysOnSamplerProvider implements ConfigurableSamplerProvider { - - @Override - public Sampler createSampler(ConfigProperties config) { - return ConsistentSampler.parentBased(ConsistentSampler.alwaysOn()); - } - - @Override - public String getName() { - return "experimental_composite_parentbased_always_on"; - } -} diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeTraceIdRatioBasedSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeTraceIdRatioBasedSamplerProvider.java deleted file mode 100644 index 47838dff4..000000000 --- a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeTraceIdRatioBasedSamplerProvider.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.contrib.sampler.consistent56.ConsistentSampler; -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 CompositeTraceIdRatioBasedSamplerProvider implements ConfigurableSamplerProvider { - private static final double DEFAULT_TRACEIDRATIO_SAMPLE_RATIO = 1.0d; - - @Override - public Sampler createSampler(ConfigProperties config) { - return ConsistentSampler.probabilityBased( - config.getDouble("otel.traces.sampler.arg", DEFAULT_TRACEIDRATIO_SAMPLE_RATIO)); - } - - @Override - public String getName() { - return "experimental_composite_traceidratio"; - } -} From dafb089e0ad8b75f7ab68bcc6f9bc06992b912bb Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Fri, 19 Sep 2025 13:52:24 +0900 Subject: [PATCH 3/3] Make dynamic --- ...BasedTraceIdRatioBasedSamplerProvider.java | 10 ++-- ...teParentBasedTraceIdRatioBasedSampler.java | 60 +++++++++++++++++++ 2 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 custom/src/main/java/co/elastic/otel/compositesampling/DynamicCompositeParentBasedTraceIdRatioBasedSampler.java diff --git a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedTraceIdRatioBasedSamplerProvider.java b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedTraceIdRatioBasedSamplerProvider.java index e912052ce..21704f5cc 100644 --- a/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedTraceIdRatioBasedSamplerProvider.java +++ b/custom/src/main/java/co/elastic/otel/compositesampling/CompositeParentBasedTraceIdRatioBasedSamplerProvider.java @@ -19,7 +19,6 @@ package co.elastic.otel.compositesampling; import com.google.auto.service.AutoService; -import io.opentelemetry.contrib.sampler.consistent56.ConsistentSampler; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider; import io.opentelemetry.sdk.trace.samplers.Sampler; @@ -27,13 +26,14 @@ @AutoService(ConfigurableSamplerProvider.class) public class CompositeParentBasedTraceIdRatioBasedSamplerProvider implements ConfigurableSamplerProvider { - private static final double DEFAULT_TRACEIDRATIO_SAMPLE_RATIO = 1.0d; @Override public Sampler createSampler(ConfigProperties config) { - return ConsistentSampler.parentBased( - ConsistentSampler.probabilityBased( - config.getDouble("otel.traces.sampler.arg", DEFAULT_TRACEIDRATIO_SAMPLE_RATIO))); + DynamicCompositeParentBasedTraceIdRatioBasedSampler.setRatio( + config.getDouble( + "otel.traces.sampler.arg", + DynamicCompositeParentBasedTraceIdRatioBasedSampler.DEFAULT_TRACEIDRATIO_SAMPLE_RATIO)); + return DynamicCompositeParentBasedTraceIdRatioBasedSampler.INSTANCE; } @Override 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 000000000..3304202b4 --- /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)); + } +}