|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.telemetry.tracing.sampler; |
| 10 | + |
| 11 | +import org.opensearch.common.settings.Settings; |
| 12 | +import org.opensearch.telemetry.OTelTelemetrySettings; |
| 13 | +import org.opensearch.telemetry.TelemetrySettings; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +import io.opentelemetry.api.common.AttributeKey; |
| 19 | +import io.opentelemetry.api.common.Attributes; |
| 20 | +import io.opentelemetry.api.trace.SpanKind; |
| 21 | +import io.opentelemetry.context.Context; |
| 22 | +import io.opentelemetry.sdk.trace.data.LinkData; |
| 23 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 24 | +import io.opentelemetry.sdk.trace.samplers.SamplingDecision; |
| 25 | +import io.opentelemetry.sdk.trace.samplers.SamplingResult; |
| 26 | + |
| 27 | +import static org.opensearch.telemetry.tracing.AttributeNames.TRANSPORT_ACTION; |
| 28 | + |
| 29 | +/** |
| 30 | + * ProbabilisticTransportActionSampler sampler samples request with action based on defined probability |
| 31 | + */ |
| 32 | +public class ProbabilisticTransportActionSampler implements Sampler { |
| 33 | + |
| 34 | + private final Sampler fallbackSampler; |
| 35 | + private Sampler actionSampler; |
| 36 | + private final TelemetrySettings telemetrySettings; |
| 37 | + private final Settings settings; |
| 38 | + private double actionSamplingRatio; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates ProbabilisticTransportActionSampler sampler |
| 42 | + * @param telemetrySettings TelemetrySettings |
| 43 | + */ |
| 44 | + private ProbabilisticTransportActionSampler(TelemetrySettings telemetrySettings, Settings settings, Sampler fallbackSampler) { |
| 45 | + this.telemetrySettings = Objects.requireNonNull(telemetrySettings); |
| 46 | + this.settings = Objects.requireNonNull(settings); |
| 47 | + this.actionSamplingRatio = OTelTelemetrySettings.TRACER_SAMPLER_ACTION_PROBABILITY.get(settings); |
| 48 | + this.actionSampler = Sampler.traceIdRatioBased(actionSamplingRatio); |
| 49 | + this.fallbackSampler = fallbackSampler; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Create probabilistic transport action sampler. |
| 54 | + * |
| 55 | + * @param telemetrySettings the telemetry settings |
| 56 | + * @param settings the settings |
| 57 | + * @param fallbackSampler the fallback sampler |
| 58 | + * @return the probabilistic transport action sampler |
| 59 | + */ |
| 60 | + public static Sampler create(TelemetrySettings telemetrySettings, Settings settings, Sampler fallbackSampler) { |
| 61 | + return new ProbabilisticTransportActionSampler(telemetrySettings, settings, fallbackSampler); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public SamplingResult shouldSample( |
| 66 | + Context parentContext, |
| 67 | + String traceId, |
| 68 | + String name, |
| 69 | + SpanKind spanKind, |
| 70 | + Attributes attributes, |
| 71 | + List<LinkData> parentLinks |
| 72 | + ) { |
| 73 | + final String action = attributes.get(AttributeKey.stringKey(TRANSPORT_ACTION)); |
| 74 | + if (action != null) { |
| 75 | + final SamplingResult result = actionSampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); |
| 76 | + if (result.getDecision() != SamplingDecision.DROP && fallbackSampler != null) { |
| 77 | + return fallbackSampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); |
| 78 | + } |
| 79 | + return result; |
| 80 | + } |
| 81 | + if (fallbackSampler != null) return fallbackSampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); |
| 82 | + |
| 83 | + return SamplingResult.drop(); |
| 84 | + } |
| 85 | + |
| 86 | + double getSamplingRatio() { |
| 87 | + return actionSamplingRatio; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public String getDescription() { |
| 92 | + return "Transport Action Sampler"; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String toString() { |
| 97 | + return getDescription(); |
| 98 | + } |
| 99 | +} |
0 commit comments