Skip to content

Commit 3e34d36

Browse files
committed
Set http.server_name based on Host header
1 parent 775f58b commit 3e34d36

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Diff for: ext/opentelemetry-ext-asgi/src/opentelemetry/ext/asgi/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,18 @@ def collect_request_attributes(scope):
8989
result = {
9090
"component": scope.get("type"),
9191
"http.method": scope.get("method"),
92-
"http.server_name": server[0],
9392
"http.scheme": scope.get("scheme"),
9493
"http.host": server_host,
9594
"host.port": port,
9695
"http.flavor": scope.get("http_version"),
9796
"http.target": scope.get("path"),
9897
"http.url": http_url,
9998
}
99+
http_host_value = ",".join(get_header_from_scope(scope, "host"))
100+
if http_host_value:
101+
result['http.server_name'] = http_host_value
100102

101-
if "client" in scope:
103+
if "client" in scope and scope["client"] is not None:
102104
result["net.peer.ip"] = scope.get("client")[0]
103105
result["net.peer.port"] = scope.get("client")[1]
104106

Diff for: ext/opentelemetry-ext-asgi/tests/test_asgi_middleware.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def validate_outputs(self, outputs, error=None, modifiers=None):
114114
"attributes": {
115115
"component": "http",
116116
"http.method": "GET",
117-
"http.server_name": "127.0.0.1",
118117
"http.scheme": "http",
119118
"host.port": 80,
120119
"http.host": "127.0.0.1",
@@ -174,7 +173,6 @@ def test_behavior_with_scope_server_as_none(self):
174173
"""Test that middleware is ok when server is none in scope."""
175174
def update_expected_server(expected):
176175
expected[3]['attributes'].update({
177-
'http.server_name': '0.0.0.0',
178176
'http.host': '0.0.0.0',
179177
'host.port': 80,
180178
'http.url': 'http://0.0.0.0/'
@@ -187,6 +185,21 @@ def update_expected_server(expected):
187185
outputs = self.get_all_output()
188186
self.validate_outputs(outputs, modifiers=[update_expected_server])
189187

188+
def test_host_header(self):
189+
"""Test that middleware is ok when server is none in scope."""
190+
hostname = b"server_name_1"
191+
def update_expected_server(expected):
192+
expected[3]['attributes'].update({
193+
'http.server_name': hostname.decode('utf8')
194+
})
195+
return expected
196+
self.scope["headers"].append([b'host', hostname])
197+
app = otel_asgi.OpenTelemetryMiddleware(simple_asgi)
198+
self.seed_app(app)
199+
self.send_default_request()
200+
outputs = self.get_all_output()
201+
self.validate_outputs(outputs, modifiers=[update_expected_server])
202+
190203

191204

192205
class TestAsgiAttributes(unittest.TestCase):
@@ -209,7 +222,6 @@ def test_request_attributes(self):
209222
"http.url": "http://127.0.0.1/?foo=bar",
210223
"host.port": 80,
211224
"http.scheme": "http",
212-
"http.server_name": "127.0.0.1",
213225
"http.flavor": "1.0",
214226
"net.peer.ip": "127.0.0.1",
215227
"net.peer.port": 32767,

0 commit comments

Comments
 (0)