|
| 1 | +import logging |
| 2 | +from unittest.mock import MagicMock, patch |
| 3 | + |
| 4 | +from fastapi import FastAPI |
| 5 | + |
| 6 | +from slm_server.config import TraceSettings |
| 7 | +from slm_server.trace import setup_tracing |
| 8 | + |
| 9 | + |
| 10 | +def test_setup_tracing_disabled(): |
| 11 | + """When tracing is disabled, nothing is set up.""" |
| 12 | + app = FastAPI() |
| 13 | + settings = TraceSettings( |
| 14 | + enabled=False, |
| 15 | + endpoint="http://tempo:4318", |
| 16 | + username="user", |
| 17 | + password="pass", |
| 18 | + ) |
| 19 | + with patch("slm_server.trace.trace.set_tracer_provider") as mock_set_tp: |
| 20 | + setup_tracing(app, settings) |
| 21 | + mock_set_tp.assert_not_called() |
| 22 | + |
| 23 | + |
| 24 | +def test_setup_tracing_missing_endpoint(caplog): |
| 25 | + """When enabled but endpoint is empty, logs warning and skips setup.""" |
| 26 | + app = FastAPI() |
| 27 | + settings = TraceSettings( |
| 28 | + enabled=True, |
| 29 | + endpoint="", |
| 30 | + username="user", |
| 31 | + password="pass", |
| 32 | + ) |
| 33 | + with ( |
| 34 | + patch("slm_server.trace.trace.set_tracer_provider") as mock_set_tp, |
| 35 | + caplog.at_level(logging.WARNING, logger="slm_server.trace"), |
| 36 | + ): |
| 37 | + setup_tracing(app, settings) |
| 38 | + mock_set_tp.assert_not_called() |
| 39 | + assert "not configured" in caplog.text |
| 40 | + |
| 41 | + |
| 42 | +def test_setup_tracing_missing_username(caplog): |
| 43 | + """When enabled but username is empty, logs warning and skips setup.""" |
| 44 | + app = FastAPI() |
| 45 | + settings = TraceSettings( |
| 46 | + enabled=True, |
| 47 | + endpoint="http://tempo:4318", |
| 48 | + username="", |
| 49 | + password="pass", |
| 50 | + ) |
| 51 | + with ( |
| 52 | + patch("slm_server.trace.trace.set_tracer_provider") as mock_set_tp, |
| 53 | + caplog.at_level(logging.WARNING, logger="slm_server.trace"), |
| 54 | + ): |
| 55 | + setup_tracing(app, settings) |
| 56 | + mock_set_tp.assert_not_called() |
| 57 | + assert "not configured" in caplog.text |
| 58 | + |
| 59 | + |
| 60 | +def test_setup_tracing_missing_password(caplog): |
| 61 | + """When enabled but password is empty, logs warning and skips setup.""" |
| 62 | + app = FastAPI() |
| 63 | + settings = TraceSettings( |
| 64 | + enabled=True, |
| 65 | + endpoint="http://tempo:4318", |
| 66 | + username="user", |
| 67 | + password="", |
| 68 | + ) |
| 69 | + with ( |
| 70 | + patch("slm_server.trace.trace.set_tracer_provider") as mock_set_tp, |
| 71 | + caplog.at_level(logging.WARNING, logger="slm_server.trace"), |
| 72 | + ): |
| 73 | + setup_tracing(app, settings) |
| 74 | + mock_set_tp.assert_not_called() |
| 75 | + assert "not configured" in caplog.text |
| 76 | + |
| 77 | + |
| 78 | +def test_setup_tracing_full_setup(): |
| 79 | + """When fully configured, sets up tracer provider, processors, and instruments app.""" |
| 80 | + app = FastAPI() |
| 81 | + settings = TraceSettings( |
| 82 | + enabled=True, |
| 83 | + service_name="test-service", |
| 84 | + endpoint="http://tempo:4318/v1/traces", |
| 85 | + username="user", |
| 86 | + password="pass", |
| 87 | + sample_rate=1.0, |
| 88 | + excluded_urls=["/health"], |
| 89 | + ) |
| 90 | + |
| 91 | + mock_provider = MagicMock() |
| 92 | + |
| 93 | + with ( |
| 94 | + patch("slm_server.trace.trace.set_tracer_provider") as mock_set_tp, |
| 95 | + patch("slm_server.trace.trace.get_tracer_provider", return_value=mock_provider), |
| 96 | + patch("slm_server.trace.OTLPSpanExporter") as mock_otlp, |
| 97 | + patch("slm_server.trace.BatchSpanProcessor") as mock_batch, |
| 98 | + patch("slm_server.trace.FastAPIInstrumentor") as mock_instrumentor, |
| 99 | + ): |
| 100 | + setup_tracing(app, settings) |
| 101 | + |
| 102 | + # Tracer provider was set |
| 103 | + mock_set_tp.assert_called_once() |
| 104 | + |
| 105 | + # OTLP exporter created with endpoint and auth header |
| 106 | + mock_otlp.assert_called_once() |
| 107 | + call_kwargs = mock_otlp.call_args |
| 108 | + assert call_kwargs[1]["endpoint"] == "http://tempo:4318/v1/traces" |
| 109 | + assert "Authorization" in call_kwargs[1]["headers"] |
| 110 | + assert call_kwargs[1]["headers"]["Authorization"].startswith("Basic ") |
| 111 | + |
| 112 | + # BatchSpanProcessor created with the OTLP exporter |
| 113 | + mock_batch.assert_called_once_with(mock_otlp.return_value) |
| 114 | + |
| 115 | + # Three span processors added: OTLP batch + logging + metrics |
| 116 | + assert mock_provider.add_span_processor.call_count == 3 |
| 117 | + |
| 118 | + # FastAPI instrumented |
| 119 | + mock_instrumentor.instrument_app.assert_called_once() |
| 120 | + instr_kwargs = mock_instrumentor.instrument_app.call_args |
| 121 | + assert instr_kwargs[1]["excluded_urls"] == "/health" |
0 commit comments