|
| 1 | +# Copyright 2020, OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import asyncio |
| 16 | +import contextlib |
| 17 | +import typing |
| 18 | +import unittest |
| 19 | +import urllib.parse |
| 20 | +from functools import partial |
| 21 | +from http import HTTPStatus |
| 22 | +from unittest import mock |
| 23 | + |
| 24 | +import aiohttp |
| 25 | +import aiohttp.test_utils |
| 26 | +import yarl |
| 27 | +from pkg_resources import iter_entry_points |
| 28 | + |
| 29 | +from opentelemetry import context |
| 30 | +from opentelemetry.instrumentation import aiohttp_server |
| 31 | +from opentelemetry.instrumentation.aiohttp_server import ( |
| 32 | + AioHttpServerInstrumentor, |
| 33 | +) |
| 34 | +from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY |
| 35 | +from opentelemetry.semconv.trace import SpanAttributes |
| 36 | +from opentelemetry.test.test_base import TestBase |
| 37 | +from opentelemetry.trace import Span, StatusCode |
| 38 | + |
| 39 | + |
| 40 | +def run_with_test_server( |
| 41 | + runnable: typing.Callable, url: str, handler: typing.Callable |
| 42 | +) -> typing.Tuple[str, int]: |
| 43 | + async def do_request(): |
| 44 | + app = aiohttp.web.Application() |
| 45 | + parsed_url = urllib.parse.urlparse(url) |
| 46 | + app.add_routes([aiohttp.web.get(parsed_url.path, handler)]) |
| 47 | + app.add_routes([aiohttp.web.post(parsed_url.path, handler)]) |
| 48 | + app.add_routes([aiohttp.web.patch(parsed_url.path, handler)]) |
| 49 | + |
| 50 | + with contextlib.suppress(aiohttp.ClientError): |
| 51 | + async with aiohttp.test_utils.TestServer(app) as server: |
| 52 | + netloc = (server.host, server.port) |
| 53 | + await server.start_server() |
| 54 | + await runnable(server) |
| 55 | + return netloc |
| 56 | + |
| 57 | + loop = asyncio.get_event_loop() |
| 58 | + return loop.run_until_complete(do_request()) |
| 59 | + |
| 60 | + |
| 61 | +class TestAioHttpServerIntegration(TestBase): |
| 62 | + URL = "/test-path" |
| 63 | + |
| 64 | + def setUp(self): |
| 65 | + super().setUp() |
| 66 | + AioHttpServerInstrumentor().instrument() |
| 67 | + |
| 68 | + def tearDown(self): |
| 69 | + super().tearDown() |
| 70 | + AioHttpServerInstrumentor().uninstrument() |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + # pylint:disable=unused-argument |
| 74 | + async def default_handler(request, status=200): |
| 75 | + return aiohttp.web.Response(status=status) |
| 76 | + |
| 77 | + def assert_spans(self, num_spans: int): |
| 78 | + finished_spans = self.memory_exporter.get_finished_spans() |
| 79 | + self.assertEqual(num_spans, len(finished_spans)) |
| 80 | + if num_spans == 0: |
| 81 | + return None |
| 82 | + if num_spans == 1: |
| 83 | + return finished_spans[0] |
| 84 | + return finished_spans |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def get_default_request(url: str = URL): |
| 88 | + async def default_request(server: aiohttp.test_utils.TestServer): |
| 89 | + async with aiohttp.test_utils.TestClient(server) as session: |
| 90 | + await session.get(url) |
| 91 | + |
| 92 | + return default_request |
| 93 | + |
| 94 | + def test_instrument(self): |
| 95 | + host, port = run_with_test_server( |
| 96 | + self.get_default_request(), self.URL, self.default_handler |
| 97 | + ) |
| 98 | + span = self.assert_spans(1) |
| 99 | + self.assertEqual("GET", span.attributes[SpanAttributes.HTTP_METHOD]) |
| 100 | + self.assertEqual( |
| 101 | + f"http://{host}:{port}/test-path", |
| 102 | + span.attributes[SpanAttributes.HTTP_URL], |
| 103 | + ) |
| 104 | + self.assertEqual(200, span.attributes[SpanAttributes.HTTP_STATUS_CODE]) |
| 105 | + |
| 106 | + def test_status_codes(self): |
| 107 | + error_handler = partial(self.default_handler, status=400) |
| 108 | + host, port = run_with_test_server( |
| 109 | + self.get_default_request(), self.URL, error_handler |
| 110 | + ) |
| 111 | + span = self.assert_spans(1) |
| 112 | + self.assertEqual("GET", span.attributes[SpanAttributes.HTTP_METHOD]) |
| 113 | + self.assertEqual( |
| 114 | + f"http://{host}:{port}/test-path", |
| 115 | + span.attributes[SpanAttributes.HTTP_URL], |
| 116 | + ) |
| 117 | + self.assertEqual(400, span.attributes[SpanAttributes.HTTP_STATUS_CODE]) |
| 118 | + |
| 119 | + def test_not_recording(self): |
| 120 | + mock_tracer = mock.Mock() |
| 121 | + mock_span = mock.Mock() |
| 122 | + mock_span.is_recording.return_value = False |
| 123 | + mock_tracer.start_span.return_value = mock_span |
| 124 | + with mock.patch("opentelemetry.trace.get_tracer"): |
| 125 | + # pylint: disable=W0612 |
| 126 | + host, port = run_with_test_server( |
| 127 | + self.get_default_request(), self.URL, self.default_handler |
| 128 | + ) |
| 129 | + |
| 130 | + self.assertFalse(mock_span.is_recording()) |
| 131 | + self.assertTrue(mock_span.is_recording.called) |
| 132 | + self.assertFalse(mock_span.set_attribute.called) |
| 133 | + self.assertFalse(mock_span.set_status.called) |
0 commit comments