-
Notifications
You must be signed in to change notification settings - Fork 25
Export unsampled span in AWS Lambda environment #247
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
288df57
Export unsampled span in AWS Lambda environment
wangzlei f91d294
Fix unittest typo
wangzlei ec98293
fix unit test coverage
wangzlei c881fd7
fix unit test coverage
wangzlei d2bbcb6
fix unit test coverage
wangzlei a1154df
fix unit test coverage
wangzlei a1d322d
fix unit test coverage
wangzlei 0fd43f8
fix lint
wangzlei 8108f4d
address comments
wangzlei 15147d0
Merge branch 'main' into main
wangzlei 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
42 changes: 42 additions & 0 deletions
42
...pentelemetry-distro/src/amazon/opentelemetry/distro/aws_batch_unsampled_span_processor.py
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,42 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import logging | ||
from typing import Optional | ||
|
||
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_TRACE_FLAG_UNSAMPLED | ||
from opentelemetry.context import Context | ||
from opentelemetry.sdk.trace import ReadableSpan, Span | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor as BaseBatchSpanProcessor | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
SPANUNSAMPLED_FLAG = "OTEL_AWS_APP_SIGNALS_ENABLED" | ||
|
||
|
||
class BatchUnsampledSpanProcessor(BaseBatchSpanProcessor): | ||
|
||
# pylint: disable=no-self-use | ||
def on_start(self, span: Span, parent_context: Optional[Context] = None) -> None: | ||
if not span.context.trace_flags.sampled: | ||
span.set_attribute(AWS_TRACE_FLAG_UNSAMPLED, True) | ||
|
||
def on_end(self, span: ReadableSpan) -> None: | ||
if span.context.trace_flags.sampled: | ||
return | ||
|
||
if self.done: | ||
logger.warning("Already shutdown, dropping span.") | ||
return | ||
|
||
if len(self.queue) == self.max_queue_size: | ||
# pylint: disable=access-member-before-definition | ||
if not self._spans_dropped: | ||
logger.warning("Queue is full, likely spans will be dropped.") | ||
# pylint: disable=attribute-defined-outside-init | ||
self._spans_dropped = True | ||
|
||
self.queue.appendleft(span) | ||
|
||
if len(self.queue) >= self.max_export_batch_size: | ||
with self.condition: | ||
self.condition.notify() |
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
54 changes: 54 additions & 0 deletions
54
...metry-distro/tests/amazon/opentelemetry/distro/test_aws_batch_unsampled_span_processor.py
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,54 @@ | ||
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
from unittest import TestCase | ||
from unittest.mock import MagicMock, patch | ||
|
||
from amazon.opentelemetry.distro._aws_attribute_keys import AWS_TRACE_FLAG_UNSAMPLED | ||
from amazon.opentelemetry.distro.aws_batch_unsampled_span_processor import BatchUnsampledSpanProcessor | ||
from opentelemetry.trace import TraceFlags | ||
|
||
|
||
class TestBatchUnsampledSpanProcessor(TestCase): | ||
|
||
def setUp(self): | ||
self.mock_exporter = MagicMock() | ||
self.processor = BatchUnsampledSpanProcessor(self.mock_exporter, max_queue_size=1, max_export_batch_size=1) | ||
|
||
@patch("opentelemetry.sdk.trace.Span") | ||
def test_on_end_sampled(self, mock_span_class): | ||
trace_flags = TraceFlags(TraceFlags.SAMPLED) | ||
|
||
mock_span = mock_span_class.return_value | ||
mock_span.context.trace_flags = trace_flags | ||
|
||
self.processor.on_start(mock_span) | ||
self.processor.on_end(mock_span) | ||
|
||
self.assertEqual(len(self.processor.queue), 0) | ||
mock_span.set_attribute.assert_not_called() | ||
|
||
@patch("opentelemetry.sdk.trace.Span") | ||
def test_on_end_not_sampled(self, mock_span_class): | ||
|
||
trace_flags = TraceFlags(0) | ||
mock_span1 = mock_span_class.return_value | ||
mock_span1.context.trace_flags = trace_flags | ||
|
||
self.processor.on_start(mock_span1) | ||
self.processor.on_end(mock_span1) | ||
|
||
mock_span2 = mock_span_class.return_value | ||
mock_span2.context.trace_flags = trace_flags | ||
self.processor.on_start(mock_span2) | ||
self.processor.on_end(mock_span2) | ||
|
||
self.assertEqual(len(self.processor.queue), 1) | ||
self.assertIn(AWS_TRACE_FLAG_UNSAMPLED, mock_span1.set_attribute.call_args_list[0][0][0]) | ||
|
||
self.processor.shutdown() | ||
mock_span2 = mock_span_class.return_value | ||
mock_span2.context.trace_flags = trace_flags | ||
self.processor.on_start(mock_span2) | ||
self.processor.on_end(mock_span2) | ||
|
||
self.assertEqual(len(self.processor.queue), 0) |
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
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.