Agent SRE × OpenLit 集成指南:用 OpenTelemetry 为 AI Agent 采集 SLO、混沌实验与错误预算可观测性数据 📅 发布时间:2026/9/19 10:41:19 👁 浏览次数: Agent SRE × OpenLit 集成指南用 OpenTelemetry 为 AI Agent 采集 SLO、混沌实验与错误预算可观测性数据【免费下载链接】agent-governance-toolkitAI Agent Governance Toolkit — Policy enforcement, zero-trust identity, execution sandboxing, and reliability engineering for autonomous AI agents. Covers 10/10 OWASP Agentic Top 10.项目地址: https://gitcode.com/GitHub_Trending/ag/agent-governance-toolkit导读本指南讲解 Agent Governance Toolkit 中 Agent SRE 组件与 OpenLit 的 OpenTelemetryOTel原生集成方案核心内容源自仓库提案文档 OPENLIT-INTEGRATION-PROPOSAL.md。你将掌握Agent SRE 的 SLO/SLI 评估、错误预算消耗、混沌实验全生命周期如何被转换为 OTel span 与指标并送入 OpenLitOpenLitExporter便捷导出器的完整配置与调用方式以及这些遥测数据在 SLO 健康看板、混沌实验追踪、错误预算消费跟踪、金丝雀发布等场景中的落地用法。读完本文你可以直接把 Agent SRE 的可靠性遥测接入任意 OTLP 兼容后端。Agent SRE 是什么AI 原生的 SRE 纪律层Agent SRE源码位于 agent-governance-python/agent-sre是 Agent Governance Toolkit 面向自主 AI Agent 的可靠性工程框架核心能力包括SLO/SLI 追踪定义 Agent 的可靠性目标并持续评估agent_sre.slo.objectives.SLO、agent_sre.slo.indicators.SLI混沌测试Chaos Testing对 Agent 注入延迟、错误等故障并度量韧性agent_sre.chaos.engine错误预算Error Budget管理跟踪好/坏事件消耗决定该发布新功能还是先修可靠性agent_sre.slo.objectives.ErrorBudget此外还有告警去重、故障注入调度、事件响应 Runbook、成本治理、金丝雀/蓝绿发布、回放与黄金轨迹等模块。OpenLit 是 OpenTelemetry 原生的可观测性平台内置 50 LLM 提供商集成。两者的结合点在于OpenLit 擅长展示 LLM 调用轨迹与 Token/成本指标而 Agent SRE 补齐了SRE 纪律层——把 SLO 健康度、混沌韧性、错误预算消耗与 LLM 轨迹并排展示让开发者同时看到Agent 在做什么和Agent 有多可靠。注提案中提及的上游 OpenLit 侧 Issue/PRopenlit#1003、PR #1037/#1062属于外部仓库事项本文以当前仓库中实际落地、可验证的OpenLitExporter实现与 OTel 约定为准。集成架构导入时 Monkey-Patch业务零侵入提案给出的架构设计是自动埋点auto-instrumentationAgent SRE 本身已经原生导出 OTel 指标与 trace见 integrations/otel 下的metrics.py、traces.py、conventions.pyOpenLit 侧的 instrumentor 使用wrapt包装核心方法与 CrewAI、LangChain 等同一模式在openlit.init()被调用时自动捕获 SRE 遥测Agent SRE 代码中无需任何 OpenLit 专有代码——埋点发生在 import 时对SLO.evaluate()、ErrorBudget.record_event()、ChaosExperiment.start()/complete()/abort()、SLI.record()等方法打补丁。提案中规划的 instrumentor 文件结构对应 OpenLit 仓库侧实现文件用途instrumentation/agent_sre/__init__.pyAgentSREInstrumentor类继承BaseInstrumentorinstrumentation/agent_sre/agent_sre.py覆盖全部操作的 5 个同步 wrapperinstrumentation/agent_sre/utils.pySpan 属性辅助函数与指标记录_instrumentors.py在MODULE_NAME_MAP与INSTRUMENTOR_MAP中注册agent-sre被插桩的操作与 Span 命名操作Span 名称关键属性SLO.evaluate()slo.evaluate {name}status、error_budget_remaining、burn_rate、is_exhaustedErrorBudget.record_event()error_budget.record {good\|bad}total、consumed、remaining_percentChaosExperiment.start()chaos.start {name}target_agent、fault_count、blast_radiusChaosExperiment.complete()chaos.complete {name}resilience_score、resilience_passed、durationChaosExperiment.abort()chaos.abort {name}abort_reason、injection_countSLI.record()详细模式sli.record {name}value、target、windowOTel 语义约定agent.sre.*前缀所有指标以agent.sre.*为前缀避免与标准 OTel 约定冲突见 conventions.pyagent.sre.sli.value— SLI 测量值agent.sre.burn_rate— SLO 消耗速率1.0 恰好按预期速率消耗agent.sre.chaos.resilience_score— 混沌实验韧性分数agent.sre.chaos.experiment_id— 实验标识符。此外约定中还有agent.sre.slo.status_code0healthy、1warning、2critical、3exhausted、-1unknown、agent.sre.error_budget.remaining、agent.sre.chaos.experiment_name、agent.sre.chaos.fault_type、agent.sre.chaos.fault_target、agent.sre.chaos.state等属性键。SLO 状态码映射定义在 metrics.py 的SLO_STATUS_CODES。本地便捷导出器OpenLitExporter除了自动埋点仓库还提供了开箱即用的OpenLitExporter便捷类实现于 agent_sre/integrations/openlit.py它会预先为 OpenLit 的 OTLP 端点配置好 OTel SDK同时支持 gRPC 4317 与 HTTP 4318 端口。快速上手from agent_sre.integrations.openlit import OpenLitExporter exporter OpenLitExporter( endpointhttp://localhost:4318, service_namemy-ai-service ) exporter.record_slo(latency-p99, slo_result) exporter.record_chaos_experiment(failover-test, experiment)构造函数参数参数默认值说明endpointhttp://localhost:4318OpenLit 的 OTLP 端点HTTP 协议/v1/traces与/v1/metrics路径由内部拼接service_nameagent-sre写入service.name资源属性api_keyNone若提供会以Authorization: Bearer api_key请求头发送environmentdefault写入deployment.environment资源属性application_namedefault写入application.name资源属性底层初始化逻辑_setup_otel()完成三件事openlit.pyTrace 链路创建OTLPSpanExporter(endpointf{endpoint}/v1/traces)挂载BatchSpanProcessor构建TracerProvider并取得 traceragent_sre.openlitMetric 链路创建OTLPMetricExporter(endpointf{endpoint}/v1/metrics)通过PeriodicExportingMetricReader默认每 10 秒导出一次export_interval_millis10000挂载MeterProvider复用 Agent SRE 自带导出器在配置好的 provider 之上创建 MetricsExporter 与 TraceExporter对外暴露exporter.metrics与exporter.traces两个属性。依赖前置条件若未安装 OTel HTTP 导出器构造函数会抛出带指引的ImportErrorpip install agent-sre[otel]。OTel 依赖版本约束见 pyproject.tomlopentelemetry-api/sdk/exporter-otlp-proto-grpc1.44.0,2.0等。record_sloSLO 状态 全部 SLI 明细exporter.record_slo(slo)该方法openlit.py执行调用slo.evaluate()获取当前状态healthy/warning/critical/exhausted/unknown判定逻辑见 objectives.py先看错误预算是否耗尽再看是否有 critical/warning 级燃烧率告警触发最后看是否无任何 SLI 数据通过metrics.record_slo上报slo_name、status映射为状态码、error_budget_remaining、burn_rate与labels遍历slo.indicators对每个有当前值的 SLI 调用metrics.record_sli上报sli_name、value、target、window、compliance。容错细节当 SLI 尚无任何测量数据current_value()为None时该 SLI 不会被记录——这一点由测试 test_openlit_integration.py 显式断言避免把空数据当 0 上报误导告警。record_chaos_experiment混沌实验落成 Span 韧性指标exporter.record_chaos_experiment(experiment)该方法openlit.py将一个ChaosExperiment对象转换为一条 OTel Span名称chaos.{experiment.name}属性包括agent.sre.chaos.experiment_id、agent.sre.chaos.experiment_nameagent.id目标 Agentagent.sre.chaos.state、agent.sre.chaos.duration_seconds、agent.sre.chaos.blast_radiusagent.sre.chaos.fault_count故障数量、agent.sre.chaos.injection_count注入事件数agent.sre.chaos.resilience_score、agent.sre.chaos.resilience_passed首个故障的类型与目标agent.sre.chaos.fault_type/agent.sre.chaos.fault_target以及可选的agent.sre.chaos.abort_reasonSpan 状态映射completed→OKaborted/failed→ERROR附原因一条韧性指标通过metrics.record_resilience记录agent.sre.chaos.resilience_score。ChaosExperiment的核心状态机定义在 chaos/engine.pyExperimentState枚举及start()/abort(reason)/complete(resilience)方法Fault.latency_injection()、Fault.error_injection()等工厂方法可快速构造故障。测试 test_openlit_integration.py 覆盖了完成态成功 span 韧性指标与中止态 ERROR span abort_reason两种路径。完整示例定义 SLO 并导出结合 objectives.py 与 indicators.py 的类型一个端到端用法如下from agent_sre.integrations.openlit import OpenLitExporter from agent_sre.slo.indicators import TaskSuccessRate, TimeWindow from agent_sre.slo.objectives import ErrorBudget, SLO from agent_sre.chaos.engine import ChaosExperiment, Fault, ResilienceScore # 1) 构造 SLI目标 99%24 小时窗口并记录事件 sli TaskSuccessRate(target0.99, windowTimeWindow.DAY_1) sli.record(True); sli.record(True); sli.record(False) # 2) 构造 SLO 与错误预算总预算 1 - target 1% budget ErrorBudget(total0.01, consumed0.003) slo SLO(namelatency-p99, indicators[sli], error_budgetbudget, labels{team: platform}) # 3) 导出到 OpenLit exporter OpenLitExporter(endpointhttp://localhost:4318, service_namemy-ai-service) exporter.record_slo(slo) # 4) 混沌实验注入 5s 延迟完成并导出 exp ChaosExperiment(namefailover-test, target_agentbot-1, faults[Fault.latency_injection(openai, delay_ms5000)], duration_seconds60) exp.start() exp.complete(ResilienceScore(overall85.0, passedTrue)) exporter.record_chaos_experiment(exp) # 5) 结束前刷新并关闭导出器 exporter.shutdown() # force_flush shutdown tracer/meter providerErrorBudget的关键行为objectives.py值得注意事件保存在有界deque(maxlen100_000)中防止内存无限增长remaining返回 0.01.0 的剩余预算比例burn_rate()默认按 1 小时窗口计算实际错误率 ÷ 允许错误率并支持 warning默认 2.0/critical默认 10.0两级燃烧率告警。典型使用场景集成完成后OpenLit 用户可以针对 Agent 系统构建四类视图SLO 健康看板SLO Health Dashboards——实时展示错误预算燃烧率与 SLI 合规率一眼看出哪些 Agent 正在逼近违约混沌实验追踪Chaos Experiment Traces——故障注入事件、韧性分数、中止条件与它们所影响的 LLM 调用同屏展示把注入的故障与观测到的劣化因果对齐错误预算消费跟踪Error Budget Consumption——按 good/bad 事件追踪预算消耗为发布新功能 vs 专注可靠性的决策提供数据依据金丝雀指标Canary Metrics——渐进式上线新 LLM 模型/提示词时对比金丝雀与基线版本的表现。验证与测试依据OpenLit 集成有一组完整的单元测试覆盖test_openlit_integration.py可作为行为契约初始化未安装 OTel HTTP 导出器时抛出带安装指引的ImportErrorrecord_slo断言metrics.record_slo收到正确的slo_name与labelsmetrics.record_sli收到sli_name如task_success_raterecord_chaos_experiment完成态断言 span 名为chaos.latency-test属性含agent.idbot-1、resilience_score85.0、resilience_passedTrue且metrics.record_resilience被正确调用record_chaos_experiment中止态断言abort_reason与stateaborted写入 span 属性无数据容错SLI 无测量数据时不记录shutdownforce_flush()与shutdown()被依次调用。扩展阅读提案原文docs/proposals/OPENLIT-INTEGRATION-PROPOSAL.md导出器实现agent_sre/integrations/openlit.pyOTel 语义约定integrations/otel/conventions.pyOTel 指标导出器integrations/otel/metrics.pyOTel Trace 导出器integrations/otel/traces.pySLO/错误预算引擎slo/objectives.py、slo/indicators.py混沌实验引擎chaos/engine.py集成测试tests/test_openlit_integration.py依赖约束agent-sre/pyproject.toml【免费下载链接】agent-governance-toolkitAI Agent Governance Toolkit — Policy enforcement, zero-trust identity, execution sandboxing, and reliability engineering for autonomous AI agents. Covers 10/10 OWASP Agentic Top 10.项目地址: https://gitcode.com/GitHub_Trending/ag/agent-governance-toolkit创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考