-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Added custom sampler support based on action in request #10136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sachinpkale
merged 22 commits into
opensearch-project:main
from
devagarwal1803:devagarw-action-based-sampler
Feb 7, 2024
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b0b5f52
Added custom sampler support based on action in request
devagarwal1803 6c7fa2f
UT Fix
devagarwal1803 fa55e70
Added Transport action sampler, which will sample based on different …
devagarwal1803 42863a8
Added Transport action sampler, which will sample based on different …
devagarwal1803 ec0daab
Added missing java-doc
devagarwal1803 0d09fbb
Moving sampler class settings to OtelTelemetry setting
devagarwal1803 8c6aa65
Minor refactor
devagarwal1803 92ab035
Refactored to use chain of samplers
devagarwal1803 e9aed28
Addressed comments
devagarwal1803 4e130e6
Addressed comments to move action_probability to OtelTelemetrySettings
devagarwal1803 60429d8
Updated eror msg returned when Sampler class is not found
devagarwal1803 6cf7db3
Added UT for OTelSamplerFactory
devagarwal1803 4eb99bf
minor refactor
devagarwal1803 5f8060c
minor refactor
devagarwal1803 ff7c18c
spotless check
devagarwal1803 1fa76db
Updating OtelTelemetryPlugin.get() method
devagarwal1803 351e879
Addressed comments
devagarwal1803 80be159
minor refactor
devagarwal1803 81f4f97
addressed comments
devagarwal1803 afe700c
Updated transport action sampler
devagarwal1803 bbb454b
Empty-Commit
devagarwal1803 9c3380d
Empty-Commit
devagarwal1803 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...metry-otel/src/main/java/org/opensearch/telemetry/tracing/sampler/OTelSamplerFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.telemetry.tracing.sampler; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.opensearch.SpecialPermission; | ||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.telemetry.OTelTelemetrySettings; | ||
| import org.opensearch.telemetry.TelemetrySettings; | ||
|
|
||
| import java.lang.invoke.MethodHandles; | ||
| import java.lang.invoke.MethodType; | ||
| import java.security.AccessController; | ||
| import java.security.PrivilegedExceptionAction; | ||
| import java.util.List; | ||
| import java.util.ListIterator; | ||
|
|
||
| import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
|
|
||
| /** | ||
| * Factory class to create the instance of OTelSampler | ||
| */ | ||
| public class OTelSamplerFactory { | ||
|
|
||
| /** | ||
| * Logger instance for logging messages related to the OTelSamplerFactory. | ||
| */ | ||
| private static final Logger logger = LogManager.getLogger(OTelSamplerFactory.class); | ||
|
|
||
| /** | ||
| * Base constructor. | ||
| */ | ||
| private OTelSamplerFactory() { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Creates the {@link Sampler} instances based on the TRACER_SPAN_SAMPLER_CLASSES value. | ||
| * | ||
| * @param telemetrySettings TelemetrySettings. | ||
| * @param settings the settings | ||
| * @return list of samplers. | ||
| */ | ||
| public static Sampler create(TelemetrySettings telemetrySettings, Settings settings) { | ||
| List<Class<Sampler>> samplersNameList = OTelTelemetrySettings.OTEL_TRACER_SPAN_SAMPLER_CLASS_SETTINGS.get(settings); | ||
| ListIterator<Class<Sampler>> li = samplersNameList.listIterator(samplersNameList.size()); | ||
|
|
||
| Sampler fallbackSampler = null; | ||
|
|
||
| // Iterating samplers list in reverse order to create chain of sampler | ||
| while (li.hasPrevious()) { | ||
devagarwal1803 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Class<Sampler> samplerName = li.previous(); | ||
| fallbackSampler = instantiateSampler(samplerName, telemetrySettings, settings, fallbackSampler); | ||
| } | ||
|
|
||
| return fallbackSampler; | ||
| } | ||
|
|
||
| private static Sampler instantiateSampler( | ||
| Class<Sampler> samplerClassName, | ||
| TelemetrySettings telemetrySettings, | ||
| Settings settings, | ||
| Sampler fallbackSampler | ||
| ) { | ||
| try { | ||
| // Check we ourselves are not being called by unprivileged code. | ||
| SpecialPermission.check(); | ||
|
|
||
| return AccessController.doPrivileged((PrivilegedExceptionAction<Sampler>) () -> { | ||
| try { | ||
| // Define the method type which receives TelemetrySettings & Sampler as arguments | ||
| MethodType methodType = MethodType.methodType(Sampler.class, TelemetrySettings.class, Settings.class, Sampler.class); | ||
|
|
||
| return (Sampler) MethodHandles.publicLookup() | ||
| .findStatic(samplerClassName, "create", methodType) | ||
| .invokeExact(telemetrySettings, settings, fallbackSampler); | ||
| } catch (Throwable e) { | ||
| if (e.getCause() instanceof NoSuchMethodException) { | ||
| throw new IllegalStateException("No create method exist in [" + samplerClassName + "]", e.getCause()); | ||
| } else { | ||
| throw new IllegalStateException("Sampler instantiation failed for class [" + samplerClassName + "]", e.getCause()); | ||
| } | ||
| } | ||
| }); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Sampler instantiation failed for class [" + samplerClassName + "]", e.getCause()); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...in/java/org/opensearch/telemetry/tracing/sampler/ProbabilisticTransportActionSampler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.telemetry.tracing.sampler; | ||
|
|
||
| import org.opensearch.common.settings.Settings; | ||
| import org.opensearch.telemetry.OTelTelemetrySettings; | ||
| import org.opensearch.telemetry.TelemetrySettings; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import io.opentelemetry.api.common.AttributeKey; | ||
| 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.SamplingDecision; | ||
| import io.opentelemetry.sdk.trace.samplers.SamplingResult; | ||
|
|
||
| import static org.opensearch.telemetry.tracing.AttributeNames.TRANSPORT_ACTION; | ||
|
|
||
| /** | ||
| * ProbabilisticTransportActionSampler sampler samples request with action based on defined probability | ||
| */ | ||
| public class ProbabilisticTransportActionSampler implements Sampler { | ||
|
|
||
| private final Sampler fallbackSampler; | ||
| private Sampler actionSampler; | ||
| private final TelemetrySettings telemetrySettings; | ||
| private final Settings settings; | ||
| private double actionSamplingRatio; | ||
devagarwal1803 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Creates ProbabilisticTransportActionSampler sampler | ||
| * @param telemetrySettings TelemetrySettings | ||
| */ | ||
| private ProbabilisticTransportActionSampler(TelemetrySettings telemetrySettings, Settings settings, Sampler fallbackSampler) { | ||
| this.telemetrySettings = Objects.requireNonNull(telemetrySettings); | ||
| this.settings = Objects.requireNonNull(settings); | ||
| this.actionSamplingRatio = OTelTelemetrySettings.TRACER_SAMPLER_ACTION_PROBABILITY.get(settings); | ||
| this.actionSampler = Sampler.traceIdRatioBased(actionSamplingRatio); | ||
| this.fallbackSampler = fallbackSampler; | ||
| } | ||
|
|
||
| /** | ||
| * Create probabilistic transport action sampler. | ||
| * | ||
| * @param telemetrySettings the telemetry settings | ||
| * @param settings the settings | ||
| * @param fallbackSampler the fallback sampler | ||
| * @return the probabilistic transport action sampler | ||
| */ | ||
| public static Sampler create(TelemetrySettings telemetrySettings, Settings settings, Sampler fallbackSampler) { | ||
| return new ProbabilisticTransportActionSampler(telemetrySettings, settings, fallbackSampler); | ||
| } | ||
|
|
||
| @Override | ||
| public SamplingResult shouldSample( | ||
| Context parentContext, | ||
| String traceId, | ||
| String name, | ||
| SpanKind spanKind, | ||
| Attributes attributes, | ||
| List<LinkData> parentLinks | ||
| ) { | ||
| final String action = attributes.get(AttributeKey.stringKey(TRANSPORT_ACTION)); | ||
| if (action != null) { | ||
| final SamplingResult result = actionSampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); | ||
| if (result.getDecision() != SamplingDecision.DROP && fallbackSampler != null) { | ||
| return fallbackSampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); | ||
| } | ||
| return result; | ||
| } | ||
| if (fallbackSampler != null) return fallbackSampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); | ||
|
|
||
| return SamplingResult.drop(); | ||
| } | ||
|
|
||
| double getSamplingRatio() { | ||
| return actionSamplingRatio; | ||
| } | ||
|
|
||
| @Override | ||
| public String getDescription() { | ||
| return "Transport Action Sampler"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return getDescription(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.