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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ CI (`.github/workflows/main.yml`) runs `ruff format --check`, `ruff check --no-f
`app/__main__.py` boots Granian pointing at `app.application:build_app` (factory). `build_app()` in `app/application.py`:

1. Creates a `modern_di.Container` with the `Dependencies` group from `app/ioc.py`.
2. Builds a `FastAPIBootstrapper` from `settings.api_bootstrapper_config`, injecting SQLAlchemy + asyncpg OpenTelemetry instrumentors.
2. Builds a `FastAPIBootstrapper` from `settings.api_bootstrapper_config`, injecting the SQLAlchemy OpenTelemetry instrumentor.
3. `modern_di_fastapi.setup_di(app, container)` wires DI scopes onto the FastAPI app.
4. Includes one `ROUTER` per resource (`app.api.decks.ROUTER`, `app.api.cards.ROUTER`) under `/api` via `include_routers`. Add a new resource by creating `app/api/<name>.py` with its own `APIRouter`-based `ROUTER` and registering it there.
5. Registers `DuplicateKeyError` → 422 handler from `app/exceptions.py`.
Expand Down
6 changes: 1 addition & 5 deletions app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import modern_di_fastapi
from advanced_alchemy.exceptions import DuplicateKeyError, NotFoundError
from lite_bootstrap import FastAPIBootstrapper
from opentelemetry.instrumentation.asyncpg import AsyncPGInstrumentor
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor

from app import exceptions, ioc
Expand All @@ -26,10 +25,7 @@ def build_app() -> fastapi.FastAPI:
di_container = modern_di.Container(groups=[ioc.Dependencies])
bootstrap_config = dataclasses.replace(
settings.api_bootstrapper_config,
opentelemetry_instrumentors=[
SQLAlchemyInstrumentor(),
AsyncPGInstrumentor(capture_parameters=True),
],
opentelemetry_instrumentors=[SQLAlchemyInstrumentor()],
)
bootstrapper = FastAPIBootstrapper(bootstrap_config=bootstrap_config)
app: fastapi.FastAPI = bootstrapper.bootstrap()
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ dependencies = [
"sqlalchemy[asyncio]<2.1",
"asyncpg",
# tracing
"opentelemetry-instrumentation-asyncpg",
"opentelemetry-instrumentation-sqlalchemy",
]

Expand Down
41 changes: 41 additions & 0 deletions tests/test_tracing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import dataclasses
from typing import TYPE_CHECKING

from asgi_lifespan import LifespanManager
from fastapi import status
from httpx import ASGITransport, AsyncClient
from opentelemetry import trace
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 app.application import build_app
from app.settings import Settings, settings


if TYPE_CHECKING:
import pytest


async def test_db_queries_produce_sqlalchemy_spans(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()
app = build_app()
tracer_provider = trace.get_tracer_provider()
assert isinstance(tracer_provider, TracerProvider)
tracer_provider.add_span_processor(SimpleSpanProcessor(exporter))

async with LifespanManager(app), AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as client:
response = await client.get("/api/decks/")

assert response.status_code == status.HTTP_200_OK
db_spans = [
span
for span in exporter.get_finished_spans()
if span.instrumentation_scope is not None
and span.instrumentation_scope.name == "opentelemetry.instrumentation.sqlalchemy"
and span.name.startswith("SELECT")
]
assert db_spans
assert all(span.parent is not None for span in db_spans)
16 changes: 0 additions & 16 deletions uv.lock

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

Loading