Skip to content

Commit

Permalink
wsgi: Fix non-absolute http.url (fixes open-telemetry#143).
Browse files Browse the repository at this point in the history
  • Loading branch information
Oberon00 committed Sep 18, 2019
1 parent dbb3be8 commit 327d299
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
33 changes: 27 additions & 6 deletions ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import functools
import wsgiref.util as wsgiref_util
from urllib.parse import urlparse

from opentelemetry import trace
from opentelemetry.ext.wsgi.version import __version__ # noqa
Expand Down Expand Up @@ -47,13 +48,33 @@ def _add_request_attributes(span, environ):
span.set_attribute("http.method", environ["REQUEST_METHOD"])

host = environ.get("HTTP_HOST") or environ["SERVER_NAME"]
span.set_attribute("http.host", host)
span.set_attribute("http.host", host) # NOTE: Nonstandard.

url = environ.get("REQUEST_URI") or environ.get("RAW_URI")

if url: # We got something, but is absolute?
# The simplistic ``"://" in url` is not sufficient,
# as that could be contained in the query string.
try:
urlparts = urlparse(url)
except Exception: # pylint:disable=broad-except
url = wsgiref_util.request_uri(environ)
else:
if not urlparts.netloc:
scheme = environ["wsgi.url_scheme"]
portstr = ""
if scheme == "https":
if environ.get("SERVER_PORT", "443") != "443":
portstr = ":" + environ["SERVER_PORT"]
elif environ.get("SERVER_PORT", "80") != "80":
portstr = ":" + environ["SERVER_PORT"]

url = (
scheme + "://" + (host or "localhost") + portstr + url
)
else:
url = wsgiref_util.request_uri(environ)

url = (
environ.get("REQUEST_URI")
or environ.get("RAW_URI")
or wsgiref_util.request_uri(environ, include_query=False)
)
span.set_attribute("http.url", url)

@staticmethod
Expand Down
30 changes: 30 additions & 0 deletions ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,36 @@ def test_request_attributes(self):
self.assertEqual(self.span.set_attribute.call_count, len(expected))
self.span.set_attribute.assert_has_calls(expected, any_order=True)

def test_request_attributes_with_partial_raw_uri(self):
self.environ["RAW_URI"] = "/#top"
OpenTelemetryMiddleware._add_request_attributes( # noqa pylint: disable=protected-access
self.span, self.environ
)
self.span.set_attribute.assert_any_call(
"http.url", "http://127.0.0.1/#top"
)

def test_request_attributes_with_partial_raw_uri_and_nonstandard_port(
self
):
self.environ["RAW_URI"] = "/#top"
self.environ["SERVER_PORT"] = "8080"
OpenTelemetryMiddleware._add_request_attributes( # noqa pylint: disable=protected-access
self.span, self.environ
)
self.span.set_attribute.assert_any_call(
"http.url", "http://127.0.0.1:8080/#top"
)

def test_request_attributes_with_full_request_uri(self):
self.environ["REQUEST_URI"] = "http://foobar.com:8080/?foo=bar#top"
OpenTelemetryMiddleware._add_request_attributes( # noqa pylint: disable=protected-access
self.span, self.environ
)
self.span.set_attribute.assert_any_call(
"http.url", "http://foobar.com:8080/?foo=bar#top"
)

def test_response_attributes(self):
OpenTelemetryMiddleware._add_response_attributes( # noqa pylint: disable=protected-access
self.span, "404 Not Found"
Expand Down

0 comments on commit 327d299

Please sign in to comment.