Skip to content

Commit 9aba340

Browse files
author
Ryo Kather
committed
Moved common function into utils
Readded removed dependency Added dependency for docker test Fixed linting errors Placed yarl dependency and moved common function into utils
1 parent c8be726 commit 9aba340

File tree

12 files changed

+52
-54
lines changed

12 files changed

+52
-54
lines changed

dev-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ readme-renderer~=24.0
1313
grpcio-tools==1.29.0
1414
mypy-protobuf>=1.23
1515
protobuf>=3.13.0
16+
yarl~=1.6

instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def strip_query_params(url: yarl.URL) -> str:
6565
import types
6666
import typing
6767
from typing import Collection
68-
import yarl
6968

7069
import aiohttp
7170
import wrapt
@@ -77,6 +76,7 @@ def strip_query_params(url: yarl.URL) -> str:
7776
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
7877
from opentelemetry.instrumentation.utils import (
7978
http_status_to_status_code,
79+
remove_url_credentials,
8080
unwrap,
8181
)
8282
from opentelemetry.propagate import inject
@@ -172,10 +172,7 @@ async def on_request_start(
172172
)
173173

174174
if trace_config_ctx.span.is_recording():
175-
try:
176-
url = yarl.URL(params.url).with_user(None)
177-
except ValueError: # invalid url was passed
178-
pass
175+
url = remove_url_credentials(params.url)
179176

