-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added sampler based on Blanket Probabilistic Sampling rate and Overri…
…de for on demand (#9522) Signed-off-by: Dev Agarwal <[email protected]>
- Loading branch information
1 parent
f9a661c
commit 082d425
Showing
13 changed files
with
367 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
82 changes: 82 additions & 0 deletions
82
...try-otel/src/main/java/org/opensearch/telemetry/tracing/sampler/ProbabilisticSampler.java
This file contains 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,82 @@ | ||
/* | ||
* 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.telemetry.TelemetrySettings; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
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.SamplingResult; | ||
|
||
/** | ||
* ProbabilisticSampler implements a head-based sampling strategy based on provided settings. | ||
*/ | ||
public class ProbabilisticSampler implements Sampler { | ||
private Sampler defaultSampler; | ||
private final TelemetrySettings telemetrySettings; | ||
private double samplingRatio; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param telemetrySettings Telemetry settings. | ||
*/ | ||
public ProbabilisticSampler(TelemetrySettings telemetrySettings) { | ||
this.telemetrySettings = Objects.requireNonNull(telemetrySettings); | ||
this.samplingRatio = telemetrySettings.getTracerHeadSamplerSamplingRatio(); | ||
this.defaultSampler = Sampler.traceIdRatioBased(samplingRatio); | ||
} | ||
|
||
Sampler getSampler() { | ||
double newSamplingRatio = telemetrySettings.getTracerHeadSamplerSamplingRatio(); | ||
if (isSamplingRatioChanged(newSamplingRatio)) { | ||
synchronized (this) { | ||
this.samplingRatio = newSamplingRatio; | ||
defaultSampler = Sampler.traceIdRatioBased(samplingRatio); | ||
} | ||
} | ||
return defaultSampler; | ||
} | ||
|
||
private boolean isSamplingRatioChanged(double newSamplingRatio) { | ||
return Double.compare(this.samplingRatio, newSamplingRatio) != 0; | ||
} | ||
|
||
double getSamplingRatio() { | ||
return samplingRatio; | ||
} | ||
|
||
@Override | ||
public SamplingResult shouldSample( | ||
Context parentContext, | ||
String traceId, | ||
String name, | ||
SpanKind spanKind, | ||
Attributes attributes, | ||
List<LinkData> parentLinks | ||
) { | ||
return getSampler().shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Probabilistic Sampler"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getDescription(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...telemetry-otel/src/main/java/org/opensearch/telemetry/tracing/sampler/RequestSampler.java
This file contains 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,67 @@ | ||
/* | ||
* 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 java.util.List; | ||
|
||
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.SamplingResult; | ||
|
||
/** | ||
* HeadBased sampler | ||
*/ | ||
public class RequestSampler implements Sampler { | ||
private final Sampler defaultSampler; | ||
|
||
// TODO: Pick value of TRACE from PR #9415. | ||
private static final String TRACE = "trace"; | ||
|
||
/** | ||
* Creates Head based sampler | ||
* @param defaultSampler defaultSampler | ||
*/ | ||
public RequestSampler(Sampler defaultSampler) { | ||
this.defaultSampler = defaultSampler; | ||
} | ||
|
||
@Override | ||
public SamplingResult shouldSample( | ||
Context parentContext, | ||
String traceId, | ||
String name, | ||
SpanKind spanKind, | ||
Attributes attributes, | ||
List<LinkData> parentLinks | ||
) { | ||
|
||
final String trace = attributes.get(AttributeKey.stringKey(TRACE)); | ||
|
||
if (trace != null) { | ||
return (Boolean.parseBoolean(trace) == true) ? SamplingResult.recordAndSample() : SamplingResult.drop(); | ||
} else { | ||
return defaultSampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Request Sampler"; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getDescription(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...s/telemetry-otel/src/main/java/org/opensearch/telemetry/tracing/sampler/package-info.java
This file contains 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,12 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* This package contains classes needed for sampler. | ||
*/ | ||
package org.opensearch.telemetry.tracing.sampler; |
This file contains 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
64 changes: 64 additions & 0 deletions
64
...tel/src/test/java/org/opensearch/telemetry/tracing/sampler/ProbabilisticSamplerTests.java
This file contains 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,64 @@ | ||
/* | ||
* 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.ClusterSettings; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.telemetry.TelemetrySettings; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.Set; | ||
|
||
import io.opentelemetry.sdk.trace.samplers.Sampler; | ||
|
||
import static org.opensearch.telemetry.OTelTelemetrySettings.TRACER_EXPORTER_DELAY_SETTING; | ||
import static org.opensearch.telemetry.TelemetrySettings.TRACER_ENABLED_SETTING; | ||
import static org.opensearch.telemetry.TelemetrySettings.TRACER_SAMPLER_PROBABILITY; | ||
|
||
public class ProbabilisticSamplerTests extends OpenSearchTestCase { | ||
|
||
// When ProbabilisticSampler is created with OTelTelemetrySettings as null | ||
public void testProbabilisticSamplerWithNullSettings() { | ||
// Verify that the constructor throws IllegalArgumentException when given null settings | ||
assertThrows(NullPointerException.class, () -> { new ProbabilisticSampler(null); }); | ||
} | ||
|
||
public void testDefaultGetSampler() { | ||
Settings settings = Settings.builder().put(TRACER_EXPORTER_DELAY_SETTING.getKey(), "1s").build(); | ||
TelemetrySettings telemetrySettings = new TelemetrySettings( | ||
Settings.EMPTY, | ||
new ClusterSettings(settings, Set.of(TRACER_SAMPLER_PROBABILITY, TRACER_ENABLED_SETTING)) | ||
); | ||
|
||
// Probabilistic Sampler | ||
ProbabilisticSampler probabilisticSampler = new ProbabilisticSampler(telemetrySettings); | ||
|
||
assertNotNull(probabilisticSampler.getSampler()); | ||
assertEquals(0.01, probabilisticSampler.getSamplingRatio(), 0.0d); | ||
} | ||
|
||
public void testGetSamplerWithUpdatedSamplingRatio() { | ||
Settings settings = Settings.builder().put(TRACER_EXPORTER_DELAY_SETTING.getKey(), "1s").build(); | ||
TelemetrySettings telemetrySettings = new TelemetrySettings( | ||
Settings.EMPTY, | ||
new ClusterSettings(settings, Set.of(TRACER_SAMPLER_PROBABILITY, TRACER_ENABLED_SETTING)) | ||
); | ||
|
||
// Probabilistic Sampler | ||
ProbabilisticSampler probabilisticSampler = new ProbabilisticSampler(telemetrySettings); | ||
assertEquals(0.01d, probabilisticSampler.getSamplingRatio(), 0.0d); | ||
|
||
telemetrySettings.setSamplingProbability(0.02); | ||
|
||
// Need to call getSampler() to update the value of tracerHeadSamplerSamplingRatio | ||
Sampler updatedProbabilisticSampler = probabilisticSampler.getSampler(); | ||
assertEquals(0.02, probabilisticSampler.getSamplingRatio(), 0.0d); | ||
} | ||
|
||
} |
Oops, something went wrong.