Skip to content

Commit 301ca3e

Browse files
authored
fix: reload module for telemetry testing so all tests can run (#805)
* fix: reload module for telemetry testing so all tests can run * fix: remove unecessary ci skip checks * fix: get tracers from tracing provider * fix: tracing test and env var setting for tests * fix: issues with telemetry test changes * fix: remove unused fixture from telemetry tests
1 parent 9964f6e commit 301ca3e

4 files changed

Lines changed: 49 additions & 24 deletions

File tree

mellea/telemetry/tracing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ def _setup_tracer_provider() -> Any:
8787
_tracer_provider = _setup_tracer_provider()
8888
# Create separate tracers for application and backend
8989
_mellea_version = version("mellea")
90-
_application_tracer = trace.get_tracer("mellea.application", _mellea_version) # type: ignore
91-
_backend_tracer = trace.get_tracer("mellea.backend", _mellea_version) # type: ignore
90+
_application_tracer = _tracer_provider.get_tracer(
91+
"mellea.application", _mellea_version
92+
)
93+
_backend_tracer = _tracer_provider.get_tracer("mellea.backend", _mellea_version)
9294

9395

9496
def is_application_tracing_enabled() -> bool:

test/telemetry/test_metrics.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def clean_metrics_env(monkeypatch):
4545
def enable_metrics(monkeypatch):
4646
"""Enable metrics for tests."""
4747
monkeypatch.setenv("MELLEA_METRICS_ENABLED", "true")
48+
# Clear other env vars to prevent user-set values from leaking into reload
49+
monkeypatch.delenv("MELLEA_METRICS_CONSOLE", raising=False)
50+
monkeypatch.delenv("MELLEA_METRICS_OTLP", raising=False)
51+
monkeypatch.delenv("MELLEA_METRICS_PROMETHEUS", raising=False)
52+
monkeypatch.delenv("OTEL_EXPORTER_OTLP_ENDPOINT", raising=False)
53+
monkeypatch.delenv("OTEL_EXPORTER_OTLP_METRICS_ENDPOINT", raising=False)
54+
monkeypatch.delenv("OTEL_METRIC_EXPORT_INTERVAL", raising=False)
55+
monkeypatch.delenv("OTEL_SERVICE_NAME", raising=False)
4856
# Force reload of metrics module to pick up env vars
4957
import importlib
5058

@@ -453,6 +461,9 @@ def test_otlp_enabled_without_endpoint_warning(monkeypatch):
453461
"""Test that enabling OTLP without endpoint produces helpful warning."""
454462
monkeypatch.setenv("MELLEA_METRICS_ENABLED", "true")
455463
monkeypatch.setenv("MELLEA_METRICS_OTLP", "true")
464+
# Ensure no endpoint env vars are set (user env could have these)
465+
monkeypatch.delenv("OTEL_EXPORTER_OTLP_METRICS_ENDPOINT", raising=False)
466+
monkeypatch.delenv("OTEL_EXPORTER_OTLP_ENDPOINT", raising=False)
456467

457468
import importlib
458469

test/telemetry/test_tracing.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,21 @@ def enable_backend_tracing(monkeypatch):
4343
importlib.reload(mellea.telemetry.tracing)
4444

4545

46-
def test_telemetry_disabled_by_default():
46+
@pytest.fixture
47+
def disable_tracing(monkeypatch):
48+
"""Disable all tracing for tests."""
49+
monkeypatch.delenv("MELLEA_TRACE_APPLICATION", raising=False)
50+
monkeypatch.delenv("MELLEA_TRACE_BACKEND", raising=False)
51+
import importlib
52+
53+
import mellea.telemetry.tracing
54+
55+
importlib.reload(mellea.telemetry.tracing)
56+
yield
57+
importlib.reload(mellea.telemetry.tracing)
58+
59+
60+
def test_telemetry_disabled_by_default(disable_tracing):
4761
"""Test that telemetry is disabled by default."""
4862
from mellea.telemetry import (
4963
is_application_tracing_enabled,

test/telemetry/test_tracing_backend.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@
3737
@pytest.fixture(scope="module", autouse=True)
3838
def setup_telemetry():
3939
"""Set up telemetry for all tests in this module."""
40+
import importlib
41+
42+
import mellea.telemetry.tracing
43+
4044
mp = pytest.MonkeyPatch()
4145
mp.setenv("MELLEA_TRACE_BACKEND", "true")
46+
importlib.reload(mellea.telemetry.tracing)
4247

4348
yield
4449

45-
mp.undo()
50+
mp.setenv("MELLEA_TRACE_BACKEND", "false")
51+
importlib.reload(mellea.telemetry.tracing)
4652

4753

4854
@pytest.fixture
@@ -67,10 +73,8 @@ def span_exporter():
6773

6874

6975
@pytest.mark.asyncio
70-
async def test_span_duration_captures_async_operation(span_exporter, gh_run):
76+
async def test_span_duration_captures_async_operation(span_exporter):
7177
"""Test that span duration includes the full async operation time."""
72-
if gh_run:
73-
pytest.skip("Skipping in CI - requires Ollama")
7478

7579
backend = OllamaModelBackend(model_id=IBM_GRANITE_4_HYBRID_MICRO.ollama_name) # type: ignore
7680
ctx = SimpleContext()
@@ -113,17 +117,19 @@ async def test_span_duration_captures_async_operation(span_exporter, gh_run):
113117

114118

115119
@pytest.mark.asyncio
116-
async def test_context_propagation_parent_child(span_exporter, gh_run):
120+
async def test_context_propagation_parent_child(span_exporter):
117121
"""Test that parent-child span relationships are maintained."""
118-
if gh_run:
119-
pytest.skip("Skipping in CI - requires Ollama")
120122

121123
backend = OllamaModelBackend(model_id=IBM_GRANITE_4_HYBRID_MICRO.ollama_name) # type: ignore
122124
ctx = SimpleContext()
123125
ctx = ctx.add(Message(role="user", content="Say 'test' and nothing else"))
124126

125-
# Create a parent span
126-
tracer = trace.get_tracer(__name__)
127+
# Create a parent span using the module's own tracer provider
128+
# (not the global one, which may be pinned to a different provider
129+
# due to OTel's set-once semantics for set_tracer_provider)
130+
from mellea.telemetry import tracing
131+
132+
tracer = tracing._tracer_provider.get_tracer(__name__)
127133
with tracer.start_as_current_span("parent_operation"):
128134
mot, _ = await backend.generate_from_context(
129135
Message(role="assistant", content=""), ctx
@@ -158,10 +164,8 @@ async def test_context_propagation_parent_child(span_exporter, gh_run):
158164

159165

160166
@pytest.mark.asyncio
161-
async def test_token_usage_recorded_after_completion(span_exporter, gh_run):
167+
async def test_token_usage_recorded_after_completion(span_exporter):
162168
"""Test that token usage metrics are recorded after async completion."""
163-
if gh_run:
164-
pytest.skip("Skipping in CI - requires Ollama")
165169

166170
backend = OllamaModelBackend(model_id=IBM_GRANITE_4_HYBRID_MICRO.ollama_name) # type: ignore
167171
ctx = SimpleContext()
@@ -209,10 +213,8 @@ async def test_token_usage_recorded_after_completion(span_exporter, gh_run):
209213

210214

211215
@pytest.mark.asyncio
212-
async def test_span_not_closed_prematurely(span_exporter, gh_run):
216+
async def test_span_not_closed_prematurely(span_exporter):
213217
"""Test that spans are not closed before async operations complete."""
214-
if gh_run:
215-
pytest.skip("Skipping in CI - requires Ollama")
216218

217219
backend = OllamaModelBackend(model_id=IBM_GRANITE_4_HYBRID_MICRO.ollama_name) # type: ignore
218220
ctx = SimpleContext()
@@ -245,10 +247,8 @@ async def test_span_not_closed_prematurely(span_exporter, gh_run):
245247

246248

247249
@pytest.mark.asyncio
248-
async def test_multiple_generations_separate_spans(span_exporter, gh_run):
250+
async def test_multiple_generations_separate_spans(span_exporter):
249251
"""Test that multiple generations create separate spans."""
250-
if gh_run:
251-
pytest.skip("Skipping in CI - requires Ollama")
252252

253253
backend = OllamaModelBackend(model_id=IBM_GRANITE_4_HYBRID_MICRO.ollama_name) # type: ignore
254254
ctx = SimpleContext()
@@ -279,10 +279,8 @@ async def test_multiple_generations_separate_spans(span_exporter, gh_run):
279279

280280

281281
@pytest.mark.asyncio
282-
async def test_streaming_span_duration(span_exporter, gh_run):
282+
async def test_streaming_span_duration(span_exporter):
283283
"""Test that streaming operations have accurate span durations."""
284-
if gh_run:
285-
pytest.skip("Skipping in CI - requires Ollama")
286284

287285
from mellea.backends.model_options import ModelOption
288286

0 commit comments

Comments
 (0)