Skip to content

Commit 441a0df

Browse files
author
Ryo Kather
committed
Implemented check on aiohttp and wrapped conversions in try catch
1 parent 2e8e14f commit 441a0df

File tree

8 files changed

+53
-23
lines changed

8 files changed

+53
-23
lines changed

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

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

6970
import aiohttp
7071
import wrapt
@@ -171,13 +172,16 @@ async def on_request_start(
171172
)
172173

173174
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
179+
174180
attributes = {
175181
SpanAttributes.HTTP_METHOD: http_method,
176-
SpanAttributes.HTTP_URL: trace_config_ctx.url_filter(
177-
params.url
178-
)
182+
SpanAttributes.HTTP_URL: trace_config_ctx.url_filter(url)
179183
if callable(trace_config_ctx.url_filter)
180-
else str(params.url),
184+
else str(url),
181185
}
182186
for key, value in attributes.items():
183187
trace_config_ctx.span.set_attribute(key, value)

instrumentation/opentelemetry-instrumentation-aiohttp-client/tests/test_aiohttp_client_integration.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,34 +263,37 @@ async def do_request(url):
263263
]
264264
)
265265
self.memory_exporter.clear()
266+
267+
def test_credentials(self):
268+
trace_configs = [aiohttp_client.create_trace_config()]
266269

267-
def test_timeout(self):
268-
async def request_handler(request):
269-
await asyncio.sleep(1)
270-
assert "traceparent" in request.headers
271-
return aiohttp.web.Response()
270+
url = "http://username:[email protected]/"
271+
with self.subTest(url=url):
272272

273-
host, port = self._http_request(
274-
trace_config=aiohttp_client.create_trace_config(),
275-
url="/test_timeout",
276-
request_handler=request_handler,
277-
timeout=aiohttp.ClientTimeout(sock_read=0.01),
278-
)
273+
async def do_request(url):
274+
async with aiohttp.ClientSession(
275+
trace_configs=trace_configs,
276+
) as session:
277+
async with session.get(url):
278+
pass
279+
280+
loop = asyncio.get_event_loop()
281+
loop.run_until_complete(do_request(url))
279282

280283
self.assert_spans(
281284
[
282285
(
283286
"HTTP GET",
284-
(StatusCode.ERROR, None),
287+
(StatusCode.UNSET, None),
285288
{
286289
SpanAttributes.HTTP_METHOD: "GET",
287-
SpanAttributes.HTTP_URL: "http://{}:{}/test_timeout".format(
288-
host, port
289-
),
290+
SpanAttributes.HTTP_URL: "http://httpbin.org/",
291+
SpanAttributes.HTTP_STATUS_CODE: int(HTTPStatus.OK),
290292
},
291293
)
292294
]
293295
)
296+
self.memory_exporter.clear()
294297

295298
def test_too_many_redirects(self):
296299
async def request_handler(request):

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

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

4041
from requests.models import Response
4142
from requests.sessions import Session
@@ -124,6 +125,11 @@ def _instrumented_requests_call(
124125
if not span_name or not isinstance(span_name, str):
125126
span_name = get_default_span_name(method)
126127

128+
try:
129+
url = str(yarl.URL(url).with_user(None))
130+
except ValueError: # invalid url was passed
131+
pass
132+
127133
labels = {}
128134
labels[SpanAttributes.HTTP_METHOD] = method
129135
labels[SpanAttributes.HTTP_URL] = url

instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ def test_invalid_url(self):
357357
)
358358
self.assertEqual(span.status.status_code, StatusCode.ERROR)
359359

360+
def test_url_credentials(self):
361+
new_url = "http://username:[email protected]/status/200"
362+
self.perform_request(new_url)
363+
span = self.assert_span()
364+
365+
self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], self.URL)
366+
360367
def test_if_headers_equals_none(self):
361368
result = requests.get(self.URL, headers=None)
362369
self.assertEqual(result.text, "Hello!")

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,14 @@ def _instrumented_open_call(
143143
if not span_name or not isinstance(span_name, str):
144144
span_name = get_default_span_name(method)
145145

146+
try:
147+
url = str(yarl.URL(url).with_user(None))
148+
except ValueError: # invalid url was passed
149+
pass
150+
146151
labels = {
147152
SpanAttributes.HTTP_METHOD: method,
148-
SpanAttributes.HTTP_URL: str(yarl.URL(url).with_user(None)),
153+
SpanAttributes.HTTP_URL: url
149154
}
150155

151156
with tracer.start_as_current_span(
@@ -154,7 +159,7 @@ def _instrumented_open_call(
154159
exception = None
155160
if span.is_recording():
156161
span.set_attribute(SpanAttributes.HTTP_METHOD, method)
157-
span.set_attribute(SpanAttributes.HTTP_URL, str(yarl.URL(url).with_user(None)))
162+
span.set_attribute(SpanAttributes.HTTP_URL, url)
158163

159164
headers = get_or_create_headers()
160165
inject(headers)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ def test_credentials_url(self):
325325
self.perform_request(url)
326326

327327
span = self.assert_span()
328-
print(span.attributes)
329328
self.assertEqual(span.attributes[SpanAttributes.HTTP_URL], self.URL)
330329

331330
class TestRequestsIntegration(RequestsIntegrationTestBase, TestBase):

instrumentation/opentelemetry-instrumentation-urllib3/tests/test_urllib3_integration.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,9 @@ def url_filter(url):
287287

288288
response = self.perform_request(self.HTTP_URL + "?e=mcc")
289289
self.assert_success_span(response, self.HTTP_URL)
290+
291+
def test_url_credential_removal(self):
292+
url = "http://username:[email protected]/status/200"
293+
294+
response = self.perform_request(url)
295+
self.assert_success_span(response, self.HTTP_URL)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ commands_pre =
273273

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

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

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

0 commit comments

Comments
 (0)