2025-05-13 09:13:25 +08:00
|
|
|
from enum import StrEnum
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
from pydantic import BaseModel, ValidationInfo, field_validator
|
|
|
|
|
2025-07-07 09:34:13 +08:00
|
|
|
from core.ops.utils import validate_project_name, validate_url, validate_url_with_path
|
|
|
|
|
2024-06-26 17:33:29 +08:00
|
|
|
|
2025-05-13 09:13:25 +08:00
|
|
|
class TracingProviderEnum(StrEnum):
|
2025-07-03 10:52:14 +05:00
|
|
|
ARIZE = "arize"
|
|
|
|
PHOENIX = "phoenix"
|
2024-09-10 17:00:20 +08:00
|
|
|
LANGFUSE = "langfuse"
|
|
|
|
LANGSMITH = "langsmith"
|
2025-01-13 10:41:44 +01:00
|
|
|
OPIK = "opik"
|
2025-04-26 16:58:30 +05:30
|
|
|
WEAVE = "weave"
|
2025-07-04 21:54:33 +08:00
|
|
|
ALIYUN = "aliyun"
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BaseTracingConfig(BaseModel):
|
|
|
|
"""
|
2025-07-07 09:34:13 +08:00
|
|
|
Base model class for tracing configurations
|
2024-06-26 17:33:29 +08:00
|
|
|
"""
|
2024-09-10 17:00:20 +08:00
|
|
|
|
2025-07-07 09:34:13 +08:00
|
|
|
@classmethod
|
|
|
|
def validate_endpoint_url(cls, v: str, default_url: str) -> str:
|
|
|
|
"""
|
|
|
|
Common endpoint URL validation logic
|
|
|
|
|
|
|
|
Args:
|
|
|
|
v: URL value to validate
|
|
|
|
default_url: Default URL to use if input is None or empty
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Validated and normalized URL
|
|
|
|
"""
|
|
|
|
return validate_url(v, default_url)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def validate_project_field(cls, v: str, default_name: str) -> str:
|
|
|
|
"""
|
|
|
|
Common project name validation logic
|
|
|
|
|
|
|
|
Args:
|
|
|
|
v: Project name to validate
|
|
|
|
default_name: Default name to use if input is None or empty
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Validated project name
|
|
|
|
"""
|
|
|
|
return validate_project_name(v, default_name)
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
|
2025-07-03 10:52:14 +05:00
|
|
|
class ArizeConfig(BaseTracingConfig):
|
|
|
|
"""
|
|
|
|
Model class for Arize tracing config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
api_key: str | None = None
|
|
|
|
space_id: str | None = None
|
|
|
|
project: str | None = None
|
|
|
|
endpoint: str = "https://otlp.arize.com"
|
|
|
|
|
|
|
|
@field_validator("project")
|
|
|
|
@classmethod
|
|
|
|
def project_validator(cls, v, info: ValidationInfo):
|
2025-07-07 09:34:13 +08:00
|
|
|
return cls.validate_project_field(v, "default")
|
2025-07-03 10:52:14 +05:00
|
|
|
|
|
|
|
@field_validator("endpoint")
|
|
|
|
@classmethod
|
|
|
|
def endpoint_validator(cls, v, info: ValidationInfo):
|
2025-07-07 09:34:13 +08:00
|
|
|
return cls.validate_endpoint_url(v, "https://otlp.arize.com")
|
2025-07-03 10:52:14 +05:00
|
|
|
|
|
|
|
|
|
|
|
class PhoenixConfig(BaseTracingConfig):
|
|
|
|
"""
|
|
|
|
Model class for Phoenix tracing config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
api_key: str | None = None
|
|
|
|
project: str | None = None
|
|
|
|
endpoint: str = "https://app.phoenix.arize.com"
|
|
|
|
|
|
|
|
@field_validator("project")
|
|
|
|
@classmethod
|
|
|
|
def project_validator(cls, v, info: ValidationInfo):
|
2025-07-07 09:34:13 +08:00
|
|
|
return cls.validate_project_field(v, "default")
|
2025-07-03 10:52:14 +05:00
|
|
|
|
|
|
|
@field_validator("endpoint")
|
|
|
|
@classmethod
|
|
|
|
def endpoint_validator(cls, v, info: ValidationInfo):
|
2025-07-07 09:34:13 +08:00
|
|
|
return cls.validate_endpoint_url(v, "https://app.phoenix.arize.com")
|
2025-07-03 10:52:14 +05:00
|
|
|
|
|
|
|
|
2024-06-26 17:33:29 +08:00
|
|
|
class LangfuseConfig(BaseTracingConfig):
|
|
|
|
"""
|
|
|
|
Model class for Langfuse tracing config.
|
|
|
|
"""
|
2024-09-10 17:00:20 +08:00
|
|
|
|
2024-06-26 17:33:29 +08:00
|
|
|
public_key: str
|
|
|
|
secret_key: str
|
2024-09-10 17:00:20 +08:00
|
|
|
host: str = "https://api.langfuse.com"
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
@field_validator("host")
|
2024-09-11 16:40:52 +08:00
|
|
|
@classmethod
|
2025-07-07 09:34:13 +08:00
|
|
|
def host_validator(cls, v, info: ValidationInfo):
|
|
|
|
return cls.validate_endpoint_url(v, "https://api.langfuse.com")
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class LangSmithConfig(BaseTracingConfig):
|
|
|
|
"""
|
|
|
|
Model class for Langsmith tracing config.
|
|
|
|
"""
|
2024-09-10 17:00:20 +08:00
|
|
|
|
2024-06-26 17:33:29 +08:00
|
|
|
api_key: str
|
|
|
|
project: str
|
2024-09-10 17:00:20 +08:00
|
|
|
endpoint: str = "https://api.smith.langchain.com"
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
@field_validator("endpoint")
|
2024-09-11 16:40:52 +08:00
|
|
|
@classmethod
|
2025-07-07 09:34:13 +08:00
|
|
|
def endpoint_validator(cls, v, info: ValidationInfo):
|
|
|
|
# LangSmith only allows HTTPS
|
|
|
|
return validate_url(v, "https://api.smith.langchain.com", allowed_schemes=("https",))
|
2024-11-08 14:43:47 +08:00
|
|
|
|
|
|
|
|
2025-01-13 10:41:44 +01:00
|
|
|
class OpikConfig(BaseTracingConfig):
|
|
|
|
"""
|
|
|
|
Model class for Opik tracing config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
api_key: str | None = None
|
|
|
|
project: str | None = None
|
|
|
|
workspace: str | None = None
|
|
|
|
url: str = "https://www.comet.com/opik/api/"
|
|
|
|
|
|
|
|
@field_validator("project")
|
|
|
|
@classmethod
|
|
|
|
def project_validator(cls, v, info: ValidationInfo):
|
2025-07-07 09:34:13 +08:00
|
|
|
return cls.validate_project_field(v, "Default Project")
|
2025-01-13 10:41:44 +01:00
|
|
|
|
|
|
|
@field_validator("url")
|
|
|
|
@classmethod
|
|
|
|
def url_validator(cls, v, info: ValidationInfo):
|
2025-07-07 09:34:13 +08:00
|
|
|
return validate_url_with_path(v, "https://www.comet.com/opik/api/", required_suffix="/api/")
|
2025-01-13 10:41:44 +01:00
|
|
|
|
|
|
|
|
2025-04-26 16:58:30 +05:30
|
|
|
class WeaveConfig(BaseTracingConfig):
|
|
|
|
"""
|
|
|
|
Model class for Weave tracing config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
api_key: str
|
|
|
|
entity: str | None = None
|
|
|
|
project: str
|
|
|
|
endpoint: str = "https://trace.wandb.ai"
|
2025-06-07 20:36:23 +05:30
|
|
|
host: str | None = None
|
2025-04-26 16:58:30 +05:30
|
|
|
|
|
|
|
@field_validator("endpoint")
|
|
|
|
@classmethod
|
2025-07-07 09:34:13 +08:00
|
|
|
def endpoint_validator(cls, v, info: ValidationInfo):
|
|
|
|
# Weave only allows HTTPS for endpoint
|
|
|
|
return validate_url(v, "https://trace.wandb.ai", allowed_schemes=("https",))
|
2025-04-26 16:58:30 +05:30
|
|
|
|
2025-06-07 20:36:23 +05:30
|
|
|
@field_validator("host")
|
|
|
|
@classmethod
|
2025-07-07 09:34:13 +08:00
|
|
|
def host_validator(cls, v, info: ValidationInfo):
|
|
|
|
if v is not None and v.strip() != "":
|
|
|
|
return validate_url(v, v, allowed_schemes=("https", "http"))
|
2025-06-07 20:36:23 +05:30
|
|
|
return v
|
|
|
|
|
2025-04-26 16:58:30 +05:30
|
|
|
|
2025-07-04 21:54:33 +08:00
|
|
|
class AliyunConfig(BaseTracingConfig):
|
|
|
|
"""
|
|
|
|
Model class for Aliyun tracing config.
|
|
|
|
"""
|
|
|
|
|
|
|
|
app_name: str = "dify_app"
|
|
|
|
license_key: str
|
|
|
|
endpoint: str
|
|
|
|
|
2025-07-08 09:32:30 +08:00
|
|
|
@field_validator("app_name")
|
|
|
|
@classmethod
|
|
|
|
def app_name_validator(cls, v, info: ValidationInfo):
|
|
|
|
return cls.validate_project_field(v, "dify_app")
|
|
|
|
|
|
|
|
@field_validator("license_key")
|
|
|
|
@classmethod
|
|
|
|
def license_key_validator(cls, v, info: ValidationInfo):
|
|
|
|
if not v or v.strip() == "":
|
|
|
|
raise ValueError("License key cannot be empty")
|
|
|
|
return v
|
|
|
|
|
|
|
|
@field_validator("endpoint")
|
|
|
|
@classmethod
|
|
|
|
def endpoint_validator(cls, v, info: ValidationInfo):
|
|
|
|
return cls.validate_endpoint_url(v, "https://tracing-analysis-dc-hz.aliyuncs.com")
|
|
|
|
|
2025-07-04 21:54:33 +08:00
|
|
|
|
2024-11-08 14:43:47 +08:00
|
|
|
OPS_FILE_PATH = "ops_trace/"
|
|
|
|
OPS_TRACE_FAILED_KEY = "FAILED_OPS_TRACE"
|