22from urllib .parse import urlsplit
33
44from botocore .awsrequest import AWSRequest
5- from botocore .exceptions import ClientError
65from botocore .response import StreamingBody
76
87import sentry_sdk
9- from sentry_sdk .consts import OP , SPANDATA , SPANSTATUS
8+ from sentry_sdk .consts import OP , SPANDATA
109from sentry_sdk .integrations .boto3 import Boto3Integration
1110from sentry_sdk .traces import StreamedSpan
1211from sentry_sdk .tracing import BAGGAGE_HEADER_NAME , Span
@@ -91,99 +90,6 @@ def _get_client_attributes(
9190 return attributes
9291
9392
94- def _get_response_attributes (response : "Any" ) -> "Attributes" :
95- if not isinstance (response , dict ):
96- return {}
97-
98- metadata = response .get ("ResponseMetadata" )
99- if not isinstance (metadata , dict ):
100- return {}
101-
102- attributes : "Attributes" = {}
103-
104- status_code = metadata .get ("HTTPStatusCode" )
105- # botocore injects HTTP status into `ResponseMetadata` after parsing.
106- # https://github.com/boto/botocore/blob/develop/botocore/parsers.py#L273-L284
107- if (
108- isinstance (status_code , int )
109- and not isinstance (status_code , bool )
110- and 100 <= status_code <= 599
111- ):
112- attributes [SPANDATA .HTTP_STATUS_CODE ] = status_code
113-
114- retry_attempts = metadata .get ("RetryAttempts" )
115- # botocore represents retries as `attempts - 1`; omit zero.
116- # https://github.com/boto/botocore/blob/develop/botocore/endpoint.py#L221-L229
117- # https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-client-span
118- if (
119- isinstance (retry_attempts , int )
120- and not isinstance (retry_attempts , bool )
121- and retry_attempts > 0
122- ):
123- attributes [SPANDATA .HTTP_REQUEST_RESEND_COUNT ] = retry_attempts
124-
125- headers = metadata .get ("HTTPHeaders" )
126- if not isinstance (headers , dict ):
127- headers = {}
128-
129- request_id = metadata .get ("RequestId" )
130- if not isinstance (request_id , str ) or not request_id :
131- request_id = next (
132- (
133- value
134- for value in (
135- headers .get ("x-amzn-requestid" ),
136- headers .get ("x-amzn-request-id" ),
137- headers .get ("x-amz-request-id" ),
138- )
139- if isinstance (value , str ) and value
140- ),
141- None ,
142- )
143- if isinstance (request_id , str ) and request_id :
144- attributes [SPANDATA .AWS_REQUEST_ID ] = request_id
145-
146- # S3's `HostId` is the extended request ID returned in `x-amz-id-2`.
147- # https://docs.aws.amazon.com/AmazonS3/latest/developerguide/get-request-ids.html
148- extended_request_id = metadata .get ("HostId" )
149- if not isinstance (extended_request_id , str ) or not extended_request_id :
150- extended_request_id = headers .get ("x-amz-id-2" )
151- if isinstance (extended_request_id , str ) and extended_request_id :
152- attributes [SPANDATA .AWS_EXTENDED_REQUEST_ID ] = extended_request_id
153-
154- return attributes
155-
156-
157- def _get_error_type (exception : "BaseException" ) -> str :
158- if isinstance (exception , ClientError ):
159- # botocore wraps all AWS service errors in `ClientError`; `Error.Code`
160- # identifies actual service-specific error, e.g. `AccessDenied`.
161- # https://docs.aws.amazon.com/boto3/latest/guide/error-handling.html
162- error = exception .response .get ("Error" )
163- if isinstance (error , dict ):
164- error_code = error .get ("Code" )
165- if isinstance (error_code , str ) and error_code :
166- return error_code
167-
168- # failures before a service response, have no error code. Use exception type
169- # instead. https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/
170- exception_type = type (exception )
171- exception_name = exception_type .__qualname__
172- exception_module = exception_type .__module__
173- if exception_module not in ("builtins" , "__builtins__" ):
174- return "%s.%s" % (exception_module , exception_name )
175- return exception_name
176-
177-
178- def _get_error_attributes (exception : "BaseException" ) -> "Attributes" :
179- attributes : "Attributes" = {}
180- if isinstance (exception , ClientError ):
181- attributes .update (_get_response_attributes (exception .response ))
182-
183- attributes [SPANDATA .ERROR_TYPE ] = _get_error_type (exception )
184- return attributes
185-
186-
18793def _start_client_span (
18894 ctx : "AwsCallContext" ,
18995) -> "Optional[Union[Span, StreamedSpan]]" :
@@ -230,14 +136,6 @@ def _finish_client_span(
230136 span : "Union[Span, StreamedSpan]" ,
231137 parsed : "Dict[str, Any]" ,
232138) -> None :
233- # response metadata is only available after the call. Keep enrichment
234- # isolated so failure cannot prevent `__exit__()` below.
235- attributes : "Attributes" = {}
236- with capture_internal_exceptions ():
237- attributes = _get_response_attributes (parsed )
238-
239- with capture_internal_exceptions ():
240- _set_span_attributes (span , attributes )
241139 span .__exit__ (None , None , None )
242140
243141 body = parsed .get ("Body" )
@@ -275,18 +173,11 @@ def sentry_streaming_body_read(*args: "Any", **kwargs: "Any") -> bytes:
275173 else :
276174 streaming_span .finish ()
277175 return ret
278- except Exception as exc :
279- # enrichment must not replace exception raised by `orig_read()`.
280- # finish span with error, then re-raise.
281- with capture_internal_exceptions ():
282- _set_span_attributes (streaming_span , _get_error_attributes (exc ))
283-
284- with capture_internal_exceptions ():
285- if isinstance (streaming_span , StreamedSpan ):
286- streaming_span .__exit__ (type (exc ), exc , exc .__traceback__ )
287- else :
288- streaming_span .set_status (SPANSTATUS .INTERNAL_ERROR )
289- streaming_span .finish ()
176+ except Exception :
177+ if isinstance (streaming_span , StreamedSpan ):
178+ streaming_span .end ()
179+ else :
180+ streaming_span .finish ()
290181 raise
291182
292183 body .read = sentry_streaming_body_read # type: ignore
@@ -305,12 +196,6 @@ def _finish_client_span_with_error(
305196 span : "Union[Span, StreamedSpan]" ,
306197 exception : "BaseException" ,
307198) -> None :
308- attributes : "Attributes" = {}
309- with capture_internal_exceptions ():
310- attributes = _get_error_attributes (exception )
311-
312- with capture_internal_exceptions ():
313- _set_span_attributes (span , attributes )
314199 span .__exit__ (type (exception ), exception , exception .__traceback__ )
315200
316201
0 commit comments