文章目錄
- @[toc]
- label 簡介
- 自定義標簽
- relabel_configs
- regex
- relabel_action
- metric_relabel_configs
- 兩者的區別
- 實踐
文章目錄
- @[toc]
- label 簡介
- 自定義標簽
- relabel_configs
- regex
- relabel_action
- metric_relabel_configs
- 兩者的區別
- 實踐
label 簡介
label 對于 Prometheus 來說,屬于數據處理的方式,Prometheus 是通過指定的 label 來查詢數據
Prometheus 的 target 中實例,都會包含一些默認的 metadata label,比如基于 kubernetes 服務發現的會有類似
__meta_kubernetes_namespace="monitor"
這種 label
- 下面就是上次咱們基于
kubernetes_sd_configs
服務發現加入的 node-exporter,可以看到,Prometheus 注入了這些 metadata label,這些是不會顯示在 /metrics 頁面
里面的
自定義標簽
針對于 Prometheus 聯邦集群,自定義標簽就可以很好的去區分環境,也可以細化查詢的維度
- 比如我基于之前的 Prometheus 配置文件,給 Prometheus 增加了一個 label
- job_name: prometheusmetrics_path: '/metrics'static_configs:- targets: ['prometheus-svc:9090']labels:env: study
可以從 target 里面看到我們增加的 label
graph 里面也可以指定我們自定義的 label 來查詢數據(這里因為就一個 Prometheus,就演示一下效果)
relabel_configs
relabel_configs
- Relabeling is a powerful tool to dynamically rewrite the label set of a target before it gets scraped. Multiple relabeling steps can be configured per scrape configuration. They are applied to the label set of each target in order of their appearance in the configuration file.
- 重新標記是一個強大的工具,可以在目標被抓取之前動態重寫目標的標簽集。每個抓取配置可以配置多個重新標記步驟。它們將按照它們在配置文件中的出現順序應用于每個目標的標簽集。
- Initially, aside from the configured per-target labels, a target’s
job
label is set to thejob_name
value of the respective scrape configuration. The__address__
label is set to the:
address of the target. After relabeling, theinstance
label is set to the value of__address__
by default if it was not set during relabeling. The__scheme__
and__metrics_path__
labels are set to the scheme and metrics path of the target respectively. The__param_
label is set to the value of the first passed URL parameter called .- 最初,除了配置的每個目標標簽外,目標的
job
標簽設置為相應抓取配置的job_name
值。__address__
標簽設置為目標的<host>:<port>
地址。重新標記后,如果在重新標記期間未設置instance
標簽,則默認設置為__address__
值。__scheme__
和__metrics_path__
標簽分別設置為目標的方案和指標路徑。__param_
標簽設置為第一個傳遞的 URL 參數的值,稱為<name>
。- The
__scrape_interval__
and__scrape_timeout__
labels are set to the target’s interval and timeout. This is experimental and could change in the future.__scrape_interval__
和__scrape_timeout__
標簽設置為目標的間隔和超時。這是實驗性的,將來可能會改變。- Additional labels prefixed with
__meta_
may be available during the relabeling phase. They are set by the service discovery mechanism that provided the target and vary between mechanisms.- 在重新標記階段,可以使用以
__meta_
為前綴的其他標簽。它們由提供目標的服務發現機制設置,并且因機制而異。- Labels starting with
__
will be removed from the label set after target relabeling is completed.- 目標重新標記完成后,將從標簽集中刪除以
__
開頭的標簽。- If a relabeling step needs to store a label value only temporarily (as the input to a subsequent relabeling step), use the
__tmp
label name prefix. This prefix is guaranteed to never be used by Prometheus itself.- 如果重新標記步驟只需要臨時存儲標簽值(作為后續重新標記步驟的輸入),請使用
__tmp
標簽名稱前綴。這個前綴保證永遠不會被 Prometheus 本身使用。
regex
<regex>
是任何有效的 RE2 正則表達式。replace
、keep
、drop
、labelmap
、labeldrop
和labelkeep
操作需要它。- 正則表達式錨定在兩端。要取消錨定正則表達式,請使用
.*<regex>.*
。
relabel_action
重新標記操作有以下幾個
replace
:將regex
與連接的source_labels
匹配。然后,將target_label
設置為replacement
,將replacement
中的匹配組引用 (${1}
,${2}
, …) 替換為其值。如果regex
不匹配,則不進行替換。lowercase
:將連接的source_labels
映射到其小寫字母。uppercase
:將連接的source_labels
映射到其大寫字母。keep
:丟棄regex
與連接的source_labels
不匹配的目標。drop
:丟棄regex
與連接source_labels
匹配的目標。keepequal
:丟棄與連接的source_labels
不匹配的目標target_label
。dropequal
:丟棄與連接的source_labels
匹配target_label
的目標。hashmod
:將target_label
設置為串聯source_labels
的哈希值的modulus
。labelmap
:將regex
與所有源標簽名稱匹配,而不僅僅是source_labels
中指定的名稱。然后將匹配標簽的值復制到replacement
給出的標簽名稱,并將replacement
中的匹配組引用 (${1}
,${2}
, …) 替換為其值。labeldrop
:將regex
與所有標簽名稱匹配。任何匹配的標簽都將從標簽集中刪除。labelkeep
:將regex
與所有標簽名稱匹配。任何不匹配的標簽都將從標簽集中刪除。
必須注意
labeldrop
和labelkeep
,以確保在刪除標簽后,指標仍是唯一標記的。
metric_relabel_configs
metric_relabel_configs
- Metric relabeling is applied to samples as the last step before ingestion. It has the same configuration format and actions as target relabeling. Metric relabeling does not apply to automatically generated timeseries such as
up
.- metric_relabel_configs 應用于樣本,作為攝取前的最后一步。它具有與 relabel_config 相同的配置格式和操作。指標重新標記不適用于自動生成的時間序列,例如
up
。- One use for this is to exclude time series that are too expensive to ingest.
- 這樣做的一個用途是排除成本太高而無法攝取的時間序列。
兩者的區別
relabel_configs
在抓取(采集)之前對任何目標及其標簽進行高級修改。metric_relabel_configs
在攝取(存儲)之前對任何目標及其標簽進行高級修改。
實踐
- 同樣,采用咱們之前部署 node-exporter 的配置文件。這里就取一小段做演示
- 這里是將
__meta_kubernetes_namespace
重新標記成kubernetes_namespace
- source_labels: [__meta_kubernetes_namespace]action: replacetarget_label: kubernetes_namespace
這樣,就可以看到新增了一個 label
就可以用這個重新標記的 label 來做查詢了