Skip to content

Commit cb54a34

Browse files
committed
fix: Dont crash on AnnotatedValue
1 parent e3944c3 commit cb54a34

2 files changed

Lines changed: 51 additions & 21 deletions

File tree

sentry_sdk/tracing.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55

66
from datetime import datetime
77

8+
from sentry_sdk.utils import concat_strings
9+
10+
811
if False:
12+
from typing import Optional
913
from typing import Any
1014
from typing import Dict
1115
from typing import List
@@ -131,7 +135,7 @@ def from_traceparent(cls, traceparent):
131135
span_id = "{:016x}".format(int(span_id, 16))
132136

133137
if sampled_str:
134-
sampled = sampled_str != "0"
138+
sampled = sampled_str != "0" # type: Optional[bool]
135139
else:
136140
sampled = None
137141

@@ -179,7 +183,7 @@ def record_sql_query(hub, queries):
179183
for query in queries:
180184
hub.add_breadcrumb(message=query, category="query")
181185

182-
description = ";".join(queries)
186+
description = concat_strings(queries)
183187
with hub.span(op="db.statement", description=description) as span:
184188
yield span
185189

sentry_sdk/utils.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,36 @@ def format_and_strip(
688688
if not chunks:
689689
raise ValueError("No formatting placeholders found")
690690

691-
params = list(reversed(params))
691+
params = params[: len(chunks) - 1]
692+
693+
if len(params) < len(chunks) - 1:
694+
raise ValueError("Not enough params.")
695+
696+
concat_chunks = []
697+
iter_chunks = iter(chunks) # type: Optional[Iterator]
698+
iter_params = iter(params) # type: Optional[Iterator]
699+
700+
while iter_chunks is not None or iter_params is not None:
701+
if iter_chunks is not None:
702+
try:
703+
concat_chunks.append(next(iter_chunks))
704+
except StopIteration:
705+
iter_chunks = None
706+
707+
if iter_params is not None:
708+
try:
709+
concat_chunks.append(str(next(iter_params)))
710+
except StopIteration:
711+
iter_params = None
712+
713+
return concat_strings(
714+
concat_chunks, strip_string=strip_string, max_length=max_length
715+
)
716+
717+
718+
def concat_strings(
719+
chunks, strip_string=strip_string, max_length=MAX_FORMAT_PARAM_LENGTH
720+
):
692721
rv_remarks = [] # type: List[Any]
693722
rv_original_length = 0
694723
rv_length = 0
@@ -700,28 +729,25 @@ def realign_remark(remark):
700729
for i, x in enumerate(remark)
701730
]
702731

703-
for chunk in chunks[:-1]:
704-
rv.append(chunk)
705-
rv_length += len(chunk)
706-
rv_original_length += len(chunk)
707-
if not params:
708-
raise ValueError("Not enough params.")
709-
param = params.pop()
732+
for chunk in chunks:
733+
if isinstance(chunk, AnnotatedValue):
734+
# Assume it's already stripped!
735+
stripped_chunk = chunk
736+
chunk = chunk.value
737+
else:
738+
stripped_chunk = strip_string(chunk, max_length=max_length)
710739

711-
stripped_param = strip_string(param, max_length=max_length)
712-
if isinstance(stripped_param, AnnotatedValue):
740+
if isinstance(stripped_chunk, AnnotatedValue):
713741
rv_remarks.extend(
714-
realign_remark(remark) for remark in stripped_param.metadata["rem"]
742+
realign_remark(remark) for remark in stripped_chunk.metadata["rem"]
715743
)
716-
stripped_param = stripped_param.value
717-
718-
rv_original_length += len(param)
719-
rv_length += len(stripped_param)
720-
rv.append(stripped_param)
744+
stripped_chunk_value = stripped_chunk.value
745+
else:
746+
stripped_chunk_value = stripped_chunk
721747

722-
rv.append(chunks[-1])
723-
rv_length += len(chunks[-1])
724-
rv_original_length += len(chunks[-1])
748+
rv_original_length += len(chunk)
749+
rv_length += len(stripped_chunk_value) # type: ignore
750+
rv.append(stripped_chunk_value) # type: ignore
725751

726752
rv_joined = u"".join(rv)
727753
assert len(rv_joined) == rv_length

0 commit comments

Comments
 (0)