feat: Enable Tracing Support For Phoenix Cloud Instance (#23196)

This commit is contained in:
Ali Saleh
2025-07-31 03:58:26 +05:00
committed by GitHub
parent ffddabde43
commit 142ab74784
3 changed files with 22 additions and 8 deletions

View File

@@ -4,6 +4,7 @@ import logging
import os import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Any, Optional, Union, cast from typing import Any, Optional, Union, cast
from urllib.parse import urlparse
from openinference.semconv.trace import OpenInferenceSpanKindValues, SpanAttributes from openinference.semconv.trace import OpenInferenceSpanKindValues, SpanAttributes
from opentelemetry import trace from opentelemetry import trace
@@ -40,8 +41,14 @@ def setup_tracer(arize_phoenix_config: ArizeConfig | PhoenixConfig) -> tuple[tra
try: try:
# Choose the appropriate exporter based on config type # Choose the appropriate exporter based on config type
exporter: Union[GrpcOTLPSpanExporter, HttpOTLPSpanExporter] exporter: Union[GrpcOTLPSpanExporter, HttpOTLPSpanExporter]
# Inspect the provided endpoint to determine its structure
parsed = urlparse(arize_phoenix_config.endpoint)
base_endpoint = f"{parsed.scheme}://{parsed.netloc}"
path = parsed.path.rstrip("/")
if isinstance(arize_phoenix_config, ArizeConfig): if isinstance(arize_phoenix_config, ArizeConfig):
arize_endpoint = f"{arize_phoenix_config.endpoint}/v1" arize_endpoint = f"{base_endpoint}/v1"
arize_headers = { arize_headers = {
"api_key": arize_phoenix_config.api_key or "", "api_key": arize_phoenix_config.api_key or "",
"space_id": arize_phoenix_config.space_id or "", "space_id": arize_phoenix_config.space_id or "",
@@ -53,7 +60,7 @@ def setup_tracer(arize_phoenix_config: ArizeConfig | PhoenixConfig) -> tuple[tra
timeout=30, timeout=30,
) )
else: else:
phoenix_endpoint = f"{arize_phoenix_config.endpoint}/v1/traces" phoenix_endpoint = f"{base_endpoint}{path}/v1/traces"
phoenix_headers = { phoenix_headers = {
"api_key": arize_phoenix_config.api_key or "", "api_key": arize_phoenix_config.api_key or "",
"authorization": f"Bearer {arize_phoenix_config.api_key or ''}", "authorization": f"Bearer {arize_phoenix_config.api_key or ''}",

View File

@@ -87,7 +87,7 @@ class PhoenixConfig(BaseTracingConfig):
@field_validator("endpoint") @field_validator("endpoint")
@classmethod @classmethod
def endpoint_validator(cls, v, info: ValidationInfo): def endpoint_validator(cls, v, info: ValidationInfo):
return cls.validate_endpoint_url(v, "https://app.phoenix.arize.com") return validate_url_with_path(v, "https://app.phoenix.arize.com")
class LangfuseConfig(BaseTracingConfig): class LangfuseConfig(BaseTracingConfig):

View File

@@ -102,9 +102,14 @@ class TestPhoenixConfig:
assert config.project == "default" assert config.project == "default"
def test_endpoint_validation_with_path(self): def test_endpoint_validation_with_path(self):
"""Test endpoint validation normalizes URL by removing path""" """Test endpoint validation with path"""
config = PhoenixConfig(endpoint="https://custom.phoenix.com/api/v1") config = PhoenixConfig(endpoint="https://app.phoenix.arize.com/s/dify-integration")
assert config.endpoint == "https://custom.phoenix.com" assert config.endpoint == "https://app.phoenix.arize.com/s/dify-integration"
def test_endpoint_validation_without_path(self):
"""Test endpoint validation without path"""
config = PhoenixConfig(endpoint="https://app.phoenix.arize.com")
assert config.endpoint == "https://app.phoenix.arize.com"
class TestLangfuseConfig: class TestLangfuseConfig:
@@ -368,13 +373,15 @@ class TestConfigIntegration:
"""Test that URL normalization works consistently across configs""" """Test that URL normalization works consistently across configs"""
# Test that paths are removed from endpoints # Test that paths are removed from endpoints
arize_config = ArizeConfig(endpoint="https://arize.com/api/v1/test") arize_config = ArizeConfig(endpoint="https://arize.com/api/v1/test")
phoenix_config = PhoenixConfig(endpoint="https://phoenix.com/api/v2/") phoenix_with_path_config = PhoenixConfig(endpoint="https://app.phoenix.arize.com/s/dify-integration")
phoenix_without_path_config = PhoenixConfig(endpoint="https://app.phoenix.arize.com")
aliyun_config = AliyunConfig( aliyun_config = AliyunConfig(
license_key="test_license", endpoint="https://tracing-analysis-dc-hz.aliyuncs.com/api/v1/traces" license_key="test_license", endpoint="https://tracing-analysis-dc-hz.aliyuncs.com/api/v1/traces"
) )
assert arize_config.endpoint == "https://arize.com" assert arize_config.endpoint == "https://arize.com"
assert phoenix_config.endpoint == "https://phoenix.com" assert phoenix_with_path_config.endpoint == "https://app.phoenix.arize.com/s/dify-integration"
assert phoenix_without_path_config.endpoint == "https://app.phoenix.arize.com"
assert aliyun_config.endpoint == "https://tracing-analysis-dc-hz.aliyuncs.com" assert aliyun_config.endpoint == "https://tracing-analysis-dc-hz.aliyuncs.com"
def test_project_default_values(self): def test_project_default_values(self):