Skip to content

Commit

Permalink
switch to anyio to async testing, add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
s4v4g3 committed Oct 21, 2024
1 parent 1176041 commit 0490641
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 16 deletions.
2 changes: 1 addition & 1 deletion otel_extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ async def new_f_async(*args: Any, **kwargs: Any) -> Any:
)

def _module_allowed(self, func_module_path: str) -> bool:
if self.process_modules is None or self.process_modules == "":
if not self.process_modules:
return True

modules = self.process_modules.split(",")
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ addopts = "-ra --showlocals -vv"
testpaths = ["tests"]
xfail_strict = true
junit_family = "xunit2"
log_cli = 1
log_cli_level = "INFO"
log_cli_format = "%(asctime)s [%(levelname)8s] %(name)s - %(message)s"
log_cli_date_format = "%Y-%m-%d %H:%M:%S"
log_format = "%(asctime)s [%(levelname)8s] %(name)s - %(message)s"
log_date_format = "%Y-%m-%d %H:%M:%S"

[tool.mypy]
python_version = "3.8"
Expand Down
2 changes: 1 addition & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pytest
pytest-cov
opentelemetry-distro[otlp]
pytest-asyncio
anyio
mypy
pre-commit
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
pytest_plugins = "pytest_asyncio"
import pytest


@pytest.fixture
def anyio_backend():
return "asyncio"
7 changes: 0 additions & 7 deletions tests/pytest.ini

This file was deleted.

38 changes: 32 additions & 6 deletions tests/test_decorator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from otel_extensions import instrumented
from opentelemetry import trace
from opentelemetry.sdk.trace import ReadableSpan
import os
from collections.abc import Sequence

import pytest
from opentelemetry import trace
from opentelemetry.sdk.trace import ReadableSpan

from otel_extensions import instrumented

SPAN_ATTRS = {
"foo": "bar",
Expand Down Expand Up @@ -79,16 +82,39 @@ def test_decorator_with_span_attributes():
decorated_function_with_span_attrs()


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_async_decorator_with_default_name():
await decorated_async_function()


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_async_decorator_with_custom_name():
await decorated_async_function_with_custom_name()


@pytest.mark.asyncio
@pytest.mark.anyio
async def test_async_decorator_with_span_attributes():
await decorated_async_function_with_span_attrs()


@pytest.fixture
def otel_process_modules(request):
if request.param is not None:
os.environ["OTEL_PROCESS_MODULES"] = request.param
yield request.param
os.environ.pop("OTEL_PROCESS_MODULES", None)


@pytest.mark.parametrize(
"otel_process_modules", ["foo", "", None, "test_decorator"], indirect=True
)
def test_decorator_with_module_filter(otel_process_modules):
@instrumented(span_name="decorated_function_with_module_filter")
def decorated_local_function():
span: ReadableSpan = trace.get_current_span() # noqa
if otel_process_modules == "foo":
assert span.name == "test_decorator_with_module_filter[foo] (call)"
else:
assert span.name == "decorated_function_with_module_filter"

decorated_local_function()

0 comments on commit 0490641

Please sign in to comment.