Skip to content

Commit 6b1e2de

Browse files
committed
don't try to dedot unsampled transactions (#362)
this avoids fireworks when trying to iterate a callable closes #362
1 parent 6f73e16 commit 6b1e2de

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

elasticapm/traces.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,10 @@ def set_context(data, key="custom"):
319319
data = data()
320320

321321
# remove invalid characters from key names
322-
for k in list(data.keys()):
323-
if TAG_RE.search(k):
324-
data[TAG_RE.sub("_", k)] = data.pop(k)
322+
if not callable(data): # if transaction wasn't sampled, data is still a callable here and can be ignored
323+
for k in list(data.keys()):
324+
if TAG_RE.search(k):
325+
data[TAG_RE.sub("_", k)] = data.pop(k)
325326

326327
if key in transaction.context:
327328
transaction.context[key].update(data)

tests/instrumentation/transactions_store_tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,17 @@ def test_tags_dedot(elasticapm_client):
233233
assert transactions[0]["context"]["tags"] == {"d_o_t": "dot", "s_t_a_r": "star", "q_u_o_t_e": "quote"}
234234

235235

236+
def test_dedot_is_not_run_when_unsampled(elasticapm_client):
237+
for sampled in (True, False):
238+
t = elasticapm_client.begin_transaction("test")
239+
t.is_sampled = sampled
240+
elasticapm.set_context(lambda: {"a.b": "b"})
241+
elasticapm_client.end_transaction("x", "OK")
242+
sampled_transaction, unsampled_transaction = transactions = elasticapm_client.events[TRANSACTION]
243+
assert "a_b" in sampled_transaction["context"]["custom"]
244+
assert "context" not in unsampled_transaction
245+
246+
236247
def test_set_transaction_name(elasticapm_client):
237248
elasticapm_client.begin_transaction("test")
238249
elasticapm_client.end_transaction("test_name", 200)

0 commit comments

Comments
 (0)