Skip to content

Commit 3532b2a

Browse files
chore: [SVLS-5973] draft of s3 logic refactor to remove exeptions
1 parent 1673d2e commit 3532b2a

File tree

1 file changed

+39
-25
lines changed
  • ddtrace/_trace/utils_botocore/span_pointers

1 file changed

+39
-25
lines changed

ddtrace/_trace/utils_botocore/span_pointers/s3.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Dict
44
from typing import List
55
from typing import NamedTuple
6+
from typing import Optional
67

78
from ddtrace._trace._span_pointer import _SpanPointerDescription
89
from ddtrace._trace._span_pointer import _SpanPointerDirection
@@ -94,47 +95,60 @@ def _extract_span_pointers_for_s3_response_with_helper(
9495
record_span_pointer_calculation_issue(operation=f"S3.{operation_name}", issue_tag="request_parameters")
9596
return []
9697

97-
try:
98-
return [
99-
_aws_s3_object_span_pointer_description(
100-
pointer_direction=_SpanPointerDirection.DOWNSTREAM,
101-
bucket=bucket,
102-
key=key,
103-
etag=etag,
104-
)
105-
]
106-
except Exception as e:
107-
log.debug(
108-
"failed to generate S3.%s span pointer: %s",
109-
operation_name,
110-
str(e),
111-
)
112-
record_span_pointer_calculation_issue(operation=f"S3.{operation_name}", issue_tag="calculation")
98+
span_pointer_description = _aws_s3_object_span_pointer_description(
99+
operation=f"S3.{operation_name}",
100+
pointer_direction=_SpanPointerDirection.DOWNSTREAM,
101+
bucket=bucket,
102+
key=key,
103+
etag=etag,
104+
)
105+
if span_pointer_description is None:
113106
return []
114107

108+
return [span_pointer_description]
109+
115110

116111
def _aws_s3_object_span_pointer_description(
112+
operation: str,
117113
pointer_direction: _SpanPointerDirection,
118114
bucket: str,
119115
key: str,
120116
etag: str,
121-
) -> _SpanPointerDescription:
117+
) -> Optional[_SpanPointerDescription]:
118+
pointer_hash = _aws_s3_object_span_pointer_hash(operation, bucket, key, etag)
119+
if pointer_hash is None:
120+
return None
121+
122122
return _SpanPointerDescription(
123123
pointer_kind="aws.s3.object",
124124
pointer_direction=pointer_direction,
125-
pointer_hash=_aws_s3_object_span_pointer_hash(bucket, key, etag),
125+
pointer_hash=pointer_hash,
126126
extra_attributes={},
127127
)
128128

129129

130-
def _aws_s3_object_span_pointer_hash(bucket: str, key: str, etag: str) -> str:
130+
def _aws_s3_object_span_pointer_hash(operation: str, bucket: str, key: str, etag: str) -> Optional[str]:
131131
if '"' in etag:
132132
# Some AWS API endpoints put the ETag in double quotes. We expect the
133133
# calling code to have correctly fixed this already.
134-
raise ValueError(f"ETag should not have double quotes: {etag}")
134+
log.debug(
135+
"ETag should not have double quotes: %s",
136+
etag,
137+
)
138+
record_span_pointer_calculation_issue(operation=operation, issue_tag="etag_quotes")
139+
return None
135140

136-
return _standard_hashing_function(
137-
bucket.encode("ascii"),
138-
key.encode("utf-8"),
139-
etag.encode("ascii"),
140-
)
141+
try:
142+
return _standard_hashing_function(
143+
bucket.encode("ascii"),
144+
key.encode("utf-8"),
145+
etag.encode("ascii"),
146+
)
147+
148+
except Exception as e:
149+
log.debug(
150+
"failed to hash S3 object span pointer: %s",
151+
e,
152+
)
153+
record_span_pointer_calculation_issue(operation=operation, issue_tag="hashing")
154+
return None

0 commit comments

Comments
 (0)