在 Compute Engine 上使用 Ops Agent

本文說明如何設定及使用 Ops Agent 指標接收器,從 Compute Engine 上的 Prometheus 收集指標。本文也提供範例,方便您試用接收器。

Google Kubernetes Engine 使用者可以透過 Google Cloud Managed Service for Prometheus 收集 Prometheus 指標。Ops Agent Prometheus 接收器可為 Compute Engine 使用者提供相同功能。

您可以使用 Cloud Monitoring 提供的所有工具 (包括 PromQL),查看及分析 Prometheus 接收器收集的資料。舉例來說,您可以按照「Google Cloud Monitoring 控制台」一文所述,使用 Metrics Explorer 查詢資料。您也可以建立 Cloud Monitoring 資訊主頁和警告政策,監控 Prometheus 指標。建議您使用 PromQL 做為 Prometheus 指標的查詢語言。

您也可以在 Cloud Monitoring 以外的介面 (例如 Prometheus UI 和 Grafana) 查看 Prometheus 指標。

選擇合適的接收端

決定使用 Prometheus 接收器前,請先確認您使用的應用程式是否已有 Ops Agent 整合。如要瞭解 Ops Agent 現有的整合功能,請參閱「監控第三方應用程式」。如果已有整合,建議您使用。詳情請參閱「選擇現有整合服務」。

如果符合下列條件,建議使用 Ops Agent Prometheus 接收器:

現有整合項目

Ops Agent 提供多種第三方應用程式的整合功能。這些整合功能可提供下列服務:

  • 應用程式的一組所選workload.googleapis.com指標
  • 以視覺化方式呈現指標的資訊主頁。

使用現有整合功能擷取的指標,會依據代理程式收集指標的位元組數計費。如要瞭解這項定價,請參閱「Google Cloud Observability 定價」。指標的數量和類型是預先得知的,您可以使用這項資訊來估算費用。

舉例來說,如果您使用 Apache Web Server (httpd) 整合,Ops Agent 會收集五個純量指標,每個資料點計為 8 個位元組。如果保留 Ops Agent 預設的取樣頻率 (60 秒),則每天擷取的位元組數為 57,600 * 主機數量:

  • 8 (位元組) * 1440 (每天分鐘數) * 5 (指標) * n (主機),或
  • 57,600 * n (主機)

如要進一步瞭解如何估算費用,請參閱「根據擷取的位元組計費示例」。

Prometheus 接收器

使用 Ops Agent 收集 Prometheus 指標時,請注意下列事項:

  • 您可以控制應用程式產生的指標數量和基數。沒有精選指標組合。擷取的資料量取決於 Prometheus 應用程式和 Ops Agent Prometheus 接收器的設定。

  • 指標會以 prometheus.googleapis.com 指標的形式擷取至 Cloud Monitoring。這些指標在擷取至 Cloud Monitoring 時,會歸類為「自訂」指標,並受限於自訂指標的配額和限制。

  • 您必須根據擷取的指標組合和業務需求,設計及建立所需的 Cloud Monitoring 資訊主頁。如要瞭解如何建立資訊主頁,請參閱「資訊主頁和圖表」。

  • 指標擷取費用是依擷取的樣本數計算。如要估算使用 Prometheus 接收器的費用,您必須判斷在帳單週期內可能收集的樣本數量。預估值是依據下列因素計算得出:

    • 純量指標數量;每個值都是一個樣本
    • 分布指標數量;每個直方圖計為 (2 + 直方圖中的值區數量) 個樣本
    • 各項指標的取樣頻率
    • 指標取樣來源主機數量

    如要進一步瞭解定價,請參閱「Google Cloud Observability 定價」。如需範例,請參閱「根據擷取的樣本計費示例」。

必要條件

如要使用 Prometheus 接收器收集 Prometheus 指標,您必須安裝 Ops Agent 2.25.0 以上版本。

Ops Agent 接收器需要會發出 Prometheus 指標的端點。因此,您的應用程式必須直接提供這類端點,或使用 Prometheus 程式庫或匯出工具公開端點。許多程式庫和語言架構 (例如 Spring 和 DropWizard),或應用程式 (例如 StatsD、DogStatsD 和 Graphite) 會發出非 Prometheus 指標,但可以使用 Prometheus 用戶端程式庫或匯出工具,發出 Prometheus 樣式的指標。例如,如要發布 Prometheus 指標:

當應用程式直接或使用程式庫或匯出工具發出 Prometheus 指標時,設定 Prometheus 接收器的 Ops Agent 就能收集這些指標。

設定 Ops Agent

Ops Agent 設定模型通常涉及定義下列項目:

  • 接收器:決定要收集哪些指標。
  • 處理器:說明 Ops Agent 如何修改指標。
  • 管道:將接收器和處理器連結至「服務」。

擷取 Prometheus 指標的設定略有不同:不涉及處理器。

Prometheus 指標設定

