diff --git a/src/phoenix/config.py b/src/phoenix/config.py index c01e83d748..e452cdd82f 100644 --- a/src/phoenix/config.py +++ b/src/phoenix/config.py @@ -7,6 +7,11 @@ ENV_PHOENIX_PORT = "PHOENIX_PORT" ENV_PHOENIX_HOST = "PHOENIX_HOST" ENV_NOTEBOOK_ENV = "PHOENIX_NOTEBOOK_ENV" +ENV_PHOENIX_COLLECTOR_ENDPOINT = "PHOENIX_COLLECTOR_ENDPOINT" +""" +The endpoint traces and evals are sent to. This must be set if the Phoenix +server is running on a remote instance. +""" def _get_temp_path() -> Path: @@ -76,3 +81,7 @@ def get_env_port() -> int: def get_env_host() -> str: return os.getenv(ENV_PHOENIX_HOST) or HOST + + +def get_env_collector_endpoint() -> Optional[str]: + return os.getenv(ENV_PHOENIX_COLLECTOR_ENDPOINT) diff --git a/src/phoenix/trace/exporter.py b/src/phoenix/trace/exporter.py index f7b71fad4e..f67cd1adc1 100644 --- a/src/phoenix/trace/exporter.py +++ b/src/phoenix/trace/exporter.py @@ -11,7 +11,7 @@ from typing_extensions import TypeAlias import phoenix.trace.v1 as pb -from phoenix.config import get_env_host, get_env_port +from phoenix.config import get_env_collector_endpoint, get_env_host, get_env_port from phoenix.trace.schemas import Span from phoenix.trace.v1.utils import encode @@ -31,6 +31,7 @@ def export(self, _: Any) -> None: class HttpExporter: def __init__( self, + endpoint: Optional[str] = None, host: Optional[str] = None, port: Optional[int] = None, ) -> None: @@ -39,6 +40,11 @@ def __init__( Parameters ---------- + endpoint: Optional[str] + The endpoint of the Phoenix server (collector). This should be set if the Phoenix + server is running on a remote instance. It can also be set using environment + variable `PHOENIX_COLLECTOR_ENDPOINT`, otherwise it defaults to `http://127.0.0.1:6006` + Note, this parameter supersedes `host` and `port`. host: Optional[str] The host of the Phoenix server. It can also be set using environment variable `PHOENIX_HOST`, otherwise it defaults to `127.0.0.1`. @@ -48,7 +54,9 @@ def __init__( """ self._host = host or get_env_host() self._port = port or get_env_port() - self._base_url = f"http://{self._host}:{self._port}" + endpoint = endpoint or get_env_collector_endpoint() or f"http://{self._host}:{self._port}" + # Make sure the url does not end with a slash + self._base_url = endpoint.rstrip("/") self._warn_if_phoenix_is_not_running() self._session = Session() weakref.finalize(self, self._session.close) diff --git a/tests/trace/test_exporter.py b/tests/trace/test_exporter.py new file mode 100644 index 0000000000..0e3d0f45e6 --- /dev/null +++ b/tests/trace/test_exporter.py @@ -0,0 +1,17 @@ +import pytest +from phoenix.trace.exporter import HttpExporter + + +def test_exporter(monkeypatch: pytest.MonkeyPatch): + # Test that it defaults to local + exporter = HttpExporter() + assert exporter._base_url == "http://127.0.0.1:6006" + + # Test that you can configure an endpoint + exporter = HttpExporter(endpoint="https://my-phoenix-server.com/") + assert exporter._base_url == "https://my-phoenix-server.com" + + # Test that it supports environment variables + monkeypatch.setenv("PHOENIX_COLLECTOR_ENDPOINT", "https://my-phoenix-server.com/") + exporter = HttpExporter() + assert exporter._base_url == "https://my-phoenix-server.com"