File size: 6,388 Bytes
517919c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
"""Tests for Arize AX / Phoenix tracing setup branching."""

from __future__ import annotations

import importlib
import sys
from pathlib import Path
from types import ModuleType, SimpleNamespace
from unittest.mock import MagicMock, patch

import pytest

_AGENT_REDACT = Path(__file__).resolve().parents[1] / "agent-redact"
if str(_AGENT_REDACT) not in sys.path:
    sys.path.insert(0, str(_AGENT_REDACT))


def _reload_arize_monitoring():
    """Reload module so ``_INITIALIZED`` resets between tests."""
    import eval.arize_monitoring as mod

    importlib.reload(mod)
    return mod


@pytest.fixture(autouse=True)
def _clear_tracing_env(monkeypatch):
    for key in (
        "ARIZE_TRACING_ENABLED",
        "ARIZE_BACKEND",
        "ARIZE_SPACE_ID",
        "ARIZE_API_KEY",
        "ARIZE_PROJECT_NAME",
        "ARIZE_ENDPOINT",
        "PHOENIX_COLLECTOR_ENDPOINT",
        "PHOENIX_PROJECT_NAME",
        "PHOENIX_API_KEY",
    ):
        monkeypatch.delenv(key, raising=False)


def test_phoenix_otlp_endpoint_normalization():
    mod = _reload_arize_monitoring()
    assert (
        mod._phoenix_otlp_endpoint("http://localhost:6006")
        == "http://localhost:6006/v1/traces"
    )
    assert (
        mod._phoenix_otlp_endpoint("http://localhost:6006/v1/traces")
        == "http://localhost:6006/v1/traces"
    )
    assert mod._phoenix_otlp_endpoint("http://host:4317") == "http://host:4317"


def test_tracing_backend_defaults_and_aliases(monkeypatch):
    mod = _reload_arize_monitoring()
    assert mod._tracing_backend() == "ax"
    monkeypatch.setenv("ARIZE_BACKEND", "phoenix")
    assert mod._tracing_backend() == "phoenix"
    monkeypatch.setenv("ARIZE_BACKEND", "LOCAL")
    assert mod._tracing_backend() == "phoenix"
    monkeypatch.setenv("ARIZE_BACKEND", "ax")
    assert mod._tracing_backend() == "ax"


def test_setup_disabled_when_env_unset():
    mod = _reload_arize_monitoring()
    assert mod.setup_arize_ax_tracing() is False


def test_setup_ax_path_registers_with_space_and_key(monkeypatch):
    monkeypatch.setenv("ARIZE_TRACING_ENABLED", "true")
    monkeypatch.setenv("ARIZE_BACKEND", "ax")
    monkeypatch.setenv("ARIZE_SPACE_ID", "space-1")
    monkeypatch.setenv("ARIZE_API_KEY", "key-1")
    monkeypatch.setenv("ARIZE_PROJECT_NAME", "proj-ax")
    monkeypatch.setenv("ARIZE_ENDPOINT", "europe")

    mod = _reload_arize_monitoring()
    mock_register = MagicMock(return_value=SimpleNamespace(name="provider"))
    mock_endpoint = SimpleNamespace(ARIZE="us", ARIZE_EUROPE="eu")
    mock_instrumentor_cls = MagicMock()
    mock_instrumentor = MagicMock()
    mock_instrumentor_cls.return_value = mock_instrumentor

    arize_otel = ModuleType("arize.otel")
    arize_otel.Endpoint = mock_endpoint
    arize_otel.register = mock_register
    arize_pkg = ModuleType("arize")
    arize_pkg.otel = arize_otel

    oi_lc = ModuleType("openinference.instrumentation.langchain")
    oi_lc.LangChainInstrumentor = mock_instrumentor_cls

    with patch.dict(
        sys.modules,
        {
            "arize": arize_pkg,
            "arize.otel": arize_otel,
            "openinference.instrumentation.langchain": oi_lc,
        },
    ):
        assert mod.setup_arize_ax_tracing() is True

    mock_register.assert_called_once_with(
        space_id="space-1",
        api_key="key-1",
        project_name="proj-ax",
        endpoint="eu",
    )
    mock_instrumentor.instrument.assert_called_once()
    assert mod.setup_arize_ax_tracing() is True  # already initialized
    assert mock_register.call_count == 1


def test_setup_ax_skips_without_credentials(monkeypatch):
    monkeypatch.setenv("ARIZE_TRACING_ENABLED", "true")
    monkeypatch.setenv("ARIZE_BACKEND", "ax")
    mod = _reload_arize_monitoring()
    with pytest.warns(UserWarning, match="ARIZE_SPACE_ID"):
        assert mod.setup_arize_ax_tracing() is False


def test_setup_phoenix_path_registers_local_collector(monkeypatch):
    monkeypatch.setenv("ARIZE_TRACING_ENABLED", "true")
    monkeypatch.setenv("ARIZE_BACKEND", "phoenix")
    monkeypatch.setenv("ARIZE_PROJECT_NAME", "proj-px")
    monkeypatch.setenv("PHOENIX_COLLECTOR_ENDPOINT", "http://127.0.0.1:6006")
    monkeypatch.setenv("PHOENIX_API_KEY", "px-key")

    mod = _reload_arize_monitoring()
    mock_register = MagicMock(return_value=SimpleNamespace(name="provider"))
    mock_instrumentor_cls = MagicMock()
    mock_instrumentor = MagicMock()
    mock_instrumentor_cls.return_value = mock_instrumentor

    phoenix_otel = ModuleType("phoenix.otel")
    phoenix_otel.register = mock_register
    phoenix_pkg = ModuleType("phoenix")
    phoenix_pkg.otel = phoenix_otel

    oi_lc = ModuleType("openinference.instrumentation.langchain")
    oi_lc.LangChainInstrumentor = mock_instrumentor_cls

    with patch.dict(
        sys.modules,
        {
            "phoenix": phoenix_pkg,
            "phoenix.otel": phoenix_otel,
            "openinference.instrumentation.langchain": oi_lc,
        },
    ):
        assert mod.setup_arize_ax_tracing() is True

    mock_register.assert_called_once_with(
        project_name="proj-px",
        endpoint="http://127.0.0.1:6006/v1/traces",
        api_key="px-key",
    )
    mock_instrumentor.instrument.assert_called_once()


def test_setup_phoenix_default_endpoint_no_api_key(monkeypatch):
    monkeypatch.setenv("ARIZE_TRACING_ENABLED", "true")
    monkeypatch.setenv("ARIZE_BACKEND", "phoenix")

    mod = _reload_arize_monitoring()
    mock_register = MagicMock(return_value=SimpleNamespace(name="provider"))
    mock_instrumentor_cls = MagicMock()
    mock_instrumentor_cls.return_value = MagicMock()

    phoenix_otel = ModuleType("phoenix.otel")
    phoenix_otel.register = mock_register
    phoenix_pkg = ModuleType("phoenix")
    phoenix_pkg.otel = phoenix_otel
    oi_lc = ModuleType("openinference.instrumentation.langchain")
    oi_lc.LangChainInstrumentor = mock_instrumentor_cls

    with patch.dict(
        sys.modules,
        {
            "phoenix": phoenix_pkg,
            "phoenix.otel": phoenix_otel,
            "openinference.instrumentation.langchain": oi_lc,
        },
    ):
        assert mod.setup_arize_ax_tracing() is True

    kwargs = mock_register.call_args.kwargs
    assert kwargs["endpoint"] == "http://localhost:6006/v1/traces"
    assert kwargs["project_name"] == "doc-redaction-langgraph"
    assert "api_key" not in kwargs