設定 Ops Agent 擷取 Prometheus 指標的方式與一般設定不同,如下所示:

  • 您不必為 Prometheus 指標建立 Ops Agent 處理器。Prometheus 接收器支援 Prometheus scrape_config 規格指定的大部分設定選項,包括重新標籤選項。

    任何指標處理作業都不會使用 Ops Agent 處理器,而是按照 Prometheus 接收器中的指定,使用抓取設定的 relabel_configs 和 metric_relabel_configs 區段完成。詳情請參閱「重新標籤:修改要擷取的資料」。

  • 您只需定義 Prometheus 接收器,即可定義 Prometheus 管道。未指定任何處理器。此外,您也無法在 Prometheus 指標的管道中使用任何非 Prometheus 接收器。

接收器設定的大部分內容都是 scrape-config 選項的規格。為求簡潔,以下省略這些選項,顯示使用 Prometheus 接收器的 Ops Agent 設定結構。您需要指定 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 設定 Ops Agent

本節說明如何設定 Ops Agent,從應用程式收集 Prometheus 指標。這個範例使用 Prometheus 社群提供的 JSON Exporter (json_exporter),在通訊埠 7979 公開 Prometheus 指標。

設定範例需要下列資源,您可能需要安裝:

  • git
  • curl
  • make
  • python3
  • Go 語言 1.19 以上版本

建立或設定應用程式

如要取得並執行 JSON Exporter,請按照下列程序操作:

  1. 複製 json_exporter 存放區,並執行下列指令來查看匯出工具:

    git clone https://github.com/prometheus-community/json_exporter.git
    
    cd json_exporter
    
    git checkout v0.5.0
    
  2. 執行下列指令,建構匯出工具:

    make build
    
  3. 執行下列指令,啟動 Python HTTP 伺服器:

    python3 -m http.server 8000 &
    
  4. 執行下列指令,啟動 JSON Exporter:

    ./json_exporter --config.file examples/config.yml &
    
  5. 查詢 JSON Exporter,確認其正在執行,並在 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 Exporter 應用程式擷取指標,您必須修改代理程式設定,新增 Prometheus 接收器和管道。如要使用 JSON Exporter 範例,請按照下列程序操作:

  1. 編輯 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

如要套用設定變更,請重新啟動 Ops Agent。

Linux

  1. 如要重新啟動代理程式,請在執行個體上執行下列指令:

    sudo service google-cloud-ops-agent restart
    
  2. 如要確定代理程式已重新啟動,請執行下列指令,並驗證「指標代理程式」和「Logging 代理程式」元件是否已啟動:

    sudo systemctl status google-cloud-ops-agent"*"
    

Windows

  1. 使用 RDP 或類似工具連線至執行個體,然後登入 Windows。

  2. 在 PowerShell 圖示上按一下滑鼠右鍵,然後選取「Run as Administrator」(以管理員身分執行),開啟具有管理員權限的 PowerShell 終端機。

  3. 如要重新啟動代理程式,請執行下列 PowerShell 指令:

    Restart-Service google-cloud-ops-agent -Force
    
  4. 如要確定代理程式已重新啟動,請執行下列指令,並驗證「指標代理程式」和「Logging 代理程式」元件是否已啟動:

    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 頁面:

  1. 前往 Google Cloud 控制台的「Metrics Explorer」頁面:

    前往「Metrics Explorer」

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。

  2. 在查詢建立工具窗格的工具列中,選取名為 「PromQL」PromQL的按鈕。

  3. 在編輯器中輸入下列查詢,然後點選「Run query」(執行查詢):

    up
    

如果資料正在擷取,您會看到類似下方的圖表:

json-exporter up 指標的 Metrics Explorer 圖表。

如果您執行 JSON Exporter 範例,也可以發出類似下列的查詢:

  • 依名稱查詢特定匯出指標的所有資料,例如:

    example_value_count
    

    下圖顯示 example_value_count 的圖表,包括 JSON Exporter 應用程式定義的標籤,以及 Ops Agent 新增的標籤:

    json-exporter example_value_count 指標的 Metrics Explorer 圖表。

  • 查詢源自特定命名空間的匯出指標資料。namespace 標籤的值是 Compute Engine 執行個體 ID,也就是指派給 VM 的長數字,例如 5671897148133813325。查詢範例如下:

    example_value_count{namespace="INSTANCE_ID"}
    
  • 查詢符合特定規則運算式的資料。JSON Exporter 會發出含有 id 標籤的指標,其值包括 id-A、id-B、id-C。如要篩選 id 標籤符合此模式的任何指標,請使用下列查詢:

    example_value_count{id=~"id.*"}
    

如要進一步瞭解如何在 Metrics Explorer 和 Cloud Monitoring 圖表中使用 PromQL,請參閱「Cloud Monitoring 中的 PromQL」。

在 Cloud Monitoring 查看指標使用狀況與診斷資訊

