Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(traces): configurable endpoint for the exporter #1795

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/phoenix/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
12 changes: 10 additions & 2 deletions src/phoenix/trace/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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`.
Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions tests/trace/test_exporter.py
Original file line number Diff line number Diff line change
@@ -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"
Loading