180177
attributes = {
181178
SpanAttributes.HTTP_METHOD: http_method,

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@
2020

2121
import typing
2222
import urllib
23-
import yarl
2423
from functools import wraps
2524
from typing import Tuple
2625

2726
from asgiref.compatibility import guarantee_single_callable
2827

2928
from opentelemetry import context, trace
3029
from opentelemetry.instrumentation.asgi.version import __version__ # noqa
31-
from opentelemetry.instrumentation.utils import http_status_to_status_code
30+
from opentelemetry.instrumentation.utils import (
31+
http_status_to_status_code,
32+
remove_url_credentials,
33+
)
3234
from opentelemetry.propagate import extract
3335
from opentelemetry.propagators.textmap import Getter
3436
from opentelemetry.semconv.trace import SpanAttributes
@@ -81,18 +83,13 @@ def collect_request_attributes(scope):
8183
query_string = query_string.decode("utf8")
8284
http_url = http_url + ("?" + urllib.parse.unquote(query_string))
8385

84-
try:
85-
http_url = str(yarl.URL(http_url).with_user(None))
86-
except ValueError: # invalid url was passed
87-
pass
88-
8986
result = {
9087
SpanAttributes.HTTP_SCHEME: scope.get("scheme"),
9188
SpanAttributes.HTTP_HOST: server_host,
9289
SpanAttributes.NET_HOST_PORT: port,
9390
SpanAttributes.HTTP_FLAVOR: scope.get("http_version"),
9491
SpanAttributes.HTTP_TARGET: scope.get("path"),
95-
SpanAttributes.HTTP_URL: http_url,
92+
SpanAttributes.HTTP_URL: str(remove_url_credentials(http_url)),
9693
}
9794
http_method = scope.get("method")
9895
if http_method:

instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def test_response_attributes(self):
429429
def test_response_attributes_invalid_status_code(self):
430430
otel_asgi.set_status_code(self.span, "Invalid Status Code")
431431
self.assertEqual(self.span.set_status.call_count, 1)
432-
432+
433433
def test_credential_removal(self):
434434
self.scope["server"] = ("username:[email protected]", 80)
435435
self.scope["path"] = "/status/200"

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import functools
3737
import types
3838
from typing import Collection
39-
import yarl
4039

4140
from requests.models import Response
4241
from requests.sessions import Session
@@ -46,7 +45,10 @@
4645
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
4746
from opentelemetry.instrumentation.requests.package import _instruments
4847
from opentelemetry.instrumentation.requests.version import __version__
49-
from opentelemetry.instrumentation.utils import http_status_to_status_code
48+
from opentelemetry.instrumentation.utils import (
49+
http_status_to_status_code,
50+
remove_url_credentials,
51+
)
5052
from opentelemetry.propagate import inject
5153
from opentelemetry.semconv.trace import SpanAttributes
5254
from opentelemetry.trace import SpanKind, get_tracer
@@ -125,10 +127,7 @@ def _instrumented_requests_call(
125127
if not span_name or not isinstance(span_name, str):
126128
span_name = get_default_span_name(method)
127129

128-
try:
129-
url = str(yarl.URL(url).with_user(None))
130-
except ValueError: # invalid url was passed
131-
pass
130+
url = str(remove_url_credentials(url))
132131

133132
labels = {}
134133
labels[SpanAttributes.HTTP_METHOD] = method

instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/client.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414

1515
import functools
1616

17-
import yarl
18-
1917
from tornado.httpclient import HTTPError, HTTPRequest
2018

2119
from opentelemetry import trace
22-
from opentelemetry.instrumentation.utils import http_status_to_status_code
20+
from opentelemetry.instrumentation.utils import (
21+
http_status_to_status_code,
22+
remove_url_credentials,
23+
)
2324
from opentelemetry.propagate import inject
2425
from opentelemetry.semconv.trace import SpanAttributes
2526
from opentelemetry.trace.status import Status
@@ -62,13 +63,8 @@ def fetch_async(tracer, request_hook, response_hook, func, _, args, kwargs):
6263
request_hook(span, request)
6364

6465
if span.is_recording():
65-
try:
66-
url = str(yarl.URL(request.url).with_user(None))
67-
except ValueError: # invalid url was passed
68-
pass
69-
7066
attributes = {
71-
SpanAttributes.HTTP_URL: url,
67+
SpanAttributes.HTTP_URL: str(remove_url_credentials(request.url)),
7268
SpanAttributes.HTTP_METHOD: request.method,
7369
}
7470
for key, value in attributes.items():

instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ def test_response_headers(self):
456456
set_global_response_propagator(orig)
457457

458458
def test_credential_removal(self):
459-
response = self.fetch("http://username:[email protected]/status/200")
459+
response = self.fetch(
460+
"http://username:[email protected]/status/200"
461+
)
460462
self.assertEqual(response.code, 200)
461463

462464
spans = self.sorted_spans(self.memory_exporter.get_finished_spans())

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import functools
4040
import types
4141
from typing import Collection
42-
import yarl
4342
from urllib.request import ( # pylint: disable=no-name-in-module,import-error
4443
OpenerDirector,
4544
Request,
@@ -49,7 +48,10 @@
4948
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
5049
from opentelemetry.instrumentation.urllib.package import _instruments
5150
from opentelemetry.instrumentation.urllib.version import __version__
52-
from opentelemetry.instrumentation.utils import http_status_to_status_code
51+
from opentelemetry.instrumentation.utils import (
52+
http_status_to_status_code,
53+
remove_url_credentials,
54+
)
5355
from opentelemetry.propagate import inject
5456
from opentelemetry.semconv.trace import SpanAttributes
5557
from opentelemetry.trace import SpanKind, get_tracer
@@ -143,14 +145,11 @@ def _instrumented_open_call(
143145
if not span_name or not isinstance(span_name, str):
144146
span_name = get_default_span_name(method)
145147

146-
try:
147-
url = str(yarl.URL(url).with_user(None))
148-
except ValueError: # invalid url was passed
149-
pass
148+
url = str(remove_url_credentials(url))
150149

151150
labels = {
152151
SpanAttributes.HTTP_METHOD: method,
153-
SpanAttributes.HTTP_URL: url
152+
SpanAttributes.HTTP_URL: url,
154153
}
155154

156155
with tracer.start_as_current_span(

instrumentation/opentelemetry-instrumentation-urllib/tests/test_urllib_integration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ def test_credential_removal(self):
327327
span = self.assert_span()
328328
self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], self.URL)
329329

330+
330331
class TestRequestsIntegration(RequestsIntegrationTestBase, TestBase):
331332
@staticmethod
332333
def perform_request(url: str, opener: OpenerDirector = None):

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ def hello():
5757
import functools
5858
import typing
5959
import wsgiref.util as wsgiref_util
60-
import yarl
6160

6261
from opentelemetry import context, trace
63-
from opentelemetry.instrumentation.utils import http_status_to_status_code
62+
from opentelemetry.instrumentation.utils import (
63+
http_status_to_status_code,
64+
remove_url_credentials,
65+
)
6466
from opentelemetry.instrumentation.wsgi.version import __version__
6567
from opentelemetry.propagate import extract
6668
from opentelemetry.propagators.textmap import Getter
@@ -129,12 +131,9 @@ def collect_request_attributes(environ):
129131
if target is not None:
130132
result[SpanAttributes.HTTP_TARGET] = target
131133
else:
132-
try:
133-
url = str(yarl.URL(wsgiref_util.request_uri(environ)).with_user(None))
134-
except ValueError: # invalid url was passed
135-
pass
136-
137-
result[SpanAttributes.HTTP_URL] = url
134+
result[SpanAttributes.HTTP_URL] = str(
135+
remove_url_credentials(wsgiref_util.request_uri(environ))
136+
)
138137

139138
remote_addr = environ.get("REMOTE_ADDR")
140139
if remote_addr:

opentelemetry-instrumentation/src/opentelemetry/instrumentation/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
from wrapt import ObjectProxy
1818

19+
from yarl import URL
20+
1921
from opentelemetry.trace import StatusCode
2022

2123

@@ -60,3 +62,12 @@ def unwrap(obj, attr: str):
6062
func = getattr(obj, attr, None)
6163
if func and isinstance(func, ObjectProxy) and hasattr(func, "__wrapped__"):
6264
setattr(obj, attr, func.__wrapped__)
65+
66+
67+
def remove_url_credentials(url: str) -> URL:
68+
"""Given a string url, attempt to remove the username and password"""
69+
try:
70+
url = URL(url).with_user(None)
71+
except ValueError: # invalid url was passed
72+
pass
73+
return url

tox.ini

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ commands_pre =
233233
grpc: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-grpc[test]
234234

235235
falcon,flask,django,pyramid,tornado,starlette,fastapi: pip install {toxinidir}/util/opentelemetry-util-http[test]
236-
wsgi,falcon,flask,django,pyramid: pip install yarl {toxinidir}/instrumentation/opentelemetry-instrumentation-wsgi[test]
237-
asgi,starlette,fastapi: pip install yarl {toxinidir}/instrumentation/opentelemetry-instrumentation-asgi[test]
236+
wsgi,falcon,flask,django,pyramid: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-wsgi[test]
237+
asgi,starlette,fastapi: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-asgi[test]
238238

239239
asyncpg: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-asyncpg[test]
240240

@@ -245,7 +245,7 @@ commands_pre =
245245

246246
flask: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-flask[test]
247247

248-
urllib: pip install yarl {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib[test]
248+
urllib: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib[test]
249249

250250
urllib3: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-urllib3[test]
251251

@@ -273,11 +273,11 @@ commands_pre =
273273

274274
redis: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-redis[test]
275275

276-
requests: pip install yarl {toxinidir}/instrumentation/opentelemetry-instrumentation-requests[test]
276+
requests: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-requests[test]
277277

278278
starlette: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-starlette[test]
279279

280-
tornado: pip install yarl {toxinidir}/instrumentation/opentelemetry-instrumentation-tornado[test]
280+
tornado: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-tornado[test]
281281

282282
jinja2: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-jinja2[test]
283283

@@ -287,10 +287,6 @@ commands_pre =
287287

288288
aiopg: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-dbapi pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-aiopg[test]
289289

290-
asgi: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-asgi[test]
291-
292-
wsgi: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-wsgi[test]
293-
294290
datadog: pip install flaky {toxinidir}/exporter/opentelemetry-exporter-datadog[test]
295291

296292
sklearn: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-sklearn[test]

0 commit comments

Comments
 (0)