Cloud Monitoring 的「Metrics Management」(指標管理) 頁面提供相關資訊,可協助您控管可計費指標的支出,同時不影響觀測能力。「Metrics Management」(指標管理) 頁面會回報下列資訊:

  • 依位元組和樣本計費的擷取量,涵蓋指標網域和個別指標。
  • 指標標籤和基數的資料。
  • 每項指標的讀取次數。
  • 警告政策和自訂資訊主頁中的指標使用情形。
  • 指標寫入錯誤率。

您也可以使用「指標管理」頁面排除不必要的指標,避免產生擷取費用。

如要查看「Metrics Management」(指標管理) 頁面,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「Metrics management」(指標管理) 頁面:

    前往「Metrics management」(指標管理)

    如果您是使用搜尋列尋找這個頁面,請選取子標題為「Monitoring」的結果。

  2. 在工具列中選取時間範圍。根據預設,「Metrics Management」(指標管理) 頁面會顯示過去一天收集的指標資訊。

如要進一步瞭解「Metrics Management」(指標管理) 頁面,請參閱「查看及管理指標用量」。

prometheus_target 資源

在 Cloud Monitoring 中,時間序列資料會寫入受監控資源類型。如果是 Prometheus 指標,受監控資源類型為 prometheus_target。如果 Prometheus 指標並非以 PromQL 編寫,則 Monitoring 查詢必須指定這個資源類型。

prometheus_target 資源具有下列標籤,可用於篩選及操控查詢資料:

  • project_id:執行 Ops Agent 的 Google Cloud 專案 ID,例如 my-project。
  • location:執行 Ops Agent 的 Google Cloud 區域或可用區,例如 us-east1-a。
  • cluster:使用 Ops Agent 收集的 Prometheus 指標一律為 __gce__。
  • namespace:執行 Ops Agent 的 VM Compute Engine 執行個體 ID。
  • job:接收器設定中 job_name 欄位的值。
  • instance:Prometheus 目標的執行個體標籤,取自接收器設定,預設為目標。

這些標籤的值是在收集期間設定。namespace、location 和 cluster 標籤的值不可變更。如果從應用程式中擷取的指標也包含這些標籤,Ops Agent 會在擷取的標籤前面加上 exported_ 字串。

重新加上標籤:修改要擷取的資料

您可以使用重新標籤功能,在抓取目標之前,修改抓取目標或其指標的標籤集。如果重新標籤設定有多個步驟,系統會按照設定檔中的順序套用。

Ops Agent 會建立一組中繼標籤 (前置字串為 __meta_ 的標籤)。這些中繼標籤會記錄執行 Ops Agent 的 Compute Engine 執行個體相關資訊。只有在重新加上標籤時,才能使用前置字串為 __ 的標籤,包括中繼標籤。您可以使用重新標籤功能,在擷取的標籤中擷取這些標籤的值。

指標重新標籤會套用至樣本,這是擷取前的最後一個步驟。您可以使用指標重新標籤功能,捨棄不需要擷取的時間序列。捨棄這些時間序列可減少擷取的樣本數量,進而降低費用。

如要進一步瞭解重新加上標籤,請參閱 Prometheus 說明文件中的 relabel_config 和 metric_relabel_configs。

重新加上標籤時可用的 Compute Engine 中繼標籤

Ops Agent 抓取指標時,會加入一組中繼標籤,其值取決於執行代理程式的 Compute Engine VM 設定。您可以使用這些標籤和 Prometheus 接收器的 relabel_configs 區段,在指標中加入其他中繼資料,說明指標擷取自哪個 VM。如需範例,請參閱「其他接收器設定」。

目標提供下列中繼標籤,可在 relabel_configs 區段中使用:

  • __meta_gce_instance_id:Compute Engine 執行個體的數值 ID (本機)
  • __meta_gce_instance_name:Compute Engine 執行個體的名稱 (本機);Ops Agent 會自動將這個值放入指標的可變動 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 Exporter 範例的修改版接收器,該範例使用重新標籤選項。

新增 VM 中繼資料

您可以使用 relabel_configs 區段為指標新增標籤。舉例來說,下列範例使用 Ops Agent 提供的中繼標籤 __meta_gce_zone 建立指標標籤 zone,該標籤在重新加上標籤後會保留,因為 zone 沒有 __ 前置字串。

如需可用中繼標籤的清單,請參閱「重新加上標籤時可用的 Compute Engine 中繼標籤」。預設的 Ops Agent 設定會重新標示部分中繼標籤。

relabel_configs:
  - source_labels: [__meta_gce_zone]
    regex: '(.+)'
    replacement: '${1}'
    target_label: zone

「範例:設定 Prometheus 的 Ops Agent」中顯示的 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 Exporter 範例的 Prometheus 接收器版本使用這些設定模式執行下列操作:

  • 根據 Ops Agent 提供的 __meta_gce_zone 中繼標籤值,設定 zone 標籤。
  • 捨棄匯出工具的 example_global_value 指標。
  • 為所有擷取的指標新增 staticLabel 標籤,並將值設為「A static value」。
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