Skip to content

Commit c161fd2

Browse files
authored
Cast transaction name to str (#1389)
* Cast transaction name to str * CHANGELOG * Parametrize transaction_name test
1 parent ae9d4e5 commit c161fd2

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ endif::[]
4646
* Update the `User-Agent` header to the new https://github.com/elastic/apm/pull/514[spec] {pull}1378[#1378]
4747
* Improve status_code handling in AWS Lambda integration {pull}1382[#1382]
4848
* Fix `aiohttp` exception handling to allow for non-500 responses including `HTTPOk` {pull}1384[#1384]
49+
* Force transaction names to strings {pull}1389[#1389]
4950
5051
[float]
5152
===== Other

elasticapm/traces.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ def end_transaction(self, result=None, transaction_name=None, duration=None):
793793
transaction = execution_context.get_transaction(clear=True)
794794
if transaction:
795795
if transaction.name is None:
796-
transaction.name = transaction_name if transaction_name is not None else ""
796+
transaction.name = str(transaction_name) if transaction_name is not None else ""
797797
transaction.end(duration=duration)
798798
if self._should_ignore(transaction.name):
799799
return
@@ -925,7 +925,7 @@ def label(**labels):
925925
transaction.label(**labels)
926926

927927

928-
def set_transaction_name(name, override=True):
928+
def set_transaction_name(name: str, override: bool = True) -> None:
929929
"""
930930
Sets the name of the transaction
931931
@@ -937,7 +937,7 @@ def set_transaction_name(name, override=True):
937937
if not transaction:
938938
return
939939
if transaction.name is None or override:
940-
transaction.name = name
940+
transaction.name = str(name)
941941

942942

943943
def set_transaction_result(result, override=True):

tests/instrumentation/transactions_store_tests.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,19 +346,26 @@ def test_dedot_is_not_run_when_unsampled(elasticapm_client):
346346
assert "context" not in unsampled_transaction
347347

348348

349-
def test_set_transaction_name(elasticapm_client):
349+
@pytest.mark.parametrize(
350+
"name1,name2,expected1,expected2",
351+
[
352+
("test_name", "another_name", "test_name", "another_name"),
353+
(["test_name"], {"another_name": "foo"}, "['test_name']", "{'another_name': 'foo'}"),
354+
],
355+
)
356+
def test_set_transaction_name(elasticapm_client, name1, name2, expected1, expected2):
350357
elasticapm_client.begin_transaction("test")
351-
elasticapm_client.end_transaction("test_name", 200)
358+
elasticapm_client.end_transaction(name1, 200)
352359

353360
elasticapm_client.begin_transaction("test")
354361

355-
elasticapm.set_transaction_name("another_name")
362+
elasticapm.set_transaction_name(name2)
356363

357-
elasticapm_client.end_transaction("test_name", 200)
364+
elasticapm_client.end_transaction(name1, 200)
358365

359366
transactions = elasticapm_client.events[TRANSACTION]
360-
assert transactions[0]["name"] == "test_name"
361-
assert transactions[1]["name"] == "another_name"
367+
assert transactions[0]["name"] == expected1
368+
assert transactions[1]["name"] == expected2
362369

363370

364371
def test_set_transaction_custom_data(elasticapm_client):

0 commit comments

Comments
 (0)