Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[httplib, requests] Sanitize urls in span metadata #688

Merged
merged 9 commits into from Nov 6, 2018
4 changes: 3 additions & 1 deletion ddtrace/compat.py
@@ -1,5 +1,5 @@
import sys
import platform
import sys

PYTHON_VERSION_INFO = sys.version_info
PY2 = sys.version_info[0] == 2
Expand All @@ -12,6 +12,7 @@

if PY2:
from urllib import urlencode
from urlparse import urlparse
import httplib
stringify = unicode
from Queue import Queue
Expand All @@ -22,6 +23,7 @@
else:
from queue import Queue
from urllib.parse import urlencode
from urllib.parse import urlparse
majorgreys marked this conversation as resolved.
Show resolved Hide resolved
import http.client as httplib
from io import StringIO

Expand Down
8 changes: 6 additions & 2 deletions ddtrace/contrib/httplib/patch.py
Expand Up @@ -5,12 +5,11 @@
import wrapt

# Project
from ...compat import httplib, PY2
from ...compat import PY2, httplib, urlparse
from ...ext import http as ext_http
from ...pin import Pin
from ...utils.wrappers import unwrap as _u


span_name = 'httplib.request' if PY2 else 'http.client.request'

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -60,9 +59,14 @@ def _wrap_putrequest(func, instance, args, kwargs):
method, path = args[:2]
scheme = 'https' if isinstance(instance, httplib.HTTPSConnection) else 'http'
port = ':{port}'.format(port=instance.port)

# strip path of non-path data
path = urlparse(path).path

if (scheme == 'http' and instance.port == 80) or (scheme == 'https' and instance.port == 443):
port = ''
url = '{scheme}://{host}{port}{path}'.format(scheme=scheme, host=instance.host, port=port, path=path)
majorgreys marked this conversation as resolved.
Show resolved Hide resolved

span.set_tag(ext_http.URL, url)
span.set_tag(ext_http.METHOD, method)
except Exception:
Expand Down