Skip to content

Commit

Permalink
feat: phoenix.otel infers GRPC port from env (#6017)
Browse files Browse the repository at this point in the history
* Infer GRPC port from env var if available

* Add duplicate config definitions
  • Loading branch information
anticorrelator authored Jan 14, 2025
1 parent c232a30 commit 4e036e7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/phoenix-otel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions packages/phoenix-otel/src/phoenix/otel/otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
*,
Expand Down Expand Up @@ -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

Expand All @@ -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 = {
Expand Down
17 changes: 17 additions & 0 deletions packages/phoenix-otel/src/phoenix/otel/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 "(),/:;<=>?@[\]{}
Expand Down

0 comments on commit 4e036e7

Please sign in to comment.