12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ from enum import Enum
16
+ from http import HTTPStatus
17
+
18
+ import aiohttp
15
19
import pytest
16
20
import pytest_asyncio
17
- import aiohttp
18
- from http import HTTPStatus
19
- from .utils import HTTPMethod
20
21
21
22
from opentelemetry import trace as trace_api
22
- from opentelemetry .test .test_base import TestBase
23
- from opentelemetry .instrumentation .aiohttp_server import AioHttpServerInstrumentor
23
+ from opentelemetry .instrumentation .aiohttp_server import (
24
+ AioHttpServerInstrumentor ,
25
+ )
24
26
from opentelemetry .semconv .trace import SpanAttributes
27
+ from opentelemetry .test .globals_test import reset_trace_globals
28
+ from opentelemetry .test .test_base import TestBase
25
29
from opentelemetry .util ._importlib_metadata import entry_points
26
30
27
- from opentelemetry .test .globals_test import (
28
- reset_trace_globals ,
29
- )
30
31
32
+ class HTTPMethod (Enum ):
33
+ """HTTP methods and descriptions"""
34
+
35
+ def __repr__ (self ):
36
+ return f"{ self .value } "
37
+
38
+ CONNECT = "CONNECT"
39
+ DELETE = "DELETE"
40
+ GET = "GET"
41
+ HEAD = "HEAD"
42
+ OPTIONS = "OPTIONS"
43
+ PATCH = "PATCH"
44
+ POST = "POST"
45
+ PUT = "PUT"
46
+ TRACE = "TRACE"
31
47
32
- @pytest .fixture (scope = "session" )
33
- def tracer ():
48
+
49
+ @pytest .fixture (name = "tracer" , scope = "session" )
50
+ def fixture_tracer ():
34
51
test_base = TestBase ()
35
52
36
53
tracer_provider , memory_exporter = test_base .create_tracer_provider ()
@@ -47,15 +64,14 @@ async def default_handler(request, status=200):
47
64
return aiohttp .web .Response (status = status )
48
65
49
66
50
- @pytest_asyncio .fixture
51
- async def server_fixture (tracer , aiohttp_server ):
67
+ @pytest_asyncio .fixture ( name = "server_fixture" )
68
+ async def fixture_server_fixture (tracer , aiohttp_server ):
52
69
_ , memory_exporter = tracer
53
70
54
71
AioHttpServerInstrumentor ().instrument ()
55
72
56
73
app = aiohttp .web .Application ()
57
- app .add_routes (
58
- [aiohttp .web .get ("/test-path" , default_handler )])
74
+ app .add_routes ([aiohttp .web .get ("/test-path" , default_handler )])
59
75
60
76
server = await aiohttp_server (app )
61
77
yield server , app
@@ -67,26 +83,31 @@ async def server_fixture(tracer, aiohttp_server):
67
83
68
84
def test_checking_instrumentor_pkg_installed ():
69
85
70
- (instrumentor_entrypoint ,) = entry_points (group = "opentelemetry_instrumentor" , name = "aiohttp-server" )
86
+ (instrumentor_entrypoint ,) = entry_points (
87
+ group = "opentelemetry_instrumentor" , name = "aiohttp-server"
88
+ )
71
89
instrumentor = instrumentor_entrypoint .load ()()
72
- assert ( isinstance (instrumentor , AioHttpServerInstrumentor ) )
90
+ assert isinstance (instrumentor , AioHttpServerInstrumentor )
73
91
74
92
75
93
@pytest .mark .asyncio
76
- @pytest .mark .parametrize ("url, expected_method, expected_status_code" , [
77
- ("/test-path" , HTTPMethod .GET , HTTPStatus .OK ),
78
- ("/not-found" , HTTPMethod .GET , HTTPStatus .NOT_FOUND )
79
- ])
94
+ @pytest .mark .parametrize (
95
+ "url, expected_method, expected_status_code" ,
96
+ [
97
+ ("/test-path" , HTTPMethod .GET , HTTPStatus .OK ),
98
+ ("/not-found" , HTTPMethod .GET , HTTPStatus .NOT_FOUND ),
99
+ ],
100
+ )
80
101
async def test_status_code_instrumentation (
81
102
tracer ,
82
103
server_fixture ,
83
104
aiohttp_client ,
84
105
url ,
85
106
expected_method ,
86
- expected_status_code
107
+ expected_status_code ,
87
108
):
88
109
_ , memory_exporter = tracer
89
- server , app = server_fixture
110
+ server , _ = server_fixture
90
111
91
112
assert len (memory_exporter .get_finished_spans ()) == 0
92
113
@@ -98,8 +119,12 @@ async def test_status_code_instrumentation(
98
119
[span ] = memory_exporter .get_finished_spans ()
99
120
100
121
assert expected_method .value == span .attributes [SpanAttributes .HTTP_METHOD ]
101
- assert expected_status_code == span .attributes [SpanAttributes .HTTP_STATUS_CODE ]
102
-
103
- assert f"http://{ server .host } :{ server .port } { url } " == span .attributes [
104
- SpanAttributes .HTTP_URL
105
- ]
122
+ assert (
123
+ expected_status_code
124
+ == span .attributes [SpanAttributes .HTTP_STATUS_CODE ]
125
+ )
126
+
127
+ assert (
128
+ f"http://{ server .host } :{ server .port } { url } "
129
+ == span .attributes [SpanAttributes .HTTP_URL ]
130
+ )
0 commit comments