Skip to content

Commit dbc0578

Browse files
committed
elasticapm/utils: match the specs better in build_name_with_http_method_prefix
In case the route name is not available we should use `unknown route` as route name. While at it handle the case where the request does not have a method attribute. Mostly theorical since the current users, flask and django, requests have it. Specs: https://github.com/elastic/apm/blob/main/specs/agents/tracing-instrumentation-http.md
1 parent 071203e commit dbc0578

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

elasticapm/utils/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ def get_name_from_func(func: FunctionType) -> str:
9494

9595

9696
def build_name_with_http_method_prefix(name, request):
97-
return " ".join((request.method, name)) if name else name
97+
name = name or "unknown route"
98+
return "{} {}".format(request.method, name) if hasattr(request, "method") else name
9899

99100

100101
def is_master_process() -> bool:

tests/utils/tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import elasticapm.utils
3838
from elasticapm.conf import constants
3939
from elasticapm.utils import (
40+
build_name_with_http_method_prefix,
4041
get_name_from_func,
4142
get_url_dict,
4243
getfqdn,
@@ -263,3 +264,19 @@ def test_getfqdn(invalidate_fqdn_cache):
263264
def test_getfqdn_caches(invalidate_fqdn_cache):
264265
elasticapm.utils.fqdn = "foo"
265266
assert getfqdn() == "foo"
267+
268+
269+
class Request:
270+
method: "str" = "GET"
271+
272+
273+
@pytest.mark.parametrize(
274+
"http_request,name,expected",
275+
[
276+
("", None, "unknown route"),
277+
(Request, None, "GET unknown route"),
278+
(Request, "/foo", "GET /foo"),
279+
],
280+
)
281+
def test_build_name_with_http_method_prefix(http_request, name, expected):
282+
assert build_name_with_http_method_prefix(name, http_request) == expected

0 commit comments

Comments
 (0)