Skip to content

Commit fc36beb

Browse files
committed
Try to fix tests
1 parent edf3ea7 commit fc36beb

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-4
lines changed

packages/service-library/src/servicelib/aiohttp/tracing.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from opentelemetry.instrumentation.aiohttp_server import (
1616
middleware as aiohttp_server_opentelemetry_middleware, # pylint:disable=no-name-in-module
1717
)
18-
from opentelemetry.instrumentation.requests import RequestsInstrumentor
1918
from opentelemetry.sdk.resources import Resource
2019
from opentelemetry.sdk.trace import TracerProvider
2120
from opentelemetry.sdk.trace.export import BatchSpanProcessor
@@ -37,6 +36,12 @@
3736
HAS_AIOPG = True
3837
except ImportError:
3938
HAS_AIOPG = False
39+
try:
40+
from opentelemetry.instrumentation.requests import RequestsInstrumentor
41+
42+
HAS_REQUESTS = True
43+
except ImportError:
44+
HAS_REQUESTS = False
4045

4146

4247
def setup_tracing(
@@ -116,4 +121,10 @@ def setup_tracing(
116121
msg="Attempting to add botocore opentelemetry autoinstrumentation...",
117122
):
118123
BotocoreInstrumentor().instrument()
119-
RequestsInstrumentor().instrument()
124+
if HAS_REQUESTS:
125+
with log_context(
126+
_logger,
127+
logging.INFO,
128+
msg="Attempting to add requests opentelemetry autoinstrumentation...",
129+
):
130+
RequestsInstrumentor().instrument()

packages/service-library/src/servicelib/fastapi/tracing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
except ImportError:
5151
HAS_BOTOCORE = False
5252

53+
try:
54+
from opentelemetry.instrumentation.requests import RequestsInstrumentor
55+
56+
HAS_REQUESTS = True
57+
except ImportError:
58+
HAS_REQUESTS = False
59+
5360

5461
def setup_tracing(
5562
app: FastAPI, tracing_settings: TracingSettings, service_name: str
@@ -107,3 +114,10 @@ def setup_tracing(
107114
msg="Attempting to add botocore opentelemetry autoinstrumentation...",
108115
):
109116
BotocoreInstrumentor().instrument()
117+
if HAS_REQUESTS:
118+
with log_context(
119+
_logger,
120+
logging.INFO,
121+
msg="Attempting to add requests opentelemetry autoinstrumentation...",
122+
):
123+
RequestsInstrumentor().instrument()

packages/service-library/tests/aiohttp/test_tracing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import importlib
66
from collections.abc import Callable
7-
from typing import Any
7+
from typing import Any, Iterator
88

99
import pip
1010
import pytest
@@ -45,6 +45,7 @@ async def test_valid_tracing_settings(
4545
aiohttp_client: Callable,
4646
set_and_clean_settings_env_vars: Callable,
4747
tracing_settings_in,
48+
uninstrument_opentelemetry: Iterator[None],
4849
) -> TestClient:
4950
app = web.Application()
5051
service_name = "simcore_service_webserver"
@@ -69,6 +70,7 @@ async def test_invalid_tracing_settings(
6970
aiohttp_client: Callable,
7071
set_and_clean_settings_env_vars: Callable,
7172
tracing_settings_in,
73+
uninstrument_opentelemetry: Iterator[None],
7274
) -> TestClient:
7375
with pytest.raises(ValidationError):
7476
TracingSettings()
@@ -115,6 +117,7 @@ async def test_tracing_setup_package_detection(
115117
set_and_clean_settings_env_vars: Callable[[], None],
116118
tracing_settings_in: Callable[[], dict[str, Any]],
117119
manage_package,
120+
uninstrument_opentelemetry: Iterator[None],
118121
):
119122
package_name = manage_package
120123
importlib.import_module(package_name)

packages/service-library/tests/conftest.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,60 @@ async def _cleanup_redis_data(clients_manager: RedisClientsManager) -> None:
9999
await _cleanup_redis_data(clients_manager)
100100
yield _
101101
await _cleanup_redis_data(clients_manager)
102+
103+
104+
@pytest.fixture()
105+
def uninstrument_opentelemetry():
106+
yield
107+
try:
108+
from opentelemetry.instrumentation.redis import RedisInstrumentor
109+
110+
RedisInstrumentor().uninstrument()
111+
except ImportError:
112+
pass
113+
try:
114+
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
115+
116+
BotocoreInstrumentor().uninstrument()
117+
except ImportError:
118+
pass
119+
try:
120+
from opentelemetry.instrumentation.requests import RequestsInstrumentor
121+
122+
RequestsInstrumentor().uninstrument()
123+
except ImportError:
124+
pass
125+
try:
126+
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor
127+
128+
AiopgInstrumentor().uninstrument()
129+
except ImportError:
130+
pass
131+
try:
132+
from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor
133+
134+
AsyncPGInstrumentor().uninstrument()
135+
except ImportError:
136+
pass
137+
try:
138+
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
139+
140+
FastAPIInstrumentor().uninstrument()
141+
except ImportError:
142+
pass
143+
try:
144+
from opentelemetry.instrumentation.aiohttp_client import (
145+
AioHttpClientInstrumentor,
146+
)
147+
148+
AioHttpClientInstrumentor().uninstrument()
149+
except ImportError:
150+
pass
151+
try:
152+
from opentelemetry.instrumentation.aiohttp_server import (
153+
AioHttpServerInstrumentor,
154+
)
155+
156+
AioHttpServerInstrumentor().uninstrument()
157+
except ImportError:
158+
pass

packages/service-library/tests/fastapi/test_tracing.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import random
66
import string
77
from collections.abc import Callable
8-
from typing import Any
8+
from typing import Any, Iterator
99

1010
import pip
1111
import pytest
@@ -51,6 +51,7 @@ async def test_valid_tracing_settings(
5151
mocked_app: FastAPI,
5252
set_and_clean_settings_env_vars: Callable[[], None],
5353
tracing_settings_in: Callable[[], dict[str, Any]],
54+
uninstrument_opentelemetry: Iterator[None],
5455
):
5556
tracing_settings = TracingSettings()
5657
setup_tracing(
@@ -86,6 +87,7 @@ async def test_invalid_tracing_settings(
8687
mocked_app: FastAPI,
8788
set_and_clean_settings_env_vars: Callable[[], None],
8889
tracing_settings_in: Callable[[], dict[str, Any]],
90+
uninstrument_opentelemetry: Iterator[None],
8991
):
9092
app = mocked_app
9193
with pytest.raises((BaseException, ValidationError, TypeError)): # noqa: PT012
@@ -137,6 +139,7 @@ async def test_tracing_setup_package_detection(
137139
mocked_app: FastAPI,
138140
set_and_clean_settings_env_vars: Callable[[], None],
139141
tracing_settings_in: Callable[[], dict[str, Any]],
142+
uninstrument_opentelemetry: Iterator[None],
140143
manage_package,
141144
):
142145
package_name = manage_package

0 commit comments

Comments
 (0)