-
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.
Signed-off-by: Nishchay Malhotra <nishcha@amazon.com>
- Loading branch information
1 parent
1e1d624
commit 8bce540
Showing
10 changed files
with
459 additions
and
5 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
47 changes: 47 additions & 0 deletions
47
...etry-otel/src/test/java/org/opensearch/telemetry/tracing/processor/MockSpanProcessor.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,47 @@ | ||
/* | ||
* 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.processor; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
|
||
public class MockSpanProcessor implements SpanProcessor { | ||
|
||
@Override | ||
public void onStart(Context parentContext, ReadWriteSpan span) { | ||
{} | ||
} | ||
|
||
@Override | ||
public boolean isStartRequired() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void onEnd(ReadableSpan span) {} | ||
|
||
@Override | ||
public boolean isEndRequired() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode forceFlush() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...otel/src/test/java/org/opensearch/telemetry/tracing/processor/OTelSpanProcessorTests.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,84 @@ | ||
/* | ||
* 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.processor; | ||
|
||
import org.opensearch.telemetry.tracing.attributes.SamplingAttributes; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.api.trace.TraceFlags; | ||
import io.opentelemetry.api.trace.TraceState; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
import org.mockito.Mockito; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class OTelSpanProcessorTests extends OpenSearchTestCase { | ||
|
||
private OTelSpanProcessor oTelSpanProcessor; | ||
private MockSpanProcessor mockSpanProcessor; | ||
private static final String TRACE_ID = "4aa59968f31dcbff7807741afa9d7d62"; | ||
private static final String SPAN_ID = "bea205cd25756b5e"; | ||
|
||
private ReadableSpan createEndedSpan() { | ||
return Mockito.mock(ReadableSpan.class); | ||
} | ||
|
||
public void testOTelSpanProcessorDelegation() { | ||
mockSpanProcessor = new MockSpanProcessor(); | ||
oTelSpanProcessor = new OTelSpanProcessor(mockSpanProcessor); | ||
assertEquals(mockSpanProcessor.forceFlush(), oTelSpanProcessor.forceFlush()); | ||
assertEquals(mockSpanProcessor.shutdown(), oTelSpanProcessor.shutdown()); | ||
assertEquals(mockSpanProcessor.isEndRequired(), oTelSpanProcessor.isEndRequired()); | ||
assertEquals(mockSpanProcessor.isStartRequired(), oTelSpanProcessor.isStartRequired()); | ||
} | ||
|
||
public void testOnEndFunction() { | ||
SpanProcessor mockProcessor = mock(SpanProcessor.class); | ||
oTelSpanProcessor = new OTelSpanProcessor(mockProcessor); | ||
ReadableSpan readableSpan = this.createEndedSpan(); | ||
when(readableSpan.getSpanContext()).thenReturn( | ||
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault()) | ||
); | ||
oTelSpanProcessor.onEnd(readableSpan); | ||
Mockito.verify(mockProcessor, Mockito.times(1)).onEnd(readableSpan); | ||
} | ||
|
||
public void testOnEndFunctionWithInferredAttribute() { | ||
SpanProcessor mockProcessor = mock(SpanProcessor.class); | ||
oTelSpanProcessor = new OTelSpanProcessor(mockProcessor); | ||
ReadableSpan readableSpan = this.createEndedSpan(); | ||
when(readableSpan.getSpanContext()).thenReturn( | ||
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TraceState.getDefault()) | ||
); | ||
when(readableSpan.getAttribute(AttributeKey.stringKey(SamplingAttributes.SAMPLER.getValue()))).thenReturn( | ||
SamplingAttributes.INFERRED_SAMPLER.getValue() | ||
); | ||
oTelSpanProcessor.onEnd(readableSpan); | ||
Mockito.verify(mockProcessor, Mockito.times(0)).onEnd(readableSpan); | ||
} | ||
|
||
public void testOnEndFunctionWithInferredAttributeAndSampled() { | ||
SpanProcessor mockProcessor = mock(SpanProcessor.class); | ||
oTelSpanProcessor = new OTelSpanProcessor(mockProcessor); | ||
ReadableSpan readableSpan = this.createEndedSpan(); | ||
when(readableSpan.getSpanContext()).thenReturn( | ||
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TraceState.getDefault()) | ||
); | ||
when(readableSpan.getAttribute(AttributeKey.stringKey(SamplingAttributes.SAMPLER.getValue()))).thenReturn( | ||
SamplingAttributes.INFERRED_SAMPLER.getValue() | ||
); | ||
when(readableSpan.getAttribute(AttributeKey.booleanKey(SamplingAttributes.SAMPLED.getValue()))).thenReturn(true); | ||
oTelSpanProcessor.onEnd(readableSpan); | ||
Mockito.verify(mockProcessor, Mockito.times(1)).onEnd(readableSpan); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
...try-otel/src/test/java/org/opensearch/telemetry/tracing/sampler/InferredSamplerTests.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,109 @@ | ||
/* | ||
* 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.telemetry.tracing.attributes.SamplingAttributes; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
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.samplers.Sampler; | ||
import io.opentelemetry.sdk.trace.samplers.SamplingResult; | ||
|
||
import static org.opensearch.telemetry.TelemetrySettings.TRACER_ENABLED_SETTING; | ||
import static org.opensearch.telemetry.TelemetrySettings.TRACER_INFERRED_SAMPLER_ALLOWLISTED; | ||
import static org.opensearch.telemetry.TelemetrySettings.TRACER_SAMPLER_PROBABILITY; | ||
import static org.opensearch.telemetry.tracing.AttributeNames.TRANSPORT_ACTION; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class InferredSamplerTests extends OpenSearchTestCase { | ||
|
||
public void testGetSamplerWithSettingDisabled() { | ||
ClusterSettings clusterSettings = new ClusterSettings( | ||
Settings.EMPTY, | ||
Set.of(TRACER_SAMPLER_PROBABILITY, TRACER_ENABLED_SETTING, TRACER_INFERRED_SAMPLER_ALLOWLISTED) | ||
); | ||
|
||
TelemetrySettings telemetrySettings = new TelemetrySettings(Settings.EMPTY, clusterSettings); | ||
|
||
// InferredActionSampler | ||
Sampler inferredActionSampler = InferredActionSampler.create(telemetrySettings, Settings.EMPTY, null); | ||
|
||
SamplingResult result = inferredActionSampler.shouldSample( | ||
mock(Context.class), | ||
"00000000000000000000000000000000", | ||
"spanName", | ||
SpanKind.INTERNAL, | ||
Attributes.builder().put(TRANSPORT_ACTION, "dummy_action").build(), | ||
Collections.emptyList() | ||
); | ||
|
||
assertEquals(SamplingResult.drop(), result); | ||
} | ||
|
||
public void testGetSamplerWithSettingEnabled() { | ||
ClusterSettings clusterSettings = new ClusterSettings( | ||
Settings.EMPTY, | ||
Set.of(TRACER_SAMPLER_PROBABILITY, TRACER_ENABLED_SETTING, TRACER_INFERRED_SAMPLER_ALLOWLISTED) | ||
); | ||
|
||
TelemetrySettings telemetrySettings = new TelemetrySettings(Settings.EMPTY, clusterSettings); | ||
telemetrySettings.setInferredSamplingAllowListed(true); | ||
|
||
// InferredActionSampler | ||
Sampler inferredActionSampler = InferredActionSampler.create(telemetrySettings, Settings.EMPTY, null); | ||
|
||
SamplingResult result = inferredActionSampler.shouldSample( | ||
mock(Context.class), | ||
"00000000000000000000000000000000", | ||
"spanName", | ||
SpanKind.INTERNAL, | ||
Attributes.builder().put(TRANSPORT_ACTION, "dummy_action").build(), | ||
Collections.emptyList() | ||
); | ||
|
||
assertEquals(SamplingResult.recordAndSample().getDecision(), result.getDecision()); | ||
} | ||
|
||
public void testGetSamplerWithAddedAttributes() { | ||
ClusterSettings clusterSettings = new ClusterSettings( | ||
Settings.EMPTY, | ||
Set.of(TRACER_SAMPLER_PROBABILITY, TRACER_ENABLED_SETTING, TRACER_INFERRED_SAMPLER_ALLOWLISTED) | ||
); | ||
|
||
TelemetrySettings telemetrySettings = new TelemetrySettings(Settings.EMPTY, clusterSettings); | ||
telemetrySettings.setInferredSamplingAllowListed(true); | ||
|
||
// InferredActionSampler | ||
Sampler inferredActionSampler = InferredActionSampler.create(telemetrySettings, Settings.EMPTY, null); | ||
|
||
SamplingResult result = inferredActionSampler.shouldSample( | ||
mock(Context.class), | ||
"00000000000000000000000000000000", | ||
"spanName", | ||
SpanKind.INTERNAL, | ||
Attributes.builder().put(TRANSPORT_ACTION, "dummy_action").build(), | ||
Collections.emptyList() | ||
); | ||
|
||
assertTrue(result.getAttributes().asMap().containsKey(AttributeKey.stringKey(SamplingAttributes.SAMPLER.getValue()))); | ||
assertEquals( | ||
result.getAttributes().get(AttributeKey.stringKey(SamplingAttributes.SAMPLER.getValue())), | ||
SamplingAttributes.INFERRED_SAMPLER.getValue() | ||
); | ||
} | ||
} |
Oops, something went wrong.