diff --git a/packages/phoenix-otel/README.md b/packages/phoenix-otel/README.md index c5977a2e04..8ff5d4eba3 100644 --- a/packages/phoenix-otel/README.md +++ b/packages/phoenix-otel/README.md @@ -7,6 +7,7 @@ These defaults are aware of environment variables you may have set to configure - `PHOENIX_PROJECT_NAME` - `PHOENIX_CLIENT_HEADERS` - `PHOENIX_API_KEY` +- `PHOENIX_GRPC_PORT` # Examples @@ -52,7 +53,8 @@ tracer_provider = register() When passing in the `endpoint` argument, **you must specify the fully qualified endpoint**. For example, in order to export spans via HTTP to localhost, use Pheonix's HTTP collector endpoint: -`http://localhost:6006/v1/traces`. The gRPC endpoint is different: `http://localhost:4317`. +`http://localhost:6006/v1/traces`. The default gRPC endpoint is different: `http://localhost:4317`. +If the `PHOENIX_GRPC_PORT` environment variable is set, it will override the default gRPC port. ``` from phoenix.otel import register diff --git a/packages/phoenix-otel/src/phoenix/otel/otel.py b/packages/phoenix-otel/src/phoenix/otel/otel.py index 051afa3d4b..c794e5121f 100644 --- a/packages/phoenix-otel/src/phoenix/otel/otel.py +++ b/packages/phoenix-otel/src/phoenix/otel/otel.py @@ -23,14 +23,13 @@ from .settings import ( get_env_client_headers, get_env_collector_endpoint, + get_env_grpc_port, get_env_phoenix_auth_header, get_env_project_name, ) PROJECT_NAME = _ResourceAttributes.PROJECT_NAME -_DEFAULT_GRPC_PORT = 4317 - def register( *, @@ -388,7 +387,7 @@ def _maybe_http_endpoint(parsed_endpoint: ParseResult) -> bool: def _maybe_grpc_endpoint(parsed_endpoint: ParseResult) -> bool: - if not parsed_endpoint.path and parsed_endpoint.port == 4317: + if not parsed_endpoint.path and parsed_endpoint.port == get_env_grpc_port(): return True return False @@ -413,7 +412,7 @@ def _construct_http_endpoint(parsed_endpoint: ParseResult) -> ParseResult: def _construct_grpc_endpoint(parsed_endpoint: ParseResult) -> ParseResult: - return parsed_endpoint._replace(netloc=f"{parsed_endpoint.hostname}:{_DEFAULT_GRPC_PORT}") + return parsed_endpoint._replace(netloc=f"{parsed_endpoint.hostname}:{get_env_grpc_port()}") _KNOWN_PROVIDERS = { diff --git a/packages/phoenix-otel/src/phoenix/otel/settings.py b/packages/phoenix-otel/src/phoenix/otel/settings.py index 285bbb3f59..d679dbb317 100644 --- a/packages/phoenix-otel/src/phoenix/otel/settings.py +++ b/packages/phoenix-otel/src/phoenix/otel/settings.py @@ -8,10 +8,16 @@ # Environment variables specific to the subpackage ENV_PHOENIX_COLLECTOR_ENDPOINT = "PHOENIX_COLLECTOR_ENDPOINT" +ENV_PHOENIX_GRPC_PORT = "PHOENIX_GRPC_PORT" ENV_PHOENIX_PROJECT_NAME = "PHOENIX_PROJECT_NAME" ENV_PHOENIX_CLIENT_HEADERS = "PHOENIX_CLIENT_HEADERS" ENV_PHOENIX_API_KEY = "PHOENIX_API_KEY" +GRPC_PORT = 4317 +"""The port the gRPC server will run on after launch_app is called. +The default network port for OTLP/gRPC is 4317. +See https://opentelemetry.io/docs/specs/otlp/#otlpgrpc-default-port""" + def get_env_collector_endpoint() -> Optional[str]: return os.getenv(ENV_PHOENIX_COLLECTOR_ENDPOINT) @@ -35,6 +41,17 @@ def get_env_phoenix_auth_header() -> Optional[Dict[str, str]]: return None +def get_env_grpc_port() -> int: + if not (port := os.getenv(ENV_PHOENIX_GRPC_PORT)): + return GRPC_PORT + if port.isnumeric(): + return int(port) + raise ValueError( + f"Invalid value for environment variable {ENV_PHOENIX_GRPC_PORT}: " + f"{port}. Value must be an integer." + ) + + # Optional whitespace _OWS = r"[ \t]*" # A key contains printable US-ASCII characters except: SP and "(),/:;<=>?@[\]{}