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: at exporter initialization, warn user if Phoenix is not running #1535

Merged
merged 6 commits into from
Oct 2, 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
8 changes: 7 additions & 1 deletion src/phoenix/server/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import FileResponse, Response
from starlette.responses import FileResponse, PlainTextResponse, Response
from starlette.routing import Mount, Route, WebSocketRoute
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
Expand All @@ -18,6 +18,7 @@
from strawberry.asgi import GraphQL
from strawberry.schema import BaseSchema

import phoenix
from phoenix.config import SERVER_DIR
from phoenix.core.model_schema import Model
from phoenix.core.traces import Traces
Expand Down Expand Up @@ -122,6 +123,10 @@ async def get(self, request: Request) -> FileResponse:
)


async def version(_: Request) -> PlainTextResponse:
return PlainTextResponse(f"{phoenix.__version__}")


def create_app(
export_path: Path,
model: Model,
Expand Down Expand Up @@ -157,6 +162,7 @@ def create_app(
]
)
+ [
Route("/arize_phoenix_version", version),
RogerHYang marked this conversation as resolved.
Show resolved Hide resolved
Route(
"/exports",
type(
Expand Down
26 changes: 25 additions & 1 deletion src/phoenix/trace/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from types import MethodType
from typing import Optional

import requests
from requests import Session

from phoenix.config import get_env_host, get_env_port
Expand All @@ -27,9 +28,23 @@ def __init__(
host: Optional[str] = None,
port: Optional[int] = None,
) -> None:
"""
Span Exporter using HTTP.

Parameters
----------
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`.
port: Optional[int]
The port of the Phoenix server. It can also be set using environment
variable `PHOENIX_PORT`, otherwise it defaults to `6060`.
"""
self._host = host or get_env_host()
self._port = port or get_env_port()
self._url = f"http://{self._host}:{self._port}/v1/spans"
self._base_url = f"http://{self._host}:{self._port}"
self._warn_if_phoenix_is_not_running()
self._url = f"{self._base_url}/v1/spans"
self._session = Session()
weakref.finalize(self, self._session.close)
self._session.headers.update(
Expand Down Expand Up @@ -69,3 +84,12 @@ def _send(self, span: Span) -> None:
self._session.post(self._url, data=data)
except Exception as e:
logger.exception(e)

def _warn_if_phoenix_is_not_running(self) -> None:
try:
requests.get(f"{self._base_url}/arize_phoenix_version").raise_for_status()
except Exception:
logger.warning(
f"Arize Phoenix is not running on {self._base_url}. Launch Phoenix "
f"with `import phoenix as px; px.launch_app()`"
)
4 changes: 2 additions & 2 deletions src/phoenix/trace/llama_index/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ class OpenInferenceTraceCallbackHandler(BaseCallbackHandler):
def __init__(
self,
callback: Optional[Callable[[List[Span]], None]] = None,
exporter: Optional[SpanExporter] = HttpExporter(),
exporter: Optional[SpanExporter] = None,
) -> None:
super().__init__(event_starts_to_ignore=[], event_ends_to_ignore=[])
self._tracer = Tracer(on_append=callback, exporter=exporter)
self._tracer = Tracer(on_append=callback, exporter=exporter or HttpExporter())
self._event_id_to_event_data: EventData = defaultdict(lambda: CBEventData())

def on_event_start(
Expand Down
Loading