Skip to content

Commit

Permalink
fix: aws: response_hook dump raises exception while handling bytes (#717
Browse files Browse the repository at this point in the history
)
  • Loading branch information
GuyMoses committed Apr 1, 2024
1 parent 1284cc2 commit f9eef45
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"asgiref~=3.0",
"autowrapt>=1.0",
"wrapt>=1.11.0",
"lumigo_core==0.0.6",
"lumigo_core==0.0.10",
"opentelemetry-api==1.20.0",
"opentelemetry-sdk==1.20.0",
"opentelemetry-sdk-extension-aws==2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,12 @@ def parse_response(
) -> None:
headers = result.get("ResponseMetadata", {}).get("HTTPHeaders", {})
attributes = {
"http.response.body": dump_with_context("responseBody", result),
"http.response.headers": dump_with_context("responseHeaders", headers),
"http.response.body": cls.safe_dump_payload(
context="responseBody", payload=result
),
"http.response.headers": cls.safe_dump_payload(
context="responseHeaders", payload=headers
),
"http.status_code": result.get("ResponseMetadata", {}).get(
"HTTPStatusCode", ""
),
Expand All @@ -227,6 +231,14 @@ def parse_response(
}
span.set_attributes(attributes)

@classmethod
def safe_dump_payload(cls, context: str, payload: Any) -> Optional[Any]:
try:
return dump_with_context(context, payload)
except Exception:
logger.info(f"An exception occurred in while extracting: {context}")
return f"{context} is unavailable"

@classmethod
def response_hook(
cls, span: Span, service_name: str, operation_name: str, result: Dict[Any, Any]
Expand Down
23 changes: 22 additions & 1 deletion src/test/unit/instrumentations/botocore/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from unittest.mock import Mock, patch

from lumigo_opentelemetry.instrumentations.botocore.parsers import SqsParser
from lumigo_opentelemetry.instrumentations.botocore.parsers import SqsParser, AwsParser


EMPTY_SQS_RESULT_1 = {}
Expand Down Expand Up @@ -109,3 +109,24 @@ def test_parse_sqs_response_not_skipping_polls_no_output_log(should_skip_mock, c
assert "not tracing empty sqs polling requests" not in caplog.text.lower()

# Make sure that there is an info log


@patch("lumigo_opentelemetry.instrumentations.botocore.parsers.dump_with_context")
def test_parse_response_handles_unparsable_payload(dump_with_context_mock, caplog):
dump_with_context_mock.side_effect = Exception("Boom!")

span = Mock(set_attribute=Mock())
result = {"content": {}}

# no exception
assert (
AwsParser.parse_response(
span=span,
service_name="service-name",
operation_name="operation",
result=result,
)
is None
)

assert "An exception occurred in while extracting" in caplog.text

0 comments on commit f9eef45

Please sign in to comment.