本文件說明如何設定及使用 Ops Agent 指標接收器,以便從 Compute Engine 上的 Prometheus 收集指標。本文也說明瞭範例,您可以用來試用接收器。
Google Kubernetes Engine 使用者可以使用 Google Cloud Managed Service for Prometheus 收集 Prometheus 指標。透過 Ops Agent Prometheus 接收器,Compute Engine 使用者也能享有相同功能。
您可以使用 Cloud Monitoring 提供的所有工具 (包括 PromQL),查看及分析 Prometheus 接收器收集的資料。舉例來說,您可以使用 Metrics Explorer (如「Google Cloud 監控主控台」一文所述) 查詢資料。您也可以建立 Cloud Monitoring 資訊主頁和快訊政策,以便監控 Prometheus 指標。建議您使用 PromQL 做為 Prometheus 指標的查詢語言。
您也可以在 Cloud Monitoring 以外的介面中查看 Prometheus 指標,例如 Prometheus UI 和 Grafana。
選擇合適的接收器
決定是否要使用 Prometheus 接收器前,請先確認所用應用程式是否已整合作業套件代理程式。如要瞭解現有的作業套件代理程式整合作業,請參閱「監控第三方應用程式」。如果有現有的整合服務,建議您使用該服務。詳情請參閱「選擇現有的整合服務」。
建議您在下列情況下使用 Ops Agent Prometheus 接收器:
您具備使用 Prometheus 的經驗,並依賴 Prometheus 標準,也瞭解擷取間隔和卡氏度等因素如何影響成本。詳情請參閱「選擇 Prometheus 接收器」一文。
您監控的軟體並未包含在現有的作業套件代理程式整合中。
現有整合
作業套件代理程式可為多個第三方應用程式提供整合功能。這些整合功能可提供下列功能:
- 應用程式所選取的一組
workload.googleapis.com
指標 - 用於將指標視覺化的資訊主頁。
使用現有整合功能擷取的指標,須依代理程式收集的指標以位元組計費。您可以事先得知指標的數量和類型,並利用這些資訊來估算費用。
舉例來說,如果您使用 Apache Web Server (httpd) 整合,Ops Agent 會收集五個純量指標,每個資料點的計數為 8 個位元組。如果您將 Ops Agent 的預設取樣頻率維持為 60 秒,則每天擷取的位元組數量為 57,600 * 主機數量:
- 8 (位元組) * 1440 (分鐘/天) * 5 (指標) * n (主機),或
- 57,600 * n (主機)
如要進一步瞭解如何估算成本,請參閱「依據擷取的位元組數計費示例」。
Prometheus 接收器
使用作業套件代理程式收集 Prometheus 指標時,請遵守下列規定:
您可以控制應用程式產生的指標數量和基數。沒有經過精選的指標組合。擷取的資料量取決於 Prometheus 應用程式和作業套件 Prometheus 接收器的設定。
指標會以
prometheus.googleapis.com
指標的形式擷取至 Cloud Monitoring。這些指標在擷取至 Cloud Monitoring 時會歸類為「自訂」指標,並受制於自訂指標的配額和限制。您必須根據要擷取的指標組合和業務需求,設計及建立所需的 Cloud Monitoring 資訊主頁。如要進一步瞭解如何建立資訊主頁,請參閱「資訊主頁和圖表」一文。
指標擷取的費用會根據擷取的樣本數量來計算。如要預估使用 Prometheus 接收器時的費用,您必須判斷在一個帳單週期內可能收集到的樣本數量。預估價格是根據下列因素計算得出:
- 純量指標數量,每個值代表一個樣本
- 分布指標數量;每個直方圖的計數方式為 (2 + 直方圖中的值區數量) 個樣本
- 各指標的取樣頻率
- 用於取樣指標的主機數量
如要進一步瞭解如何計算樣本數量和估算費用,請參閱「根據已擷取的樣本數量計費範例」。
事前準備
如要使用 Prometheus 接收器收集 Prometheus 指標,您必須安裝 2.25.0 以上版本的作業套件代理程式。
作業套件代理程式接收器需要能發出 Prometheus 指標的端點。因此,您的應用程式必須直接提供這類端點,或使用 Prometheus 程式庫或匯出工具來公開端點。許多程式庫和語言架構 (例如 Spring 和 DropWizard),或是會產生非 Prometheus 指標的應用程式 (例如 StatsD、DogStatsD 和 Graphite),都可以使用 Prometheus 用戶端程式庫或匯出程式來產生 Prometheus 風格指標。舉例來說,如要發出 Prometheus 指標:
- Spring 使用者可以使用 Spring Metrics 程式庫。
- StatsD 使用者可以使用
statsd_exporter
套件。 - Graphite 使用者可以使用
graphite_exporter
套件。
當應用程式直接或透過程式庫或匯出程式發出 Prometheus 指標時,作業套件代理程式 (已設定 Prometheus 接收器) 即可收集這些指標。
設定作業套件代理程式
作業套件代理程式設定模型通常會涉及定義下列項目:
- 接收器:決定要收集哪些指標。
- 處理器:說明 Ops Agent 如何修改指標。
- 管道:將接收器和處理器連結在一起,成為一個服務。
擷取 Prometheus 指標的設定略有不同:不涉及任何處理器。
Prometheus 指標的設定
設定作業套件代理程式以擷取 Prometheus 指標的做法與一般設定不同,如下所示:
您不會為 Prometheus 指標建立作業套件代理程式處理器。Prometheus 接收器支援 Prometheus
scrape_config
規格中指定的幾乎所有設定選項,包括重新標示選項。與 Prometheus 接收器中指定的做法相同,請使用 scape 設定的
relabel_configs
和metric_relabel_configs
部分,而非作業套件代理程式處理程序,處理任何指標。詳情請參閱「重新標示:修改要擷取的資料」。您只需從 Prometheus 接收器的角度定義 Prometheus 管道。您未指定任何處理器。您也無法在 Prometheus 指標的管道中使用任何非 Prometheus 接收器。
大部分的接收器設定都是 scrape-config 選項的規格。為了簡化說明,以下只會略過這些選項,並說明使用 Prometheus 接收器的作業套件代理程式設定結構。您可以指定 RECEIVER_ID 和 PIPELINE_ID 的值。
metrics: receivers: RECEIVER_ID: type: prometheus config: scrape_configs: [... omitted for brevity ...] service: pipelines: PIPELINE_ID: receivers: [RECEIVER_ID]
下節將詳細說明 Prometheus 接收器。如需接收器和管道的功能範例,請參閱「新增 Ops Agent 接收器和管道」。
Prometheus 接收器
如要為 Prometheus 指標指定接收器,請建立 prometheus
類型的指標接收器,並指定一組 scrape_config
選項。接收器支援所有 Prometheus scrape_config
選項,但下列選項除外:
- 服務探索部分,
*_sd_config
。 honor_labels
設定。
因此,您可以複製現有的擷取設定,並用於 Ops Agent,幾乎不需要或完全不需要修改。
Prometheus 接收器的完整結構如下所示:
metrics: receivers: prom_application: type: prometheus config: scrape_configs: - job_name: 'STRING' # must be unique across all Prometheus receivers scrape_interval: # duration, like 10m or 15s scrape_timeout: # duration, like 10m or 15s metrics_path: # resource path for metrics, default = /metrics honor_timestamps: # boolean, default = false scheme: # http or https, default = http params: - STRING: STRING basic_auth: username: STRING password: SECRET password_file: STRING authorization: type: STRING # default = Bearer credentials: SECRET credentials_file: FILENAME oauth2: OAUTH2 # See Prometheus oauth2 follow_redirects: # boolean, default = true enable_http2: # boolean, default = true tls_config: TLS_CONFIG # See Prometheus tls_config proxy_url: STRING static_configs: STATIC_CONFIG # See Prometheus static_config relabel_configs: RELABEL_CONFIG # See Prometheus relabel_config metric_relabel_configs: METRIC_RELABEL_CONFIGS # See Prometheus metric_relabel_configs
如需重新命名設定的範例,請參閱「額外的接收器設定」。
範例:為 Prometheus 設定作業套件代理程式
本節將舉例說明如何設定作業套件代理程式,以便從應用程式收集 Prometheus 指標。本範例使用 Prometheus 社群提供的 JSON 匯出工具 (json_exporter
),該工具會在 7979 通訊埠上公開 Prometheus 指標。
設定範例需要下列資源,您可能需要安裝這些資源:
git
curl
make
python3
- Go 語言,1.19 以上版本
建立或設定應用程式
如要取得並執行 JSON 匯出工具,請按照下列程序操作:
複製
json_exporter
存放區,然後執行下列指令來檢查匯出工具:git clone https://github.com/prometheus-community/json_exporter.git cd json_exporter git checkout v0.5.0
執行下列指令來建構匯出器:
make build
執行下列指令,啟動 Python HTTP 伺服器:
python3 -m http.server 8000 &
執行下列指令,啟動 JSON 匯出工具:
./json_exporter --config.file examples/config.yml &
請查詢 JSON 匯出工具,確認該工具是否正在執行,並在通訊埠 7979 上公開指標:
curl "http://localhost:7979/probe?module=default&target=http://localhost:8000/examples/data.json"
如果查詢成功,您會看到類似以下的輸出內容:
# HELP example_global_value Example of a top-level global value scrape in the json # TYPE example_global_value untyped example_global_value{environment="beta",location="planet-mars"} 1234 # HELP example_value_active Example of sub-level value scrapes from a json # TYPE example_value_active untyped example_value_active{environment="beta",id="id-A"} 1 example_value_active{environment="beta",id="id-C"} 1 # HELP example_value_boolean Example of sub-level value scrapes from a json # TYPE example_value_boolean untyped example_value_boolean{environment="beta",id="id-A"} 1 example_value_boolean{environment="beta",id="id-C"} 0 # HELP example_value_count Example of sub-level value scrapes from a json # TYPE example_value_count untyped example_value_count{environment="beta",id="id-A"} 1 example_value_count{environment="beta",id="id-C"} 3
在這個輸出內容中,
example_value_active
之類的字串是指標名稱,其中標籤和值則以大括號表示。資料值會接續標籤集。
新增 Ops Agent 接收器和管道
如要設定 Ops Agent 從 JSON 匯出應用程式擷取指標,您必須修改代理程式設定,新增 Prometheus 接收器和管道。針對 JSON 匯出器範例,請使用下列程序:
編輯 Ops Agent 設定檔
/etc/google-cloud-ops-agent/config.yaml
,並新增下列 Prometheus 接收器和管道項目:metrics: receivers: prometheus: type: prometheus config: scrape_configs: - job_name: 'json_exporter' scrape_interval: 10s metrics_path: /probe params: module: [default] target: [http://localhost:8000/examples/data.json] static_configs: - targets: ['localhost:7979'] service: pipelines: prometheus_pipeline: receivers: - prometheus
如果這個檔案中已有其他設定項目,請將 Prometheus 接收器和管道新增至現有的
metrics
和service
項目。詳情請參閱「指標設定」。如需接收器中重新命名設定的範例,請參閱「其他接收器設定」。
重新啟動作業套件代理程式
如要套用設定變更,您必須重新啟動 Ops Agent。
Linux
如要重新啟動代理程式,請在執行個體上執行下列指令:
sudo service google-cloud-ops-agent restart
如要確認代理程式已重新啟動,請執行下列指令,並確認「Metrics Agent」和「Logging Agent」元件已啟動:
sudo systemctl status google-cloud-ops-agent"*"
Windows
使用遠端桌面協定或類似工具連線至執行個體,然後登入 Windows。
在 PowerShell 圖示上按一下滑鼠右鍵,然後選取「Run as Administrator」,開啟具有系統管理員權限的 PowerShell 終端機。
如要重新啟動代理程式,請執行下列 PowerShell 指令:
Restart-Service google-cloud-ops-agent -Force
如要確認代理程式已重新啟動,請執行下列指令,並確認「Metrics Agent」和「Logging Agent」元件已啟動:
Get-Service google-cloud-ops-agent*
Cloud Monitoring 中的 Prometheus 指標
您可以使用 Cloud Monitoring 提供的工具,搭配 Prometheus 接收器收集的資料。舉例來說,您可以使用 Metrics Explorer 繪製資料圖表,如Google Cloud 「Monitoring 控制台」所述。以下各節說明 Cloud Monitoring 搭配 Metrics Explorer 時可用的查詢工具:
您可以為指標建立 Cloud Monitoring 資訊主頁和警示政策。如要瞭解資訊主頁和可用的圖表類型,請參閱「資訊主頁和圖表」一文。如要瞭解快訊政策,請參閱「使用快訊政策」。
您也可以在其他介面 (例如 Prometheus UI 和 Grafana) 中查看指標。如要瞭解如何設定這些介面,請參閱 Google Cloud Managed Service for Prometheus 說明文件中的以下章節:
使用 PromQL
使用 Prometheus 接收器擷取的指標,建議使用 PromQL 做為查詢語言。
如要驗證 Prometheus 資料是否已擷取,最簡單的方法是使用 Google Cloud 主控台中的 Cloud Monitoring Metrics Explorer 頁面:
-
前往 Google Cloud 控制台的 leaderboard「Metrics Explorer」頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。
在查詢建構工具窗格的工具列中,選取名稱為 code MQL 或 code PromQL 的按鈕。
確認「Language」切換鈕中已選取「PromQL」。語言切換鈕位於可讓您設定查詢格式的工具列中。
在編輯器中輸入以下查詢,然後點選「執行查詢」:
up
如果系統正在擷取資料,您會看到類似下方的圖表:
如果您執行 JSON 匯出器範例,也可以發出以下類似的查詢:
依名稱查詢特定匯出指標的所有資料,例如:
example_value_count
下圖顯示
example_value_count
的圖表,包括 JSON 匯出應用程式定義的標籤,以及 Ops Agent 新增的標籤:查詢來自特定命名空間的匯出指標資料。
namespace
標籤的值是指派給 VM 的 Compute Engine 執行個體 ID,也就是5671897148133813325
這類長數字。查詢如下所示:example_value_count{namespace="INSTANCE_ID"}
查詢符合特定規則運算式的資料。JSON 匯出工具會以
id
標籤傳送指標,其中的值為id-A
、id-B
、id-C
等。如要篩選任何指標,只要id
標籤符合此模式,請使用下列查詢:example_value_count{id=~"id.*"}
如要進一步瞭解如何在 Metrics Explorer 和 Cloud Monitoring 圖表中使用 PromQL,請參閱「Cloud Monitoring 中的 PromQL」。
使用 MQL
如要以 Cloud Monitoring 時間序列查看 Prometheus 資料,並建立圖表和資訊主頁,您也可以使用選單式介面或 MQL。以下是 Metrics Explorer 中的簡易查詢:
-
前往 Google Cloud 控制台的 leaderboard「Metrics Explorer」頁面:
如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。
指定要在圖表中顯示的資料。除了先前所述的 PromQL 分頁,您也可以使用 MQL。
如要使用 MQL,請按照下列步驟操作:
- 在查詢建構工具窗格的工具列中,選取名稱為 code MQL 或 code PromQL 的按鈕。
- 確認「Language」切換按鈕中已選取「MQL」。語言切換鈕位於可讓您設定查詢格式的工具列中。
輸入下列查詢:
fetch prometheus_target::prometheus.googleapis.com/up/gauge
按一下 [Run Query] (執行查詢)。
如要使用選單式介面,請按照下列步驟操作:
在「Resource type」欄位中輸入「prometheus」,即可篩選清單,然後選取「Prometheus Target」。
在「指標」欄位中輸入「up/」,篩選清單,然後選取「prometheus/up/gauge」。
這兩項查詢產生的圖表會顯示與 PromQL 範例相同的資料。
prometheus_target
資源
在 Cloud Monitoring 中,時間序列資料會根據受控資源類型寫入。Prometheus 指標的受控資源類型為 prometheus_target
。針對非以 PromQL 編寫的 Prometheus 指標執行監控查詢時,必須指定這個資源類型。
prometheus_target
資源具有下列標籤,可用於篩選及操作查詢資料:
project_id
: Google Cloud 專案的 ID,例如my-project
,作業套件代理程式會在其中執行。location
:Ops Agent 執行的 Google Cloud 或 AWS 區域,例如us-east1-a
(Google Cloud) 或aws:us-east-1a
(AWS)。cluster
:使用作業套件代理程式收集的 Prometheus 指標一律為__gce__
。namespace
:Ops Agent 執行所在 VM 的 Compute Engine 執行個體 ID。job
:接收器設定中的job_name
欄位值。instance
:Prometheus 目標的執行個體標籤,取自接收器設定;預設為目標。
這些標籤的值會在收集期間設定。namespace
、location
和 cluster
標籤的值無法變更。如果從應用程式擷取的指標也有這些標籤,Ops Agent 會在擷取的標籤前方加上字串 exported_
。
在 Cloud Monitoring 中查看指標用量和診斷資訊
Cloud Monitoring 的「指標管理」頁面提供的資訊可協助您控制可計費指標的支出金額,且不會影響可觀察性。「指標管理」頁面會回報下列資訊:
- 以位元組和樣本為基礎的計費作業量,跨指標網域和個別指標。
- 指標的標籤和基數資料。
- 每個指標的讀取次數。
- 在警告政策和自訂資訊主頁中使用指標。
- 指標寫入錯誤率。
您也可以使用「指標管理」頁面排除不需要的指標,省下擷取這些指標的成本。
如要查看「指標管理」頁面,請按照下列步驟操作:
-
在 Google Cloud 控制台中,前往
「Metrics management」頁面:如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。
- 在工具列中選取時間範圍。根據預設,「指標管理」頁面會顯示過去一天內收集的指標資訊。
如要進一步瞭解「指標管理」頁面,請參閱「查看及管理指標使用情形」一文。
重新標示:修改要擷取的資料
您可以使用重新標示功能,在目標被擷取前修改擷取目標的標籤組合或指標。如果重新命名設定中有多個步驟,系統會依照設定檔中的順序套用這些步驟。
作業套件代理程式會建立一組中繼標籤 (標籤前置字串為 __meta_
)。這些中繼標記會記錄 Ops Agent 執行所在的 Compute Engine 執行個體資訊。前置 __
字串的標籤 (包括中繼標籤) 僅適用於重新標示。您可以使用重新標示功能,在擷取的標籤中擷取這些標籤的值。
指標重新命名會套用至樣本,這是擷取前最後一個步驟。您可以使用指標重新命名功能,刪除不需要擷取的時間序列;刪除這些時間序列可減少擷取的樣本數量,進而降低成本。
如要進一步瞭解重新標示,請參閱 relabel_config
和 metric_relabel_configs
的 Prometheus 說明文件。
重新標記期間可用的 Compute Engine 中繼資料標籤
作業套件代理程式擷取指標時,會包含一組元資料標籤,其值取決於代理程式執行所在的 Compute Engine VM 設定。您可以使用這些標籤和 Prometheus 接收器的 relabel_configs
部分,在指標中加入其他中繼資料,讓系統瞭解這些指標的來源 VM。如需範例,請參閱「其他接收器設定」。
目標可使用下列中繼標籤,在 relabel_configs
部分使用:
__meta_gce_instance_id
:Compute Engine 執行個體的數字 ID (本機)__meta_gce_instance_name
:Compute Engine 執行個體的名稱 (本機);作業套件代理程式會自動將這個值放入指標的可變動instance_name
標籤中。__meta_gce_machine_type
:執行個體的機器類型完整或部分網址;Ops Agent 會自動將這個值放入指標的可變動machine_type
標籤中。__meta_gce_metadata_NAME
:例項的每個中繼資料項目__meta_gce_network
:執行個體的網路網址__meta_gce_private_ip
:執行個體的私人 IP 位址__meta_gce_interface_ipv4_NAME
:每個命名介面的 IPv4 位址__meta_gce_project
:執行個體執行的 Google Cloud 專案 (本機)__meta_gce_public_ip
:執行個體的公開 IP 位址 (如有)__meta_gce_tags
:以逗號分隔的例項標記清單__meta_gce_zone
:執行個體執行所在的 Compute Engine 區域網址
這些標籤的值會在 Ops Agent 啟動時設定。如果您修改了值,就必須重新啟動 Ops Agent 才能重新整理值。
其他接收器設定
本節提供範例,說明如何使用 Prometheus 接收器的 relabel_configs
和 metric_relabel_configs
部分,修改攝入指標的數量和結構。本節也包含使用重新命名選項的 JSON 匯出器範例接收器的修改版本。
新增 VM 中繼資料
您可以使用 relabel_configs
區段,為指標新增標籤。舉例來說,以下範例使用了 Ops Agent 提供的中繼標籤 __meta_gce_zone
,用來建立指標標籤 zone
,這個標籤會在重新標記後保留,因為 zone
沒有 __
前置字元。
如需可用中繼標籤的清單,請參閱「重新標記時可用的 Compute Engine 中繼標籤」。預設的作業套件代理程式設定會為您重新標示部分元資料標籤。
relabel_configs: - source_labels: [__meta_gce_zone] regex: '(.+)' replacement: '${1}' target_label: zone
範例:設定 Prometheus 的作業套件代理程式中顯示的 Prometheus 接收器,已加入這個標籤。
放置指標
您可以使用 metrics_relabel_configs
區塊,捨棄不想擷取的指標;這種模式可用於控制成本。舉例來說,您可以使用下列模式,捨棄名稱與 METRIC_NAME_REGEX_1 或 METRIC_NAME_REGEX_2 相符的任何指標:
metric_relabel_configs: - source_labels: [ __name__ ] regex: 'METRIC_NAME_REGEX_1' action: drop - source_labels: [ __name__ ] regex: 'METRIC_NAME_REGEX_2' action: drop
新增靜態標籤
您可以使用 metrics_relabel_configs
部分,為 Prometheus 接收器擷取的所有指標新增靜態標籤。您可以使用下列模式,在所有攝入的指標中加入標籤 staticLabel1
和 staticLabel2
:
metric_relabel_configs: - source_labels: [ __address__ ] action: replace replacement: 'STATIC_VALUE_1' target_label: staticLabel1 - source_labels: [ __address__ ] action: replace replacement: 'STATIC_VALUE_2' target_label: staticLabel2
以下是 JSON 匯出器範例的 Prometheus 接收器版本,使用這些設定模式執行下列操作:
- 根據作業套件代理程式提供的
__meta_gce_zone
元資料標籤值,設定zone
標籤。 - 捨棄匯出工具的
example_global_value
指標。 - 將值為「A static value」的
staticLabel
標籤新增至所有攝入的度量基準。
metrics: receivers: prometheus: type: prometheus config: scrape_configs: - job_name: 'json_exporter' scrape_interval: 10s metrics_path: /probe params: module: [default] target: [http://localhost:8000/examples/data.json] static_configs: - targets: ['localhost:7979'] relabel_configs: - source_labels: [__meta_gce_zone] regex: '(.+)' replacement: '${1}' target_label: zone metric_relabel_configs: - source_labels: [ __name__ ] regex: 'example_global_value' action: drop - source_labels: [ __address__ ] action: replace replacement: 'A static value' target_label: staticLabel