Skip to content

Commit

Permalink
Try to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnicegyu11 committed Nov 1, 2024
1 parent edf3ea7 commit fc36beb
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 4 deletions.
15 changes: 13 additions & 2 deletions packages/service-library/src/servicelib/aiohttp/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from opentelemetry.instrumentation.aiohttp_server import (
middleware as aiohttp_server_opentelemetry_middleware, # pylint:disable=no-name-in-module
)
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
Expand All @@ -37,6 +36,12 @@
HAS_AIOPG = True
except ImportError:
HAS_AIOPG = False
try:
from opentelemetry.instrumentation.requests import RequestsInstrumentor

HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False


def setup_tracing(
Expand Down Expand Up @@ -116,4 +121,10 @@ def setup_tracing(
msg="Attempting to add botocore opentelemetry autoinstrumentation...",
):
BotocoreInstrumentor().instrument()
RequestsInstrumentor().instrument()
if HAS_REQUESTS:
with log_context(
_logger,
logging.INFO,
msg="Attempting to add requests opentelemetry autoinstrumentation...",
):
RequestsInstrumentor().instrument()
14 changes: 14 additions & 0 deletions packages/service-library/src/servicelib/fastapi/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@
except ImportError:
HAS_BOTOCORE = False

try:
from opentelemetry.instrumentation.requests import RequestsInstrumentor

HAS_REQUESTS = True
except ImportError:
HAS_REQUESTS = False


def setup_tracing(
app: FastAPI, tracing_settings: TracingSettings, service_name: str
Expand Down Expand Up @@ -107,3 +114,10 @@ def setup_tracing(
msg="Attempting to add botocore opentelemetry autoinstrumentation...",
):
BotocoreInstrumentor().instrument()
if HAS_REQUESTS:
with log_context(
_logger,
logging.INFO,
msg="Attempting to add requests opentelemetry autoinstrumentation...",
):
RequestsInstrumentor().instrument()
5 changes: 4 additions & 1 deletion packages/service-library/tests/aiohttp/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import importlib
from collections.abc import Callable
from typing import Any
from typing import Any, Iterator

import pip
import pytest
Expand Down Expand Up @@ -45,6 +45,7 @@ async def test_valid_tracing_settings(
aiohttp_client: Callable,
set_and_clean_settings_env_vars: Callable,
tracing_settings_in,
uninstrument_opentelemetry: Iterator[None],
) -> TestClient:
app = web.Application()
service_name = "simcore_service_webserver"
Expand All @@ -69,6 +70,7 @@ async def test_invalid_tracing_settings(
aiohttp_client: Callable,
set_and_clean_settings_env_vars: Callable,
tracing_settings_in,
uninstrument_opentelemetry: Iterator[None],
) -> TestClient:
with pytest.raises(ValidationError):
TracingSettings()
Expand Down Expand Up @@ -115,6 +117,7 @@ async def test_tracing_setup_package_detection(
set_and_clean_settings_env_vars: Callable[[], None],
tracing_settings_in: Callable[[], dict[str, Any]],
manage_package,
uninstrument_opentelemetry: Iterator[None],
):
package_name = manage_package
importlib.import_module(package_name)
Expand Down
57 changes: 57 additions & 0 deletions packages/service-library/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,60 @@ async def _cleanup_redis_data(clients_manager: RedisClientsManager) -> None:
await _cleanup_redis_data(clients_manager)
yield _
await _cleanup_redis_data(clients_manager)


@pytest.fixture()
def uninstrument_opentelemetry():
yield
try:
from opentelemetry.instrumentation.redis import RedisInstrumentor

RedisInstrumentor().uninstrument()
except ImportError:
pass
try:
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor

BotocoreInstrumentor().uninstrument()
except ImportError:
pass
try:
from opentelemetry.instrumentation.requests import RequestsInstrumentor

RequestsInstrumentor().uninstrument()
except ImportError:
pass
try:
from opentelemetry.instrumentation.aiopg import AiopgInstrumentor

AiopgInstrumentor().uninstrument()
except ImportError:
pass
try:
from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor

AsyncPGInstrumentor().uninstrument()
except ImportError:
pass
try:
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor

FastAPIInstrumentor().uninstrument()
except ImportError:
pass
try:
from opentelemetry.instrumentation.aiohttp_client import (
AioHttpClientInstrumentor,
)

AioHttpClientInstrumentor().uninstrument()
except ImportError:
pass
try:
from opentelemetry.instrumentation.aiohttp_server import (
AioHttpServerInstrumentor,
)

AioHttpServerInstrumentor().uninstrument()
except ImportError:
pass
5 changes: 4 additions & 1 deletion packages/service-library/tests/fastapi/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import random
import string
from collections.abc import Callable
from typing import Any
from typing import Any, Iterator

import pip
import pytest
Expand Down Expand Up @@ -51,6 +51,7 @@ async def test_valid_tracing_settings(
mocked_app: FastAPI,
set_and_clean_settings_env_vars: Callable[[], None],
tracing_settings_in: Callable[[], dict[str, Any]],
uninstrument_opentelemetry: Iterator[None],
):
tracing_settings = TracingSettings()
setup_tracing(
Expand Down Expand Up @@ -86,6 +87,7 @@ async def test_invalid_tracing_settings(
mocked_app: FastAPI,
set_and_clean_settings_env_vars: Callable[[], None],
tracing_settings_in: Callable[[], dict[str, Any]],
uninstrument_opentelemetry: Iterator[None],
):
app = mocked_app
with pytest.raises((BaseException, ValidationError, TypeError)): # noqa: PT012
Expand Down Expand Up @@ -137,6 +139,7 @@ async def test_tracing_setup_package_detection(
mocked_app: FastAPI,
set_and_clean_settings_env_vars: Callable[[], None],
tracing_settings_in: Callable[[], dict[str, Any]],
uninstrument_opentelemetry: Iterator[None],
manage_package,
):
package_name = manage_package
Expand Down

0 comments on commit fc36beb

Please sign in to comment.