首页/云/可观测性速查/标签匹配运算符:=、!=、=~、!~
Prometheus 与采集64 条

标签匹配运算符:=、!=、=~、!~

四种匹配运算符:等于与不等于做精确匹配,=~ 与 !~ 做正则匹配(隐式锚定全串);正则可合并多个状态码或排除内部路由。

可观测性速查云更新于 2026-10-12

匹配示例

匹配示例
http_requests_total{code="500"}             # 精确等于
http_requests_total{code!="500"}            # 不等于
http_requests_total{code=~"5.."}            # 正则:5 开头
http_requests_total{code=~"500|502|503"}    # 多选正则
http_requests_total{route!~"^/internal/.*"} # 排除内部路由
正则默认全串匹配:写 500 只能匹配完整值 500,要包含匹配需写成 .*500.*。

易错点

  • 正则没写锚点不等于模糊匹配:'500' 不匹配 '1500',而 '5..' 也不匹配 '5000'
  • 匹配器为空值的选择器(如 {job=""})会命中缺失该标签的序列,谨慎使用

数据来源与参考

  • Prometheus 文档 · Label matchers
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