import logging from langsmith import Client from langsmith.integrations.otel import configure from zbt.core.config import Settings logger = logging.getLogger(__name__) _otel_configured = False def configure_langsmith_otel(settings: Settings) -> bool: """将项目自定义工作流 Span 发送到当前 LangSmith 项目。""" global _otel_configured if _otel_configured: return True if ( not settings.langsmith_tracing or not settings.langsmith_otel_enabled or not settings.langsmith_api_key ): return False try: client = Client( api_url=settings.langsmith_endpoint, api_key=settings.langsmith_api_key, ) try: project = client.read_project( project_name=settings.langsmith_project, ) except Exception: project = client.create_project( settings.langsmith_project, description="智保通第三阶段Agent与LangGraph运行轨迹", upsert=True, ) # OTLP请求头必须是ASCII;官方接口也接受项目UUID作为目标标识。 _otel_configured = configure( api_key=settings.langsmith_api_key, project_name=str(project.id), ) except Exception: logger.exception("LangSmith OpenTelemetry项目解析失败") return False if not _otel_configured: logger.warning("LangSmith OpenTelemetry 初始化未生效") return _otel_configured