Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions tests/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from fastapi import status
from httpx import ASGITransport, AsyncClient
from opentelemetry import trace
from opentelemetry.sdk.trace import ReadableSpan, TracerProvider
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.trace import SpanKind
Expand All @@ -18,7 +18,7 @@
import pytest


async def _trace_list_decks(monkeypatch: pytest.MonkeyPatch) -> tuple[ReadableSpan, ...]:
async def test_traced_request_records_server_and_db_spans_only(monkeypatch: pytest.MonkeyPatch) -> None:
traced_config = dataclasses.replace(settings.api_bootstrapper_config, opentelemetry_log_traces=True)
monkeypatch.setattr(Settings, "api_bootstrapper_config", traced_config)
exporter = InMemorySpanExporter()
Expand All @@ -31,26 +31,20 @@ async def _trace_list_decks(monkeypatch: pytest.MonkeyPatch) -> tuple[ReadableSp
response = await client.get("/api/decks/")

assert response.status_code == status.HTTP_200_OK
return exporter.get_finished_spans()


async def test_db_queries_produce_sqlalchemy_spans(monkeypatch: pytest.MonkeyPatch) -> None:
spans = exporter.get_finished_spans()
sqlalchemy_scope = "opentelemetry.instrumentation.sqlalchemy"
db_spans = [
span
for span in await _trace_list_decks(monkeypatch)
for span in spans
if span.instrumentation_scope is not None
and span.instrumentation_scope.name == "opentelemetry.instrumentation.sqlalchemy"
and span.instrumentation_scope.name == sqlalchemy_scope
and span.name.startswith("SELECT")
]
assert db_spans
assert all(span.parent is not None for span in db_spans)


async def test_request_has_no_asgi_event_spans(monkeypatch: pytest.MonkeyPatch) -> None:
request_spans = [
span
for span in await _trace_list_decks(monkeypatch)
if span.instrumentation_scope is None
or span.instrumentation_scope.name != "opentelemetry.instrumentation.sqlalchemy"
for span in spans
if span.instrumentation_scope is None or span.instrumentation_scope.name != sqlalchemy_scope
]
assert [span.kind for span in request_spans] == [SpanKind.SERVER]
6 changes: 3 additions & 3 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading