Files
mtusi2026lab7-safronov/service-tests/db/test_telemetry_db.py
ricoromaandClaude Opus 5.5 089d78609d chore: port lab 6 solution into the lab 7 skeleton
Source: mtusi2026lab6-safronov, solution/bvt2301-safronov, 6c6a065.
Files follow AGENTS.md, "Переход в следующую лабу". Contracts, public
tests, README, REPORT and evidence of lab 6 are not carried over; infra
and scripts of the lab 7 skeleton stay as they are.
Makefile is the lab 6 one with the lab 7 skeleton part restored
(HTTPS BASE_URL on 8443, test-security); .gitignore and .dockerignore
are taken from lab 6 because both skeletons have the same files;
.env.example is the lab 6 one without changes.
The compose project, image and CI prefix are renamed to lab7, and
references to the previous lab now name lab6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 03:59:04 +03:00

132 lines
4.6 KiB
Python

"""Span запросов PostgreSQL и фиксации на временной базе (design change
`otel-telemetry`, решения 4 и 12): слушатели engine видят контекст задачи в
greenlet асинхронной сессии, без записывающего родителя span нет."""
from collections.abc import Iterator
import pytest
from opentelemetry.sdk._logs.export import InMemoryLogRecordExporter
from opentelemetry.sdk.metrics.export import InMemoryMetricReader
from opentelemetry.sdk.trace import ReadableSpan
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
InMemorySpanExporter,
)
from opentelemetry.trace import SpanKind, StatusCode
from sqlalchemy import text
from sqlalchemy.exc import DBAPIError
from builders import PASSWORD_HASH, user_dto
from conftest import make_engine, make_factory, make_uow, truncate_all
from src.config import settings
from src.infra import telemetry
from src.infra.telemetry import core
pytestmark = pytest.mark.anyio
@pytest.fixture
def spans() -> Iterator[InMemorySpanExporter]:
exporter = InMemorySpanExporter()
assert telemetry.start_process_telemetry(
"messenger-api",
exporters=telemetry.Exporters(
spans=exporter,
metrics=InMemoryMetricReader(),
logs=InMemoryLogRecordExporter(),
),
)
try:
yield exporter
finally:
for thread in telemetry.begin_shutdown():
thread.join(5)
telemetry.remove_log_hooks()
def _finished(exporter: InMemorySpanExporter) -> list[ReadableSpan]:
for thread in telemetry.begin_shutdown():
thread.join(5)
return list(exporter.get_finished_spans())
@pytest.fixture
async def engine(spans: InMemorySpanExporter):
await truncate_all()
created = make_engine(pool_size=1)
telemetry.instrument_engine(created)
yield created
await created.dispose()
async def test_queries_and_commit_under_parent(
spans: InMemorySpanExporter,
engine,
) -> None:
uow = make_uow(make_factory(engine))
user = user_dto()
with core.tracer().start_as_current_span("parent") as parent:
async with uow:
await uow.users.insert_user(user=user, password_hash=PASSWORD_HASH)
await uow.commit()
async with uow:
await uow.users.get_user_credentials(username=user.username)
finished = _finished(spans)
parent_id = parent.get_span_context().span_id
children = [span for span in finished if span.name != "parent"]
names = [span.name for span in children]
assert "INSERT" in names
assert "COMMIT" in names
assert "SELECT" in names
for span in children:
assert span.parent is not None
assert span.parent.span_id == parent_id
assert span.kind == SpanKind.CLIENT
assert span.attributes["db.system.name"] == "postgresql"
insert = next(span for span in children if span.name == "INSERT")
assert insert.attributes["db.namespace"] == settings.database.name
assert insert.attributes["db.operation.name"] == "INSERT"
assert insert.attributes["server.port"] == settings.database.port
# параметризованный SQL: ни имени пользователя, ни хэша пароля
query = insert.attributes["db.query.text"]
assert user.username not in query
assert PASSWORD_HASH not in query
# запрос закончился раньше фиксации, оба — внутри родителя
commit = next(span for span in children if span.name == "COMMIT")
assert insert.end_time <= commit.start_time
async def test_no_spans_without_parent(
spans: InMemorySpanExporter,
engine,
) -> None:
uow = make_uow(make_factory(engine))
async with uow:
await uow.users.get_user_credentials(username="nobody_here")
async with engine.connect() as connection:
await connection.execute(text("SELECT 1"))
assert _finished(spans) == []
async def test_failed_query_marks_error(
spans: InMemorySpanExporter,
engine,
) -> None:
uow = make_uow(make_factory(engine))
with (
core.tracer().start_as_current_span("parent"),
pytest.raises(DBAPIError),
):
async with uow:
await uow._current_session().execute(
text("SELECT 1 / 0 AS secret_marker"),
)
(failed,) = [
span
for span in _finished(spans)
if span.status.status_code == StatusCode.ERROR
]
assert failed.name == "SELECT"
assert failed.attributes["error.type"]
assert failed.status.description is None
assert failed.events == ()