-
Notifications
You must be signed in to change notification settings - Fork 64
Adding AwsUnsampledOnlySpanProcessor to export batches of unsampled spans #948
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
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc7af45
adding span processor for exporting only unsampled spans
srprash 56bf70c
spotless apply
srprash d820316
refactor unit test
srprash 11a454c
javadoc and some cleanup
srprash 17fdd89
spotless apply
srprash d236e90
bug fix
srprash 3557f6a
more unit tests
srprash 43af1e5
populate aws.trace.flag.sampled attribute only if unsampled
srprash 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
87 changes: 87 additions & 0 deletions
87
...java/software/amazon/opentelemetry/javaagent/providers/AwsUnsampledOnlySpanProcessor.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,87 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.opentelemetry.javaagent.providers; | ||
|
||
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; | ||
|
||
/** | ||
* {@link SpanProcessor} that only exports unsampled spans in a batch via a delegated @{link | ||
* BatchSpanProcessor}. The processor also adds an attribute to each processed span to indicate that | ||
* it was sampled or not. | ||
*/ | ||
final class AwsUnsampledOnlySpanProcessor implements SpanProcessor { | ||
|
||
private final SpanProcessor delegate; | ||
|
||
AwsUnsampledOnlySpanProcessor(SpanProcessor delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public static AwsUnsampledOnlySpanProcessorBuilder builder() { | ||
return new AwsUnsampledOnlySpanProcessorBuilder(); | ||
} | ||
|
||
@Override | ||
public void onStart(Context parentContext, ReadWriteSpan span) { | ||
if (span.getSpanContext().isSampled()) { | ||
span.setAttribute(AwsAttributeKeys.AWS_TRACE_FLAG_SAMPLED, true); | ||
} else { | ||
span.setAttribute(AwsAttributeKeys.AWS_TRACE_FLAG_SAMPLED, false); | ||
srprash marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
delegate.onStart(parentContext, span); | ||
} | ||
|
||
@Override | ||
public void onEnd(ReadableSpan span) { | ||
if (!span.getSpanContext().isSampled()) { | ||
delegate.onEnd(span); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isStartRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isEndRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
return delegate.shutdown(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode forceFlush() { | ||
return delegate.forceFlush(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
delegate.close(); | ||
} | ||
|
||
// Visible for testing | ||
SpanProcessor getDelegate() { | ||
return delegate; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ftware/amazon/opentelemetry/javaagent/providers/AwsUnsampledOnlySpanProcessorBuilder.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,46 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.opentelemetry.javaagent.providers; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; | ||
import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
|
||
final class AwsUnsampledOnlySpanProcessorBuilder { | ||
|
||
// Default exporter is OtlpUdpSpanExporter with unsampled payload prefix | ||
private SpanExporter exporter = | ||
new OtlpUdpSpanExporterBuilder() | ||
.setPayloadSampleDecision(TracePayloadSampleDecision.UNSAMPLED) | ||
.build(); | ||
|
||
public AwsUnsampledOnlySpanProcessorBuilder setSpanExporter(SpanExporter exporter) { | ||
requireNonNull(exporter, "exporter cannot be null"); | ||
this.exporter = exporter; | ||
return this; | ||
} | ||
|
||
public AwsUnsampledOnlySpanProcessor build() { | ||
BatchSpanProcessor bsp = | ||
BatchSpanProcessor.builder(exporter).setExportUnsampledSpans(true).build(); | ||
return new AwsUnsampledOnlySpanProcessor(bsp); | ||
} | ||
|
||
SpanExporter getSpanExporter() { | ||
return exporter; | ||
} | ||
} |
163 changes: 163 additions & 0 deletions
163
.../software/amazon/opentelemetry/javaagent/providers/AwsUnsampledOnlySpanProcessorTest.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,163 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.opentelemetry.javaagent.providers; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.*; | ||
|
||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; | ||
import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class AwsUnsampledOnlySpanProcessorTest { | ||
|
||
@Test | ||
public void testIsStartRequired() { | ||
SpanProcessor processor = AwsUnsampledOnlySpanProcessor.builder().build(); | ||
assertThat(processor.isStartRequired()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testIsEndRequired() { | ||
SpanProcessor processor = AwsUnsampledOnlySpanProcessor.builder().build(); | ||
assertThat(processor.isEndRequired()).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testDefaultSpanProcessor() { | ||
AwsUnsampledOnlySpanProcessorBuilder builder = AwsUnsampledOnlySpanProcessor.builder(); | ||
AwsUnsampledOnlySpanProcessor unsampledSP = builder.build(); | ||
|
||
assertThat(builder.getSpanExporter()).isInstanceOf(OtlpUdpSpanExporter.class); | ||
SpanProcessor delegate = unsampledSP.getDelegate(); | ||
assertThat(delegate).isInstanceOf(BatchSpanProcessor.class); | ||
BatchSpanProcessor delegateBsp = (BatchSpanProcessor) delegate; | ||
String delegateBspString = delegateBsp.toString(); | ||
assertThat(delegateBspString) | ||
.contains( | ||
"spanExporter=software.amazon.opentelemetry.javaagent.providers.OtlpUdpSpanExporter"); | ||
assertThat(delegateBspString).contains("exportUnsampledSpans=true"); | ||
} | ||
|
||
@Test | ||
public void testSpanProcessorWithExporter() { | ||
AwsUnsampledOnlySpanProcessorBuilder builder = | ||
AwsUnsampledOnlySpanProcessor.builder().setSpanExporter(InMemorySpanExporter.create()); | ||
AwsUnsampledOnlySpanProcessor unsampledSP = builder.build(); | ||
|
||
assertThat(builder.getSpanExporter()).isInstanceOf(InMemorySpanExporter.class); | ||
SpanProcessor delegate = unsampledSP.getDelegate(); | ||
assertThat(delegate).isInstanceOf(BatchSpanProcessor.class); | ||
BatchSpanProcessor delegateBsp = (BatchSpanProcessor) delegate; | ||
String delegateBspString = delegateBsp.toString(); | ||
assertThat(delegateBspString) | ||
.contains("spanExporter=io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter"); | ||
assertThat(delegateBspString).contains("exportUnsampledSpans=true"); | ||
} | ||
|
||
@Test | ||
public void testStartAddsAttributeToSampledSpan() { | ||
SpanContext mockSpanContext = mock(SpanContext.class); | ||
when(mockSpanContext.isSampled()).thenReturn(true); | ||
Context parentContextMock = mock(Context.class); | ||
ReadWriteSpan spanMock = mock(ReadWriteSpan.class); | ||
when(spanMock.getSpanContext()).thenReturn(mockSpanContext); | ||
|
||
AwsUnsampledOnlySpanProcessor processor = AwsUnsampledOnlySpanProcessor.builder().build(); | ||
processor.onStart(parentContextMock, spanMock); | ||
|
||
// verify setAttribute was called with the correct arguments | ||
verify(spanMock, times(1)).setAttribute(AwsAttributeKeys.AWS_TRACE_FLAG_SAMPLED, true); | ||
} | ||
|
||
@Test | ||
public void testStartAddsAttributeToUnsampledSpan() { | ||
SpanContext mockSpanContext = mock(SpanContext.class); | ||
when(mockSpanContext.isSampled()).thenReturn(false); | ||
Context parentContextMock = mock(Context.class); | ||
ReadWriteSpan spanMock = mock(ReadWriteSpan.class); | ||
when(spanMock.getSpanContext()).thenReturn(mockSpanContext); | ||
|
||
AwsUnsampledOnlySpanProcessor processor = AwsUnsampledOnlySpanProcessor.builder().build(); | ||
processor.onStart(parentContextMock, spanMock); | ||
|
||
// verify setAttribute was called with the correct arguments | ||
verify(spanMock, times(1)).setAttribute(AwsAttributeKeys.AWS_TRACE_FLAG_SAMPLED, false); | ||
} | ||
|
||
@Test | ||
public void testExportsOnlyUnsampledSpans() { | ||
SpanExporter mockExporter = mock(SpanExporter.class); | ||
when(mockExporter.export(anyCollection())).thenReturn(CompletableResultCode.ofSuccess()); | ||
|
||
TestDelegateProcessor delegate = new TestDelegateProcessor(); | ||
AwsUnsampledOnlySpanProcessor processor = new AwsUnsampledOnlySpanProcessor(delegate); | ||
|
||
// unsampled span | ||
SpanContext mockSpanContextUnsampled = mock(SpanContext.class); | ||
when(mockSpanContextUnsampled.isSampled()).thenReturn(false); | ||
ReadableSpan mockSpanUnsampled = mock(ReadableSpan.class); | ||
when(mockSpanUnsampled.getSpanContext()).thenReturn(mockSpanContextUnsampled); | ||
|
||
// sampled span | ||
SpanContext mockSpanContextSampled = mock(SpanContext.class); | ||
when(mockSpanContextSampled.isSampled()).thenReturn(true); | ||
ReadableSpan mockSpanSampled = mock(ReadableSpan.class); | ||
when(mockSpanSampled.getSpanContext()).thenReturn(mockSpanContextSampled); | ||
|
||
processor.onEnd(mockSpanSampled); | ||
processor.onEnd(mockSpanUnsampled); | ||
|
||
// validate that only the unsampled span was delegated | ||
assertThat(delegate.getEndedSpans()).containsExactly(mockSpanUnsampled); | ||
} | ||
|
||
private static class TestDelegateProcessor implements SpanProcessor { | ||
// keep a queue of Readable spans added when onEnd is called | ||
Collection<ReadableSpan> endedSpans = new ArrayList<>(); | ||
|
||
@Override | ||
public void onStart(Context parentContext, ReadWriteSpan span) {} | ||
|
||
@Override | ||
public boolean isStartRequired() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void onEnd(ReadableSpan span) { | ||
endedSpans.add(span); | ||
} | ||
|
||
@Override | ||
public boolean isEndRequired() { | ||
return false; | ||
} | ||
|
||
public Collection<ReadableSpan> getEndedSpans() { | ||
return endedSpans; | ||
} | ||
} | ||
} |
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.