Skip to content

Commit f5a12f8

Browse files
authored
tornado: handle behaviour change in request.headers protocol (#2512)
In 6.5.3 tornado made the __in__ protocol for the request headers case sensitive. Problem is that the code extracting the traceparent header name is using a lowercase string for the header name. So start normalizing all the headers keys to lowercase so that we are able to get the traceparent again.
1 parent bbaa3bb commit f5a12f8

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

elasticapm/instrumentation/packages/tornado.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ async def call(self, module, method, wrapped, instance, args, kwargs):
7171
client = instance.application.elasticapm_client
7272
should_ignore = client.should_ignore_url(request.path)
7373
if not should_ignore:
74-
trace_parent = TraceParent.from_headers(request.headers)
74+
# In tornado 6.5.3 the __in__ protocol for the headers is case-sensitive so we need to normalize them
75+
normalized_headers = {k.lower(): v for k, v in request.headers.items()}
76+
trace_parent = TraceParent.from_headers(normalized_headers)
7577
client.begin_transaction("request", trace_parent=trace_parent)
7678
elasticapm.set_context(
7779
lambda: get_data_from_request(instance, request, client.config, constants.TRANSACTION), "request"

tests/contrib/asyncio/tornado/tornado_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ async def test_traceparent_handling(app, base_url, http_client):
177177

178178
assert transaction["trace_id"] == "0af7651916cd43dd8448eb211c80319c"
179179
assert transaction["parent_id"] == "b7ad6b7169203331"
180-
assert "foo=bar,bar=baz,baz=bazzinga" == wrapped_from_string.call_args[0][0]["TraceState"]
180+
assert "foo=bar,bar=baz,baz=bazzinga" == wrapped_from_string.call_args[0][0]["tracestate"]
181181

182182

183183
@pytest.mark.gen_test

0 commit comments

Comments
 (0)