-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RD-11321-otel-distros-python-and-node-filter-out-empty-sqs-polls (#481)
* feat: do not export empty sqs polling spans * test: new span processor and empty sqs span skipping * test: integration test empty sqs polling span skipping * docs: update README.md with filter empty sqs messages feature * docs: detailed possible values for LUMIGO_AUTO_FILTER_EMPTY_SQS
- Loading branch information
1 parent
c436f0c
commit e21f5ac
Showing
12 changed files
with
356 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
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 @@ | ||
AUTO_FILTER_EMPTY_SQS = "LUMIGO_AUTO_FILTER_EMPTY_SQS" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from opentelemetry.trace import Span | ||
from opentelemetry.sdk.trace import ReadableSpan | ||
from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
|
||
from lumigo_opentelemetry import logger | ||
|
||
|
||
# A span attributes that if is set to True, the span will not be exported | ||
SKIP_EXPORT_SPAN_ATTRIBUTE = "SKIP_EXPORT" | ||
|
||
|
||
class LumigoSpanProcessor(BatchSpanProcessor): | ||
def on_end(self, span: ReadableSpan) -> None: | ||
if should_skip_exporting_span(span): | ||
logger.debug("Not exporting span because it has NO_EXPORT=True attribute") | ||
return | ||
|
||
super().on_end(span) | ||
|
||
|
||
def should_skip_exporting_span(span: ReadableSpan) -> bool: | ||
""" | ||
Given a span, returns an answer if the span should be exported or not. | ||
@param span: A readable span to check | ||
@return: True if the span should not be exported, False otherwise | ||
""" | ||
return span.attributes.get(SKIP_EXPORT_SPAN_ATTRIBUTE, False) is True | ||
|
||
|
||
def set_span_skip_export(span: Span, skip_export: bool = True) -> None: | ||
""" | ||
marks the span as a span not intended for export (for example in spans that create a lot of noise and customers | ||
do not want to trace) | ||
@param span: The span to mark (The span is altered in place) | ||
@param skip_export: Should the span be exported or not (default is True, the span will not be exported) | ||
@return: | ||
""" | ||
|
||
span.set_attributes({SKIP_EXPORT_SPAN_ATTRIBUTE: skip_export}) |
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
108 changes: 108 additions & 0 deletions
108
src/test/unit/instrumentations/botocore/test_parsers.py
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,108 @@ | ||
import logging | ||
|
||
import pytest | ||
from unittest.mock import Mock, patch | ||
|
||
from lumigo_opentelemetry.instrumentations.botocore.parsers import SqsParser | ||
|
||
|
||
EMPTY_SQS_RESULT_1 = {} | ||
EMPTY_SQS_RESULT_2 = {"Messages": []} | ||
NON_EMPTY_SQS_RESULT = {"Messages": [{"MessageId": "1234", "Body": "test"}]} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"env_var_value, operation, result, should_skip", | ||
[ | ||
# Check that empty sqs polls are skipped | ||
("true", "ReceiveMessage", EMPTY_SQS_RESULT_1, True), | ||
("true", "ReceiveMessage", EMPTY_SQS_RESULT_2, True), | ||
# Check that non-empty polls are not skipped | ||
("true", "ReceiveMessage", NON_EMPTY_SQS_RESULT, False), | ||
# Check that other operations are not skipped | ||
("true", "DeleteMessage", EMPTY_SQS_RESULT_1, False), | ||
("true", "DeleteMessageBatch", EMPTY_SQS_RESULT_1, False), | ||
("true", "SendMessage", EMPTY_SQS_RESULT_1, False), | ||
("true", "SendMessageBatch", EMPTY_SQS_RESULT_1, False), | ||
("true", "UnknownOperation", EMPTY_SQS_RESULT_1, False), | ||
("true", None, EMPTY_SQS_RESULT_1, False), | ||
# Check that empty sqs polls are not skipped if the env var is set to false | ||
("false", "ReceiveMessage", EMPTY_SQS_RESULT_1, False), | ||
("false", "ReceiveMessage", EMPTY_SQS_RESULT_2, False), | ||
# Check that non-empty polls are not skipped if the env var is set to false | ||
("false", "ReceiveMessage", NON_EMPTY_SQS_RESULT, False), | ||
# Check that the default behavior is to skip empty sqs polls | ||
(None, "ReceiveMessage", EMPTY_SQS_RESULT_1, True), | ||
(None, "ReceiveMessage", EMPTY_SQS_RESULT_2, True), | ||
("UnsupportedEnvVarValue", "ReceiveMessage", EMPTY_SQS_RESULT_2, True), | ||
], | ||
) | ||
def test_sqs_skip_sqs_response( | ||
env_var_value, operation, result, should_skip, monkeypatch | ||
): | ||
if env_var_value is not None: | ||
monkeypatch.setenv("LUMIGO_AUTO_FILTER_EMPTY_SQS", env_var_value) | ||
|
||
assert ( | ||
SqsParser._should_skip_empty_sqs_polling_response(operation, result) | ||
== should_skip | ||
) | ||
|
||
|
||
@patch( | ||
"lumigo_opentelemetry.instrumentations.botocore.parsers.SqsParser._should_skip_empty_sqs_polling_response" | ||
) | ||
def test_parse_sqs_response_skipping_empty_polls_outputs_log(should_skip_mock, caplog): | ||
should_skip_mock.return_value = True | ||
span = Mock(set_attribute=Mock()) | ||
service_name = "sqs" | ||
operation_name = "ReceiveMessage" | ||
result = { | ||
"ResponseMetadata": { | ||
"RequestId": "54bf0dab-cfab-5fa5-b284-50d83403c94c", | ||
"HTTPStatusCode": 200, | ||
"HTTPHeaders": { | ||
"x-amzn-requestid": "54bf0dab-cfab-5fa5-b284-50d83403c94c", | ||
"date": "Thu, 07 Sep 2023 16:25:12 GMT", | ||
"content-type": "text/xml", | ||
"content-length": "240", | ||
"connection": "keep-alive", | ||
}, | ||
"RetryAttempts": 0, | ||
} | ||
} | ||
|
||
with caplog.at_level(logging.INFO): | ||
SqsParser.parse_response(span, service_name, operation_name, result) | ||
|
||
assert "not tracing empty sqs polling requests" in caplog.text.lower() | ||
|
||
|
||
@patch( | ||
"lumigo_opentelemetry.instrumentations.botocore.parsers.SqsParser._should_skip_empty_sqs_polling_response" | ||
) | ||
def test_parse_sqs_response_not_skipping_polls_no_output_log(should_skip_mock, caplog): | ||
should_skip_mock.return_value = False | ||
span = Mock(set_attribute=Mock()) | ||
service_name = "sqs" | ||
operation_name = "ReceiveMessage" | ||
result = { | ||
"ResponseMetadata": { | ||
"RequestId": "54bf0dab-cfab-5fa5-b284-50d83403c94c", | ||
"HTTPStatusCode": 200, | ||
"HTTPHeaders": { | ||
"x-amzn-requestid": "54bf0dab-cfab-5fa5-b284-50d83403c94c", | ||
"date": "Thu, 07 Sep 2023 16:25:12 GMT", | ||
"content-type": "text/xml", | ||
"content-length": "240", | ||
"connection": "keep-alive", | ||
}, | ||
"RetryAttempts": 0, | ||
} | ||
} | ||
|
||
SqsParser.parse_response(span, service_name, operation_name, result) | ||
|
||
assert "not tracing empty sqs polling requests" not in caplog.text.lower() | ||
|
||
# Make sure that there is an info log |
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
Oops, something went wrong.