Skip to content

Commit 57ca703

Browse files
authored
Add HTTP user-agent in WSGI instrumentation (open-telemetry#964)
1 parent 3143a4b commit 57ca703

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ def collect_request_attributes(environ):
117117
if remote_host and remote_host != remote_addr:
118118
result["net.peer.name"] = remote_host
119119

120+
user_agent = environ.get("HTTP_USER_AGENT")
121+
if user_agent is not None and len(user_agent) > 0:
122+
result["http.user_agent"] = user_agent
123+
120124
setifnotnone(result, "net.peer.port", environ.get("REMOTE_PORT"))
121125
flavor = environ.get("SERVER_PROTOCOL", "")
122126
if flavor.upper().startswith(_HTTP_VERSION_PREFIX):

instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py

+8
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@ def test_request_attributes_with_full_request_uri(self):
313313
expected.items(),
314314
)
315315

316+
def test_http_user_agent_attribute(self):
317+
self.environ["HTTP_USER_AGENT"] = "test-useragent"
318+
expected = {"http.user_agent": "test-useragent"}
319+
self.assertGreaterEqual(
320+
otel_wsgi.collect_request_attributes(self.environ).items(),
321+
expected.items(),
322+
)
323+
316324
def test_response_attributes(self):
317325
otel_wsgi.add_response_attributes(self.span, "404 Not Found", {})
318326
expected = (

0 commit comments

Comments
 (0)