Verify exact production traces and record rollout
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from adapters.prometheus import PrometheusMetrics
|
||||
from config import Settings
|
||||
from main import create_app
|
||||
from rag.agent import AgentReply
|
||||
@@ -177,3 +178,63 @@ def test_a_trace_write_failure_does_not_turn_a_good_answer_into_a_500():
|
||||
assert body["answer"] == "Liều 500 mg [1]."
|
||||
assert body["trace_id"] # a locally-generated fallback id, still present
|
||||
assert metrics.total(TRACE_WRITE_FAILED) == 1
|
||||
|
||||
|
||||
class _AnyRouting:
|
||||
def retrieve(self, query, subject_scope, intent):
|
||||
return RetrievalResult(EvidenceDecision.ABSTAIN, "drug_not_resolved")
|
||||
|
||||
|
||||
def test_correlation_id_round_trips_through_headers_body_and_trace_row():
|
||||
traces = MemoryTraceWriter()
|
||||
app = create_app(
|
||||
settings=Settings(embedding_provider="disabled"),
|
||||
answer_service=GroundedAnswerService(_AnyRouting()),
|
||||
trace_writer=traces,
|
||||
)
|
||||
|
||||
response = TestClient(app).post(
|
||||
"/v1/rag/query",
|
||||
headers={"X-Correlation-ID": "req-test-1"},
|
||||
json={
|
||||
"query": "Paracetamol dose?",
|
||||
"subject_scope": "human",
|
||||
"intent": "fact_lookup",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers["x-correlation-id"] == "req-test-1"
|
||||
assert response.json()["correlation_id"] == "req-test-1"
|
||||
assert traces.rows[0]["correlation_id"] == "req-test-1"
|
||||
assert "otel_trace_id" in traces.rows[0]
|
||||
|
||||
|
||||
def test_metrics_endpoint_exposes_request_decision_and_stage_histograms():
|
||||
metrics = PrometheusMetrics()
|
||||
app = create_app(
|
||||
settings=Settings(embedding_provider="disabled"),
|
||||
answer_service=GroundedAnswerService(_AnyRouting()),
|
||||
trace_writer=MemoryTraceWriter(),
|
||||
metrics=metrics,
|
||||
)
|
||||
client = TestClient(app)
|
||||
|
||||
response = client.post(
|
||||
"/v1/rag/query",
|
||||
json={
|
||||
"query": "Paracetamol dose?",
|
||||
"subject_scope": "human",
|
||||
"intent": "fact_lookup",
|
||||
},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
scrape = client.get("/metrics")
|
||||
assert scrape.status_code == 200
|
||||
body = scrape.text
|
||||
assert 'duocthu_requests_total{method="POST",route="/v1/rag/query",status="2xx"}' in body
|
||||
assert 'duocthu_decision_total{decision="abstain",reason="drug_not_resolved"}' in body
|
||||
assert 'duocthu_stage_duration_seconds_count{outcome="ok",stage="receive"}' in body
|
||||
assert 'duocthu_stage_duration_seconds_count{outcome="ok",stage="persistence"}' in body
|
||||
assert 'duocthu_stage_duration_seconds_count{outcome="ok",stage="response"}' in body
|
||||
|
||||
Reference in New Issue
Block a user