示例工程 38-observability-otel

38-observability-otel/05-otel-console-span/test_otel_console_span.py

python · 0.8 KB

"""test_otel_console_span.py"""
 
from __future__ import annotations
 
import pytest
 
pytest.importorskip("opentelemetry.sdk")
 
from otel_console_span import main, run_trace
 
 
def test_parent_child_spans() -> None:
    trace_id, spans = run_trace()
    by_name = {span["name"]: span for span in spans}
    parent = by_name["handle-request"]
    child = by_name["load-user"]
    assert trace_id == parent["trace_id"]
    assert parent["parent_id"] is None
    assert child["parent_id"] == parent["span_id"]
    assert child["trace_id"] == parent["trace_id"]
    assert child["attributes"] == {"user.id": 42}
    assert child["events"] == ["cache_miss"]
 
 
def test_main_runs(capsys: pytest.CaptureFixture[str]) -> None:
    main()
    out = capsys.readouterr().out
    assert "child_parent_matches: True" in out
    assert "child_event: cache_miss" in out