Skip to content

Commit

Permalink
Merge branch 'main' into mg-use-request-id-instead-of-thread-id
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jun 13, 2023
2 parents 3098165 + fc54787 commit 5aa045c
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 99 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:password@httpbin.org/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 @@ -705,11 +705,11 @@ def test_response_attributes_invalid_status_code(self):
self.assertEqual(self.span.set_status.call_count, 1)

def test_credential_removal(self):
self.scope["server"] = ("username:password@httpbin.org", 80)
self.scope["server"] = ("username:password@mock", 80)
self.scope["path"] = "/status/200"
attrs = otel_asgi.collect_request_attributes(self.scope)
self.assertEqual(
attrs[SpanAttributes.HTTP_URL], "http://httpbin.org/status/200"
attrs[SpanAttributes.HTTP_URL], "http://mock/status/200"
)

def test_collect_target_attribute_missing(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ When using the instrumentor, all clients will automatically trace requests.
import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
url = "https://httpbin.org/get"
url = "https://some.url/get"
HTTPXClientInstrumentor().instrument()
with httpx.Client() as client:
Expand All @@ -51,7 +51,7 @@ use the `instrument_client` method.
import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
url = "https://httpbin.org/get"
url = "https://some.url/get"
with httpx.Client(transport=telemetry_transport) as client:
HTTPXClientInstrumentor.instrument_client(client)
Expand Down Expand Up @@ -96,7 +96,7 @@ If you don't want to use the instrumentor class, you can use the transport class
SyncOpenTelemetryTransport,
)
url = "https://httpbin.org/get"
url = "https://some.url/get"
transport = httpx.HTTPTransport()
telemetry_transport = SyncOpenTelemetryTransport(transport)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
url = "https://httpbin.org/get"
url = "https://some.url/get"
HTTPXClientInstrumentor().instrument()
with httpx.Client() as client:
Expand All @@ -46,7 +46,7 @@
import httpx
from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor
url = "https://httpbin.org/get"
url = "https://some.url/get"
with httpx.Client(transport=telemetry_transport) as client:
HTTPXClientInstrumentor.instrument_client(client)
Expand Down Expand Up @@ -91,7 +91,7 @@
SyncOpenTelemetryTransport,
)
url = "https://httpbin.org/get"
url = "https://some.url/get"
transport = httpx.HTTPTransport()
telemetry_transport = SyncOpenTelemetryTransport(transport)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class BaseTestCases:
class BaseTest(TestBase, metaclass=abc.ABCMeta):
# pylint: disable=no-member

URL = "http://httpbin.org/status/200"
URL = "http://mock/status/200"
response_hook = staticmethod(_response_hook)
request_hook = staticmethod(_request_hook)
no_update_request_hook = staticmethod(_no_update_request_hook)
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_basic_multiple(self):
self.assert_span(num_spans=2)

def test_not_foundbasic(self):
url_404 = "http://httpbin.org/status/404"
url_404 = "http://mock/status/404"

with respx.mock:
respx.get(url_404).mock(httpx.Response(404))
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 @@ -63,7 +63,7 @@ class RequestsIntegrationTestBase(abc.ABC):
# pylint: disable=no-member
# pylint: disable=too-many-public-methods

URL = "http://httpbin.org/status/200"
URL = "http://mock/status/200"

# pylint: disable=invalid-name
def setUp(self):
Expand Down Expand Up @@ -152,7 +152,7 @@ def response_hook(span, request_obj, response):
self.assertEqual(span.attributes["response_hook_attr"], "value")

def test_excluded_urls_explicit(self):
url_404 = "http://httpbin.org/status/404"
url_404 = "http://mock/status/404"
httpretty.register_uri(
httpretty.GET,
url_404,
Expand Down Expand Up @@ -194,7 +194,7 @@ def name_callback(method, url):
self.assertEqual(span.name, "HTTP GET")

def test_not_foundbasic(self):
url_404 = "http://httpbin.org/status/404"
url_404 = "http://mock/status/404"
httpretty.register_uri(
httpretty.GET,
url_404,
Expand Down Expand Up @@ -460,7 +460,7 @@ def perform_request(url: str, session: requests.Session = None):
return session.get(url)

def test_credential_removal(self):
new_url = "http://username:password@httpbin.org/status/200"
new_url = "http://username:password@mock/status/200"
self.perform_request(new_url)
span = self.assert_span()

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:password@httpbin.org/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 @@ -27,8 +27,8 @@


class TestUrllibMetricsInstrumentation(TestBase):
URL = "http://httpbin.org/status/200"
URL_POST = "http://httpbin.org/post"
URL = "http://mock/status/200"
URL_POST = "http://mock/post"

def setUp(self):
super().setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
class RequestsIntegrationTestBase(abc.ABC):
# pylint: disable=no-member

URL = "http://httpbin.org/status/200"
URL_TIMEOUT = "http://httpbin.org/timeout/0"
URL_EXCEPTION = "http://httpbin.org/exception/0"
URL = "http://mock/status/200"
URL_TIMEOUT = "http://mock/timeout/0"
URL_EXCEPTION = "http://mock/exception/0"

# pylint: disable=invalid-name
def setUp(self):
Expand Down Expand Up @@ -83,7 +83,7 @@ def setUp(self):
)
httpretty.register_uri(
httpretty.GET,
"http://httpbin.org/status/500",
"http://mock/status/500",
status=500,
)

Expand Down Expand Up @@ -142,7 +142,7 @@ def test_basic(self):
)

def test_excluded_urls_explicit(self):
url_201 = "http://httpbin.org/status/201"
url_201 = "http://mock/status/201"
httpretty.register_uri(
httpretty.GET,
url_201,
Expand Down Expand Up @@ -172,7 +172,7 @@ def test_excluded_urls_from_env(self):
self.assert_span(num_spans=1)

def test_not_foundbasic(self):
url_404 = "http://httpbin.org/status/404/"
url_404 = "http://mock/status/404/"
httpretty.register_uri(
httpretty.GET,
url_404,
Expand Down Expand Up @@ -336,14 +336,14 @@ def test_custom_tracer_provider(self):

def test_requests_exception_with_response(self, *_, **__):
with self.assertRaises(HTTPError):
self.perform_request("http://httpbin.org/status/500")
self.perform_request("http://mock/status/500")

span = self.assert_span()
self.assertEqual(
dict(span.attributes),
{
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_URL: "http://httpbin.org/status/500",
SpanAttributes.HTTP_URL: "http://mock/status/500",
SpanAttributes.HTTP_STATUS_CODE: 500,
},
)
Expand All @@ -365,7 +365,7 @@ def test_requests_timeout_exception(self, *_, **__):
self.assertEqual(span.status.status_code, StatusCode.ERROR)

def test_credential_removal(self):
url = "http://username:password@httpbin.org/status/200"
url = "http://username:password@mock/status/200"

with self.assertRaises(Exception):
self.perform_request(url)
Expand Down
Loading

0 comments on commit 5aa045c

Please sign in to comment.