Skip to content

Commit

Permalink
Merge branch 'main' into first-contrib-opentelemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl authored Jun 13, 2023
2 parents 6a1e546 + bcf770d commit 62a6f5e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ instruments = [
]
test = [
"opentelemetry-instrumentation-aiohttp-client[instruments]",
"http-server-mock"
]

[project.entry-points.opentelemetry_instrumentor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import aiohttp
import aiohttp.test_utils
import yarl
from http_server_mock import HttpServerMock
from pkg_resources import iter_entry_points

from opentelemetry import context
Expand Down Expand Up @@ -313,18 +314,26 @@ async def request_handler(request):
def test_credential_removal(self):
trace_configs = [aiohttp_client.create_trace_config()]

url = "http://username:[email protected]/status/200"
with self.subTest(url=url):
app = HttpServerMock("test_credential_removal")

async def do_request(url):
async with aiohttp.ClientSession(
trace_configs=trace_configs,
) as session:
async with session.get(url):
pass
@app.route("/status/200")
def index():
return "hello"

loop = asyncio.get_event_loop()
loop.run_until_complete(do_request(url))
url = "http://username:password@localhost:5000/status/200"

with app.run("localhost", 5000):
with self.subTest(url=url):

async def do_request(url):
async with aiohttp.ClientSession(
trace_configs=trace_configs,
) as session:
async with session.get(url):
pass

loop = asyncio.get_event_loop()
loop.run_until_complete(do_request(url))

self.assert_spans(
[
Expand All @@ -333,7 +342,9 @@ async def do_request(url):
(StatusCode.UNSET, None),
{
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_URL: "http://httpbin.org/status/200",
SpanAttributes.HTTP_URL: (
"http://localhost:5000/status/200"
),
SpanAttributes.HTTP_STATUS_CODE: int(HTTPStatus.OK),
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
import pyramid.httpexceptions as exc
from pyramid.response import Response
from werkzeug.test import Client
from werkzeug.wrappers import BaseResponse

# opentelemetry-instrumentation-pyramid uses werkzeug==0.16.1 which has
# werkzeug.wrappers.BaseResponse. This is not the case for newer versions of
# werkzeug like the one lint uses.
from werkzeug.wrappers import BaseResponse # pylint: disable=no-name-in-module


class InstrumentationTest:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ instruments = [
test = [
"opentelemetry-instrumentation-tornado[instruments]",
"opentelemetry-test-utils == 0.40b0.dev",
"http-server-mock"
]

[project.entry-points.opentelemetry_instrumentor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from unittest.mock import Mock, patch

from http_server_mock import HttpServerMock
from tornado.testing import AsyncHTTPTestCase

from opentelemetry import trace
Expand Down Expand Up @@ -494,32 +495,35 @@ def test_response_headers(self):
self.memory_exporter.clear()
set_global_response_propagator(orig)

# todo(srikanthccv): fix this test
# this test is making request to real httpbin.org/status/200 which
# is not a good idea as it can fail due to availability of the
# service.
# def test_credential_removal(self):
# response = self.fetch(
# "http://username:[email protected]/status/200"
# )
# self.assertEqual(response.code, 200)

# spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
# self.assertEqual(len(spans), 1)
# client = spans[0]

# self.assertEqual(client.name, "GET")
# self.assertEqual(client.kind, SpanKind.CLIENT)
# self.assertSpanHasAttributes(
# client,
# {
# SpanAttributes.HTTP_URL: "http://httpbin.org/status/200",
# SpanAttributes.HTTP_METHOD: "GET",
# SpanAttributes.HTTP_STATUS_CODE: 200,
# },
# )

# self.memory_exporter.clear()
def test_credential_removal(self):
app = HttpServerMock("test_credential_removal")

@app.route("/status/200")
def index():
return "hello"

with app.run("localhost", 5000):
response = self.fetch(
"http://username:password@localhost:5000/status/200"
)
self.assertEqual(response.code, 200)

spans = self.sorted_spans(self.memory_exporter.get_finished_spans())
self.assertEqual(len(spans), 1)
client = spans[0]

self.assertEqual(client.name, "GET")
self.assertEqual(client.kind, SpanKind.CLIENT)
self.assertSpanHasAttributes(
client,
{
SpanAttributes.HTTP_URL: "http://localhost:5000/status/200",
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_STATUS_CODE: 200,
},
)

self.memory_exporter.clear()


class TestTornadoInstrumentationWithXHeaders(TornadoTest):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ def fn_exception():

span = spans[0]

assert span.status.is_ok is True
assert span.status.status_code == StatusCode.UNSET
assert span.status.is_ok is False
assert span.status.status_code == StatusCode.ERROR
assert span.name == "run/test_celery_functional.fn_exception"
assert span.attributes.get("celery.action") == "run"
assert span.attributes.get("celery.state") == "FAILURE"
Expand Down Expand Up @@ -443,8 +443,8 @@ def run(self):

span = spans[0]

assert span.status.is_ok is True
assert span.status.status_code == StatusCode.UNSET
assert span.status.is_ok is False
assert span.status.status_code == StatusCode.ERROR
assert span.name == "run/test_celery_functional.BaseTask"
assert span.attributes.get("celery.action") == "run"
assert span.attributes.get("celery.state") == "FAILURE"
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ envlist =

; opentelemetry-instrumentation-urllib3
py3{7,8,9,10,11}-test-instrumentation-urllib3
pypy3-test-instrumentation-urllib3
;pypy3-test-instrumentation-urllib3

; opentelemetry-instrumentation-requests
py3{7,8,9,10,11}-test-instrumentation-requests
Expand Down

0 comments on commit 62a6f5e

Please sign in to comment.