|
27 | 27 |
|
28 | 28 | try: |
29 | 29 | from botocore.awsrequest import AWSRequest |
| 30 | + from botocore.exceptions import ClientError |
30 | 31 | from botocore.response import StreamingBody |
31 | 32 | except ImportError: |
32 | 33 | raise DidNotEnable("botocore not installed") |
|
38 | 39 | def _set_span_attributes( |
39 | 40 | span: "Union[Span, StreamedSpan]", attributes: "Attributes" |
40 | 41 | ) -> None: |
| 42 | + """Will be removed in the major.""" |
41 | 43 | if isinstance(span, StreamedSpan): |
42 | 44 | span.set_attributes(attributes) |
43 | 45 | return |
@@ -89,6 +91,99 @@ def _get_client_attributes( |
89 | 91 | return attributes |
90 | 92 |
|
91 | 93 |
|
| 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 | + attributes: "Attributes" = {} |
| 102 | + |
| 103 | + # botocore injects HTTP status into `ResponseMetadata` after parsing. |
| 104 | + # https://github.com/boto/botocore/blob/develop/botocore/parsers.py#L273-L284 |
| 105 | + status_code = metadata.get("HTTPStatusCode") |
| 106 | + if isinstance(status_code, int) and 100 <= status_code <= 599: |
| 107 | + attributes[SPANDATA.HTTP_STATUS_CODE] = status_code |
| 108 | + |
| 109 | + retry_attempts = metadata.get("RetryAttempts") |
| 110 | + # botocore represents retries as `attempts - 1`; OTel suggests "if and only if", so skip zero. |
| 111 | + # https://github.com/boto/botocore/blob/develop/botocore/endpoint.py#L221-L229 |
| 112 | + # https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-client-span |
| 113 | + if ( |
| 114 | + isinstance(retry_attempts, int) |
| 115 | + # avoid emitting `resend_count=True`. |
| 116 | + and not isinstance(retry_attempts, bool) |
| 117 | + and retry_attempts > 0 |
| 118 | + ): |
| 119 | + attributes[SPANDATA.HTTP_REQUEST_RESEND_COUNT] = retry_attempts |
| 120 | + |
| 121 | + headers = metadata.get("HTTPHeaders") |
| 122 | + if not isinstance(headers, dict): |
| 123 | + headers = {} |
| 124 | + |
| 125 | + request_id = next( |
| 126 | + ( |
| 127 | + value |
| 128 | + for value in ( |
| 129 | + metadata.get("RequestId"), |
| 130 | + headers.get("x-amzn-requestid"), |
| 131 | + headers.get("x-amzn-request-id"), |
| 132 | + headers.get("x-amz-request-id"), |
| 133 | + ) |
| 134 | + if isinstance(value, str) and value |
| 135 | + ), |
| 136 | + None, |
| 137 | + ) |
| 138 | + if request_id is not None: |
| 139 | + attributes[SPANDATA.AWS_REQUEST_ID] = request_id |
| 140 | + |
| 141 | + # S3's `HostId` is the extended request ID returned in `x-amz-id-2`. |
| 142 | + # https://docs.aws.amazon.com/AmazonS3/latest/developerguide/get-request-ids.html |
| 143 | + extended_request_id = next( |
| 144 | + ( |
| 145 | + value |
| 146 | + for value in (metadata.get("HostId"), headers.get("x-amz-id-2")) |
| 147 | + if isinstance(value, str) and value |
| 148 | + ), |
| 149 | + None, |
| 150 | + ) |
| 151 | + if extended_request_id is not None: |
| 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 | + # `ClientError` wraps AWS service errors; `Error.Code` identifies the |
| 160 | + # actual service error, e.g. `AccessDeniedException`. |
| 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 AWS error code. |
| 169 | + # 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 | + |
92 | 187 | def _start_client_span( |
93 | 188 | ctx: "AwsCallContext", |
94 | 189 | ) -> "Optional[Union[Span, StreamedSpan]]": |
@@ -200,6 +295,13 @@ def finish(error: "Optional[BaseException]" = None) -> None: |
200 | 295 | return |
201 | 296 |
|
202 | 297 | finished = True |
| 298 | + if error is not None: |
| 299 | + with capture_internal_exceptions(): |
| 300 | + attributes = _get_error_attributes(error) |
| 301 | + _set_span_attributes(streaming_span, attributes) |
| 302 | + if isinstance(span, StreamedSpan): |
| 303 | + _set_span_attributes(span, attributes) |
| 304 | + |
203 | 305 | _finish_span(streaming_span, error) |
204 | 306 | if isinstance(span, StreamedSpan): |
205 | 307 | _finish_span(span, error) |
|
0 commit comments