Files
materials/knowledge/sre-learning-path/modules/s2-01-metrics/index.html

201 lines
20 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="zh-CN" data-theme="light">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>指标与 Prometheus · 阶段 2 模块 1 · SRE 从零到成熟</title>
<meta name="description" content="指标与 Prometheus 模块目录:共 14 章、53 个小节。">
<link rel="stylesheet" href="../../assets/doc.css">
</head>
<body>
<header class="topbar">
<div class="tb-in">
<a class="brand" href="../../index.html"><span class="logo">SRE</span><div>从零到成熟<small>模块详解</small></div></a>
<div class="crumbs">
<a href="../../index.html">学习路径</a><span class="sep">/</span>
<a href="../../index.html#s2">阶段 2 · 看得见</a><span class="sep">/</span>
<b>指标与 Prometheus</b>
</div>
<div class="spacer"></div>
<button class="btn" id="tocToggle">☰ 导航</button>
<button class="btn" id="themeBtn" title="切换到深色主题">深色</button>
</div>
</header>
<div class="layout" id="layout">
<aside class="sidebar">
<h2>章节目录</h2>
<nav id="chapters"><a class="toc-a" href="01-why.html"><i class="cn">1</i>为什么指标是可观测性的第一支柱</a><div class="subnav"><a class="toc-a lv3" href="01-why.html#sec-1-1">指标能回答而日志回答不了的问题</a><a class="toc-a lv3" href="01-why.html#sec-1-2">三支柱的分工:别让一种数据干三种活</a><a class="toc-a lv3" href="01-why.html#sec-1-3">成本模型:为什么指标可以全量长期保留</a></div><a class="toc-a" href="02-method.html"><i class="cn">2</i>方法论:四大黄金信号、USE 与 RED</a><div class="subnav"><a class="toc-a lv3" href="02-method.html#sec-2-1">四大黄金信号:先分清面向谁</a><a class="toc-a lv3" href="02-method.html#sec-2-2">RED:一个服务的最小可观测集合</a><a class="toc-a lv3" href="02-method.html#sec-2-3">USE:每个资源都要问三个问题</a><a class="toc-a lv3" href="02-method.html#sec-2-4">三个框架怎么一起用</a></div><a class="toc-a" href="03-model.html"><i class="cn">3</i>数据模型:时间序列、标签与基数陷阱</a><div class="subnav"><a class="toc-a lv3" href="03-model.html#sec-3-1">一条时间序列的完整身份</a><a class="toc-a lv3" href="03-model.html#sec-3-2">基数(cardinality)是怎么被撑爆的</a><a class="toc-a lv3" href="03-model.html#sec-3-3">标签设计规则:先写指标字典,再写代码</a></div><a class="toc-a" href="04-types.html"><i class="cn">4</i>指标类型:counter / gauge / histogram / summary</a><div class="subnav"><a class="toc-a lv3" href="04-types.html#sec-4-1">counter:只增不减,必须用 rate 读</a><a class="toc-a lv3" href="04-types.html#sec-4-2">gauge:可增可减,直接读</a><a class="toc-a lv3" href="04-types.html#sec-4-3">histogram:服务端分桶,能聚合的分位数</a><a class="toc-a lv3" href="04-types.html#sec-4-4">summary:客户端算分位数,聚合即失真</a></div><a class="toc-a" href="05-promql.html"><i class="cn">5</i>PromQL 核心:rate、聚合与 by / without</a><div class="subnav"><a class="toc-a lv3" href="05-promql.html#sec-5-1">instant vector 与 range vector:rate 只能吃区间</a><a class="toc-a lv3" href="05-promql.html#sec-5-2">rate 与 increase:为什么至少需要两个样本</a><a class="toc-a lv3" href="05-promql.html#sec-5-3">聚合:by 与 without 的语义差异</a><a class="toc-a lv3" href="05-promql.html#sec-5-4">offset、@ 与子查询</a></div><a class="toc-a" href="06-quantile.html"><i class="cn">6</i>分位数:histogram_quantile 与 P99 的正确算法</a><div class="subnav"><a class="toc-a lv3" href="06-quantile.html#sec-6-1">为什么不能对 P99 求平均</a><a class="toc-a lv3" href="06-quantile.html#sec-6-2">分桶是怎么变成分位数的</a><a class="toc-a lv3" href="06-quantile.html#sec-6-3">低桶数为什么失真:误差最大可以到一整个桶宽</a><a class="toc-a lv3" href="06-quantile.html#sec-6-4">生产可用的分位数查询</a></div><a class="toc-a" href="07-collect.html"><i class="cn">7</i>采集体系:exporter、pull 模型、服务发现与 relabel</a><div class="subnav"><a class="toc-a lv3" href="07-collect.html#sec-7-1">pull 模型与 exporter 生态</a><a class="toc-a lv3" href="07-collect.html#sec-7-2">一份真实的 scrape_configs</a><a class="toc-a lv3" href="07-collect.html#sec-7-3">服务发现与 relabel:从发现到抓取之间发生了什么</a><a class="toc-a lv3" href="07-collect.html#sec-7-4">抓取间隔、超时与保留策略的取舍</a></div><a class="toc-a" href="08-instrument.html"><i class="cn">8</i>埋点实战:给应用加上 RED 指标</a><div class="subnav"><a class="toc-a lv3" href="08-instrument.html#sec-8-1">先写指标字典,再写代码</a><a class="toc-a lv3" href="08-instrument.html#sec-8-2">中间件:一次搞定 QPS、错误率和耗时</a><a class="toc-a lv3" href="08-instrument.html#sec-8-3">业务与依赖指标:资源全绿时唯一能救命的信号</a><a class="toc-a lv3" href="08-instrument.html#sec-8-4">高基数防御:三道卡口</a></div><a class="toc-a" href="09-alert.ht
<div class="side-foot">
<a class="btn" href="index.html">← 模块目录</a>
<a class="btn" href="../../index.html">学习路径</a>
</div>
</aside>
<div class="mask" id="mask"></div>
<main class="main-col">
<article>
<div class="modhero">
<span class="mh-no">阶段 2 · 模块 1 / 4</span>
<h1>指标与 Prometheus</h1>
<p class="mh-sub">阶段 2 · 可观测性三支柱 · 时序模型、PromQL、告警设计</p>
<div class="mh-meta">
<span>共 <b>14</b> 章</span>
<span><b>53</b> 个小节</span>
<span>约 <b>75</b> 千字</span>
</div>
</div>
<p class="lead">
<b>模块定位:</b>故障发生时你需要的不是「所有数据」,而是「10 秒内能判断影响面」的数据。日志告诉你为什么坏,追踪告诉你慢在哪一跳,但只有指标能回答「现在坏了没有、坏了多久、影响了多少人」——它保留期最长、聚合成本最低,也是唯一能直接驱动告警的数据源。这一模块把 Prometheus 从数据模型一路讲到告警规则:先搞懂一条时间序列到底是什么,再学 PromQL 怎么算,最后落到「配出 5 条收到就知道该做什么的告警」。
<br><br>
<b>学完之后你应该能:</b>在一个已有服务上补出完整的 RED 指标(QPS、错误率、P95/P99)与饱和度指标;读懂并写出带 <code>scrape_configs</code> / <code>relabel_configs</code> 的采集配置;写出能直接粘进 Prometheus 表达式框的查询并解释它为什么对;判断一个标签会不会打爆基数;用 <code>for</code>、分级、路由、抑制设计告警,并用多窗口多燃烧率替代固定阈值。
<br><br>
<b>前置:</b>阶段 1 的 <a href="../s1-01-nginx/index.html">Nginx 与流量入口</a>(知道 <code>request_time</code> / <code>upstream_response_time</code> 与状态码分布从哪里来)、<a href="../s1-02-deploy/index.html">应用部署与发布</a>(知道一次发布在时间轴上会留下什么痕迹)、<a href="../s1-04-cicd/index.html">CI/CD 流水线</a>(知道指标校验该卡在哪一步)。需要会 Linux 命令行、Docker,以及至少一门带 HTTP 框架的语言。
<br><br>
<b>建议投入:</b>16–20 小时,其中至少 8 小时在真实环境动手。第 12 章的 lab 不是可选项——没有亲手把 <code>/metrics</code> 打出来、没有亲眼看着一条告警从 Pending 翻成 Firing,前面 11 章都会记不牢。
</p>
<h2 id="chapters-list">章节目录</h2>
<ol class="chaplist">
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">1</span>
<a class="ci-title" href="01-why.html">为什么指标是可观测性的第一支柱</a>
<a class="ci-go" href="01-why.html">进入 →</a>
</div>
<div class="ci-secs"><a href="01-why.html#sec-1-1">指标能回答而日志回答不了的问题</a><a href="01-why.html#sec-1-2">三支柱的分工:别让一种数据干三种活</a><a href="01-why.html#sec-1-3">成本模型:为什么指标可以全量长期保留</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">2</span>
<a class="ci-title" href="02-method.html">方法论:四大黄金信号、USE 与 RED</a>
<a class="ci-go" href="02-method.html">进入 →</a>
</div>
<div class="ci-secs"><a href="02-method.html#sec-2-1">四大黄金信号:先分清面向谁</a><a href="02-method.html#sec-2-2">RED:一个服务的最小可观测集合</a><a href="02-method.html#sec-2-3">USE:每个资源都要问三个问题</a><a href="02-method.html#sec-2-4">三个框架怎么一起用</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">3</span>
<a class="ci-title" href="03-model.html">数据模型:时间序列、标签与基数陷阱</a>
<a class="ci-go" href="03-model.html">进入 →</a>
</div>
<div class="ci-secs"><a href="03-model.html#sec-3-1">一条时间序列的完整身份</a><a href="03-model.html#sec-3-2">基数(cardinality)是怎么被撑爆的</a><a href="03-model.html#sec-3-3">标签设计规则:先写指标字典,再写代码</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">4</span>
<a class="ci-title" href="04-types.html">指标类型:counter / gauge / histogram / summary</a>
<a class="ci-go" href="04-types.html">进入 →</a>
</div>
<div class="ci-secs"><a href="04-types.html#sec-4-1">counter:只增不减,必须用 rate 读</a><a href="04-types.html#sec-4-2">gauge:可增可减,直接读</a><a href="04-types.html#sec-4-3">histogram:服务端分桶,能聚合的分位数</a><a href="04-types.html#sec-4-4">summary:客户端算分位数,聚合即失真</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">5</span>
<a class="ci-title" href="05-promql.html">PromQL 核心:rate、聚合与 by / without</a>
<a class="ci-go" href="05-promql.html">进入 →</a>
</div>
<div class="ci-secs"><a href="05-promql.html#sec-5-1">instant vector 与 range vector:rate 只能吃区间</a><a href="05-promql.html#sec-5-2">rate 与 increase:为什么至少需要两个样本</a><a href="05-promql.html#sec-5-3">聚合:by 与 without 的语义差异</a><a href="05-promql.html#sec-5-4">offset、@ 与子查询</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">6</span>
<a class="ci-title" href="06-quantile.html">分位数:histogram_quantile 与 P99 的正确算法</a>
<a class="ci-go" href="06-quantile.html">进入 →</a>
</div>
<div class="ci-secs"><a href="06-quantile.html#sec-6-1">为什么不能对 P99 求平均</a><a href="06-quantile.html#sec-6-2">分桶是怎么变成分位数的</a><a href="06-quantile.html#sec-6-3">低桶数为什么失真:误差最大可以到一整个桶宽</a><a href="06-quantile.html#sec-6-4">生产可用的分位数查询</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">7</span>
<a class="ci-title" href="07-collect.html">采集体系:exporter、pull 模型、服务发现与 relabel</a>
<a class="ci-go" href="07-collect.html">进入 →</a>
</div>
<div class="ci-secs"><a href="07-collect.html#sec-7-1">pull 模型与 exporter 生态</a><a href="07-collect.html#sec-7-2">一份真实的 scrape_configs</a><a href="07-collect.html#sec-7-3">服务发现与 relabel:从发现到抓取之间发生了什么</a><a href="07-collect.html#sec-7-4">抓取间隔、超时与保留策略的取舍</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">8</span>
<a class="ci-title" href="08-instrument.html">埋点实战:给应用加上 RED 指标</a>
<a class="ci-go" href="08-instrument.html">进入 →</a>
</div>
<div class="ci-secs"><a href="08-instrument.html#sec-8-1">先写指标字典,再写代码</a><a href="08-instrument.html#sec-8-2">中间件:一次搞定 QPS、错误率和耗时</a><a href="08-instrument.html#sec-8-3">业务与依赖指标:资源全绿时唯一能救命的信号</a><a href="08-instrument.html#sec-8-4">高基数防御:三道卡口</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">9</span>
<a class="ci-title" href="09-alert.html">告警设计:for、分级、路由与抑制</a>
<a class="ci-go" href="09-alert.html">进入 →</a>
</div>
<div class="ci-secs"><a href="09-alert.html#sec-9-1">一条告警规则的完整结构</a><a href="09-alert.html#sec-9-2">for:用时间换掉抖动</a><a href="09-alert.html#sec-9-3">分级与路由:让对的人在对的时间收到</a><a href="09-alert.html#sec-9-4">抑制与静默:不要为同一件事叫三遍</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">10</span>
<a class="ci-title" href="10-burnrate.html">多窗口多燃烧率告警</a>
<a class="ci-go" href="10-burnrate.html">进入 →</a>
</div>
<div class="ci-secs"><a href="10-burnrate.html#sec-10-1">固定阈值为什么永远调不好</a><a href="10-burnrate.html#sec-10-2">错误预算与燃烧率(burn rate)</a><a href="10-burnrate.html#sec-10-3">多窗口多燃烧率:快烧与慢烧</a><a href="10-burnrate.html#sec-10-4">落地:一组可以直接用的规则</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">11</span>
<a class="ci-title" href="11-antipattern.html">反模式与成本治理</a>
<a class="ci-go" href="11-antipattern.html">进入 →</a>
</div>
<div class="ci-secs"><a href="11-antipattern.html#sec-11-1">反模式一:高基数标签打爆存储</a><a href="11-antipattern.html#sec-11-2">反模式二:只有资源指标,没有业务指标</a><a href="11-antipattern.html#sec-11-3">反模式三:告警无人认领</a><a href="11-antipattern.html#sec-11-4">成本治理:省什么、代价是什么</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">12</span>
<a class="ci-title" href="12-lab.html">实操:从零搭一套 Prometheus + 埋点 + 告警</a>
<a class="ci-go" href="12-lab.html">进入 →</a>
</div>
<div class="ci-secs"><a href="12-lab.html#sec-12-1">目标与验收标准</a><a href="12-lab.html#sec-12-2">环境与目录:五个文件</a><a href="12-lab.html#sec-12-3">应用埋点:五组指标 + 故障注入开关</a><a href="12-lab.html#sec-12-4">采集与告警规则</a><a href="12-lab.html#sec-12-5">五条告警规则:每一条都回答三个问题</a><a href="12-lab.html#sec-12-6">故障注入与端到端验证</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">13</span>
<a class="ci-title" href="13-cheat.html">速查表与高频坑</a>
<a class="ci-go" href="13-cheat.html">进入 →</a>
</div>
<div class="ci-secs"><a href="13-cheat.html#sec-13-1">PromQL 速查表</a><a href="13-cheat.html#sec-13-2">配置速查表</a><a href="13-cheat.html#sec-13-3">十二个高频坑</a></div>
</li>
<li class="chap-item">
<div class="ci-head">
<span class="ci-no">14</span>
<a class="ci-title" href="14-checklist.html">完成标准与自测</a>
<a class="ci-go" href="14-checklist.html">进入 →</a>
</div>
<div class="ci-secs"><a href="14-checklist.html#sec-14-1">自测题</a><a href="14-checklist.html#sec-14-2">下一步</a></div>
</li>
</ol>
<div class="box tip">
<span class="bt">怎么用这一模块</span>
<p>左侧是<b>章节目录树</b>:先看章标题快速定位,进入某一章后该章的小节会展开并跟随滚动高亮;点其它章标题可直接跳到对应页面。</p>
<p>每章结尾都有「上一章 / 下一章」可以连续阅读,代码块右上角可以一键复制。</p>
</div>
</article>
<nav class="pager">
<a class="prev" href="../s1-04-cicd/index.html"><span class="k">← 上一模块</span><span class="v">CI/CD 流水线</span></a>
<a class="next" href="../s2-02-logs/index.html"><span class="k">下一模块 →</span><span class="v">日志采集与检索</span></a>
</nav>
<p class="foot">
指标与 Prometheus · 模块目录<br>
<a href="../../index.html">返回学习路径</a>
</p>
</main>
</div>
<button id="toTop" title="回到顶部">↑</button>
<script src="../../assets/doc.js"></script>
</body>
</html>