跳转至

Temporal

概述

OpenSRE 会查询 Temporal 的 HTTP API,以获取工作流执行记录、事件历史、任务队列健康状况和命名空间级指标——帮助诊断工作流失败、活动重试和 worker 中断。

Note

OpenSRE 连接的是 Temporal 的 HTTP API(由 frontend 服务提供的 /api/v1/... REST 接口)。这是自托管服务器的功能,通过 --http-port 标志(开发服务器)或 frontend.httpPort 配置启用。

目前不支持 Temporal Cloud:Cloud 只为工作流数据暴露 gRPC/mTLS 端点,并为控制平面管理暴露一个 HTTP Ops API——两者都不是本集成所用的 frontend HTTP API。请将 OpenSRE 指向自托管的 Temporal 部署。

前置条件

  • 已启用 HTTP API 的自托管 Temporal Server
  • HTTP API 基础 URL(仅当你的部署需要 bearer 认证时才需要 API key)

Warning

端口 7233 是 gRPC frontend 端口,不能作为 base_url 使用——HTTP API 监听在另一个端口上。在开发服务器上,它通过 --http-port 设置(否则默认使用随机空闲端口)。下面的示例使用 7243。

设置

方式 1:Interactive CLI

opensre integrations setup temporal

提示输入 HTTP API 基础 URL、命名空间和可选的 API key。

方式 2:环境变量

export TEMPORAL_API_URL="http://localhost:7243"
export TEMPORAL_NAMESPACE="default"
export TEMPORAL_API_KEY=""   # 仅当你的部署需要 bearer 认证时
变量 默认值 描述
TEMPORAL_API_URL — 必填。 Temporal HTTP API 基础 URL(--http-port 监听端口,而非 gRPC 的 7233)
TEMPORAL_NAMESPACE default 要查询的命名空间
TEMPORAL_API_KEY — 可选的 bearer token(Authorization: Bearer <key>)

方式 3:持久化存储

{
  "version": 1,
  "integrations": [
    {
      "id": "temporal-prod",
      "service": "temporal",
      "status": "active",
      "credentials": {
        "base_url": "http://temporal-frontend:7243",
        "namespace": "default",
        "api_key": ""
      }
    }
  ]
}

凭据

将 base_url / TEMPORAL_API_URL 设置为 frontend 的 HTTP API 端点。对于未认证的集群,将 api_key 留空。

使用 Docker 进行快速本地测试

docker run -d --rm \
  --name temporal-dev \
  -p 127.0.0.1:7233:7233 \
  -p 127.0.0.1:8233:8233 \
  -p 127.0.0.1:7243:7243 \
  temporalio/temporal:latest \
  server start-dev \
    --ip 0.0.0.0 \
    --http-port 7243

i=0
until curl -sf -o /dev/null http://localhost:7243/api/v1/namespaces/default; do
  i=$((i + 1))
  [ "$i" -ge 30 ] && { echo "ERROR: Temporal never became ready" >&2; exit 1; }
  sleep 2
done
[ $i -lt 30 ] || { echo "ERROR: Temporal never became ready" >&2; exit 1; }
端口 用途
7233 gRPC frontend(SDK、temporal CLI)
8233 Web UI(http://localhost:8233)
7243 HTTP API——将其设置为 base_url

确认 HTTP API 有响应:

curl -s http://localhost:7243/api/v1/namespaces/default
export TEMPORAL_API_URL="http://localhost:7243"

验证:

opensre integrations verify temporal
SERVICE  │ SOURCE    │ STATUS   │ DETAIL
temporal │ local env │ ✓ passed │ Successfully connected to Temporal.

全新的命名空间没有工作流,单凭 temporal_namespace_info 的 workflow_count: "0" 并不构成有用的证据。请先运行一个真实、故意失败的工作流:

pip install temporalio
cat > worker.py << 'EOF'
import asyncio
from datetime import timedelta
from temporalio import activity, workflow
from temporalio.client import Client
from temporalio.common import RetryPolicy
from temporalio.worker import Worker

@activity.defn
async def deploy_payment_service() -> str:
    raise RuntimeError("payment-service deploy failed: connection refused to billing-api")

@workflow.defn
class PaymentServiceDeploy:
    @workflow.run
    async def run(self) -> str:
        return await workflow.execute_activity(
            deploy_payment_service,
            start_to_close_timeout=timedelta(seconds=5),
            retry_policy=RetryPolicy(maximum_attempts=1),
        )

async def main():
    client = await Client.connect("localhost:7233")
    worker = Worker(client, task_queue="payment-service-queue",
                     workflows=[PaymentServiceDeploy], activities=[deploy_payment_service])
    async with worker:
        try:
            await client.execute_workflow(PaymentServiceDeploy.run,
                id="payment-service-deploy-1", task_queue="payment-service-queue")
        except Exception as e:
            print("workflow failed:", e)

if __name__ == "__main__":
    asyncio.run(main())
EOF
python worker.py

只有当存储解析有某些内容可回退到环境变量时,聊天会话才会将集成视为已激活——一个未修改的 ~/.opensre/integrations.json 即使只包含一个无关的集成,也会完全阻止环境变量回退。请将 OPENSRE_INTEGRATIONS_STORE_PATH 指向一个空的、有效的存储,这样你的真实配置永远不会被读取或写入,并且上方的 TEMPORAL_API_URL 也是连接信息的唯一来源:

export OPENSRE_INTEGRATIONS_STORE_PATH=$(mktemp /tmp/opensre-temporal-demo.XXXXXX)
echo '{"version": 2, "integrations": []}' > "$OPENSRE_INTEGRATIONS_STORE_PATH"

纯零字节文件在这里无法使用——存储加载器期望有效的 JSON,在读取空文件时会抛错,因此这里写入的是一个显式空(但有效)的存储,而不是仅用 mktemp 创建文件。

现在向 agent 询问失败的工作流:

opensre

询问:Why did the Temporal workflow payment-service-deploy-1 fail?

针对这个确切的本地工作流,agent 会调用全部 4 个已注册工具—— namespace info、workflow list、workflow history 和 task queue——并找到 真正的原因:deploy_payment_service 活动抛出了 RuntimeError('payment-service deploy failed: connection refused to billing-api') ,并且由于 maximumAttempts=1,该失败直接传播为 WORKFLOW_EXECUTION_FAILED。空的存储会使会话对每一个集成都回退到 环境变量解析,而不仅仅是 Temporal——如果你的 shell 已经从其自身的环境变量解析出另一个集成,它也会一并被使用(无害)。

清理:

docker rm -f temporal-dev
rm -f "$OPENSRE_INTEGRATIONS_STORE_PATH"
unset OPENSRE_INTEGRATIONS_STORE_PATH TEMPORAL_API_URL

工具

工具 作用
temporal_namespace_info Namespace 状态以及按状态(Running、Failed、TimedOut)统计的工作流数量
temporal_workflows 最近的执行记录,包含状态、类型、任务队列、时间信息
temporal_workflow_history 指定执行的事件历史
temporal_task_queue 活跃的 poller 与积压统计(深度、添加/分发速率)

排查失败工作流时的典型流程

  1. Namespace 信息 —— 有多少工作流在运行、多少已失败?
  2. 工作流 —— 筛选出失败/超时的执行;记录工作流类型和任务队列
  3. 工作流历史 —— 哪个 activity 失败了,原因是什么?
  4. 任务队列 —— worker 是否在轮询?积压是否在增长?

验证

opensre integrations verify temporal

验证会探测 GET /api/v1/namespaces/{namespace}。

故障排查

现象 修复方法
连接被拒绝 / 协议错误 你可能在用 gRPC 端口 —— 请使用 HTTP API 端口(--http-port,例如 7243),而不是 7233
/api/v1/... 返回 404 启用 HTTP API(--http-port 或 frontend.httpPort)
401 Unauthorized 将 TEMPORAL_API_KEY 设置为有效的 bearer token
404 Namespace not found 确认 namespace 完全匹配(区分大小写)
工作流列表为空 工作流可能已超过保留期 —— 检查 namespace 的保留设置
任务队列上没有 poller worker 可能已下线 —— 检查 worker 部署的健康状况

安全

  • 在支持细粒度授权的部署中,请使用只读 API key —— OpenSRE 从不向 Temporal 写入数据。
  • 将对 HTTP API 的网络访问限制在可信 IP。
  • 将凭据存放在 ~/.opensre/integrations.json 或环境变量中,不要放在源代码里。

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 op/opensre