全部產品
Search
文件中心

Managed Service for Prometheus:如何使用Pushgateway推送資料

更新時間:Dec 24, 2025

本文介紹如何使用可觀測監控 Prometheus 版提供的Pushgateway功能推送資料。

方案概覽

如果您的資料來源不能或不可以定期被Prometheus Server拉取資料(例如在沒有穩定網路連接的環境中),您可以使用Pushgateway推送,資料來源會先將監控資料發送到Pushgateway,再由Prometheus Server周期性地擷取,實現步驟如下:

  1. 擷取Pushgateway地址:通過可觀測監控 Prometheus 版控制台擷取Pushgateway地址。

  2. 上報資料:使用curl命令或者開源SDK實現資料推送,確保指標資料能夠及時、可靠地被Prometheus收集並進行監控。

  3. 增加資料保護配置(可選):標準的Pushgateway協議是不包含資料保護相關內容的,在Pushgateway的SDK中只有基本的Basic Auth,並沒有更進階和通用的鑒權實現,即任何用戶端一旦擷取Pushgateway端點地址,都可以推送資料。在可觀測監控 Prometheus 版控制台擷取Token,實現標準的JWT鑒權協議,從而保護您的資料安全。

前提條件

已建立Prometheus執行個體。具體操作,請參見:

步驟一:擷取Push Gateway地址

  1. 登入CloudMonitor控制台,在左側導覽列選擇Prometheus 監控 > 執行個體列表,進入執行個體列表頁面。

  2. 在頁面頂部功能表列選擇Prometheus執行個體所在的地區,並在目的地組群右側的操作列單擊設定

  3. 設定頁簽的Push Gateway 地址地區擷取公網的URL地址。

    image

步驟二:上報資料

V1版本

使用開源SDK推送資料

重要
  • 目前資料協議支援Text Format和Protobuf Delimited這兩種資料層協議,暫不支援Protobuf Text、Protobuf Compact-Text和Openmetrics這三種協議,SDK一般預設是Protobuf Delimited協議。

  • 目前中繼資料HELP不支援中文字元,如果HELP中有中文字元會導致資料上報失敗。

本文以Go語言和Java語言為例介紹如何使用開源SDK推送指標資料。

Go語言樣本如下:

completionTime := prometheus.NewGauge(prometheus.GaugeOpts{
    Name: "db_backup_last_completion_timestamp_seconds",
    Help: "The timestamp of the last successful completion of a DB backup.",
})
completionTime.SetToCurrentTime()
url :   = "https://cn-hangzhou.arms.aliyuncs.com/prometheus/52b12ea9cf4bb9e35****/16727530178****/1df8lj***/cn-hangzhou/api/v2"
pusher := push.New(url, "test").
    Collector(completionTime).Client(http.DefaultClient).
    Grouping("key1", "test1").Grouping("key2", "dfdf/sdsd/").
    Format(expfmt.FmtProtoDelim)
if err := pusher.Push(); err != nil {
    fmt.Println("Could not push completion time to PushGateway: ", err)
}

Java語言樣本如下:

CollectorRegistry registry = new CollectorRegistry();
Gauge duration = Gauge.build()
        .name("my_batch_job_duration_seconds").help("Duration of my batch job in seconds.").register(registry);
Gauge.Timer durationTimer = duration.startTimer();
try {
    // Your code here.

    // This is only added to the registry after success,
    // so that a previous success in the Pushgateway isn't overwritten on failure.
    Gauge lastSuccess = Gauge.build()
            .name("my_batch_job_last_success").help("Last time my batch job succeeded, in unixtime.").register(registry);
    lastSuccess.setToCurrentTime();
} finally {
    durationTimer.setDuration();
    PushGateway pg = new PushGateway(new URL("https://cn-hangzhou.arms.aliyuncs.com/prometheus/52b12ea9cf4bb9e35****/16727530178****/1df8lj***/cn-hangzhou/api/v2"));
    pg.pushAdd(registry, "my_batch_job");
}
說明
  • 在您使用開源SDK時,填入從Prometheus監控控制台上擷取的Pushgateway地址後,系統會自動補齊類似/metrics/job/<JOB_NAME>{/<LABEL_NAME>/<LABEL_VALUE>}的尾碼。如果您使用的不是開源SDK,那麼需要您自行拼接尾碼,否則會報404錯誤。

  • 若您需要為可觀測監控 Prometheus 版雲端服務共用租戶叢集中推送資料,要求推送的指標需統一攜帶tenant_userid=****標籤,標籤值是該指標隸屬的阿里雲帳號ID(即主帳號ID),用於區分指標的隸屬關係。

使用curl命令推送資料

重要

目前不支援application/x-www-form-urlencoded類型的Request,在curl命令中,需要增加Header,指定Content-Type: text/plain; version=0.0.4; charset=utf-8

echo "some_metric 3.14" | curl -H "Content-Type: text/plain; version=0.0.4; charset=utf-8" --data-binary @- https://cn-hangzhou.arms.aliyuncs.com/prometheus/51bbea9ck41b9e35****/16727530178****/1df8lj***/cn-hangzhou/api/v2/metrics/job/job_name/label_key_1/label_value_1/label_key_2/label_value_2
說明
  • 多個Label可以在URL後面拼接,但需要注意整體URL長度。

  • 根據實際情況,修改URL中“job_name”為實際的job名。

增加資料保護配置(可選)

  1. 擷取token:在左側導覽列,選擇設定,然後在設定頁簽的Token地區單擊產生token

    image

  2. 傳遞token:產生Token後,您可以看到具體的Token值,有以下兩種方式傳遞Token。

    • 方式一:將Token設定到用戶端請求Header裡,即可正常的推送資料,否則系統會拒絕資料寫入。Header的設定如下圖所示:vr

    • 方式二:由於在Pushgateway的SDK裡只有基本的Basic Auth,並沒有支援JWT。如果想要完全使用SDK並實現鑒權,可以使用BasicAuth介面,將Password設定為Token,服務側相容了這種鑒權方式。使用第一種方式有一定的開發成本。這裡以Go語言SDK為例。

      pusher := push.New(url, "test").
              Collector(completionTime).Client(http.DefaultClient).
              Grouping("key1", "test1").Grouping("key2", "dfdf/sdsd/").
              .BasicAuth("admin", "實際token值").
              Format(expfmt.FmtProtoDelim)

V2版本

1. 為RAM使用者授予CMS讀寫權限

如果您的阿里雲Prometheus執行個體是由阿里雲主帳號建立,且您需要使用RAM使用者的AccessKey ID和AccessKey Secret進行遠程讀寫,則需要先為RAM使用者授予CMS的讀寫權限。

  1. 使用阿里雲主帳號或Resource Access Management員登入RAM控制台,在左側導覽列選擇許可權管理 > 授權

  2. 授權頁面,單擊新增授權。在新增授權頁面進行如下配置。

    參數

    說明

    資源範圍

    按需選擇資源範圍。

    授權主體

    指定授權主體,即需要添加許可權的RAM使用者。

    權限原則

    選中AliyunPrometheusMetricWriteAccessAliyunCloudMonitorFullAccess

  3. 單擊確認新增授權,單擊關閉

2. 推送資料

使用開源SDK推送資料

重要
  • 目前資料協議支援Text Format和Protobuf Delimited這兩種資料層協議,暫不支援Protobuf Text、Protobuf Compact-Text和Openmetrics這三種協議,SDK一般預設是Protobuf Delimited協議。

  • 目前中繼資料HELP不支援中文字元,如果HELP中有中文字元會導致資料上報失敗。

本文以Go語言和Java語言為例介紹如何使用開源SDK推送指標資料。

Go語言樣本如下:

completionTime := prometheus.NewGauge(prometheus.GaugeOpts{
    Name: "db_backup_last_completion_timestamp_seconds",
    Help: "The timestamp of the last successful completion of a DB backup.",
})
completionTime.SetToCurrentTime()
url :   = "https://k8s-log-c8b5d0a212ffa41cxxxx.cn-hangzhou.log.aliyuncs.com/prometheus/k8s-log-c8b5d0a212ffa41c0a5xxxx/aliyun-prom-c8b5d0a212ffa41c0xxxx/api/v1/pushgateway"
pusher := push.New(url, "test").
    Collector(completionTime).Client(http.DefaultClient).
    Grouping("key1", "test1").Grouping("key2", "dfdf/sdsd/").
    BasicAuth("ak", "sk").  
    Format(expfmt.FmtProtoDelim)
if err := pusher.Push(); err != nil {
    fmt.Println("Could not push completion time to PushGateway: ", err)
}

Java語言樣本如下:

CollectorRegistry registry = new CollectorRegistry();
Gauge duration = Gauge.build()
        .name("my_batch_job_duration_seconds").help("Duration of my batch job in seconds.").register(registry);
Gauge.Timer durationTimer = duration.startTimer();
try {
    // Your code here.

    // This is only added to the registry after success,
    // so that a previous success in the Pushgateway isn't overwritten on failure.
    Gauge lastSuccess = Gauge.build()
            .name("my_batch_job_last_success").help("Last time my batch job succeeded, in unixtime.").register(registry);
    lastSuccess.setToCurrentTime();
} finally {
    durationTimer.setDuration();
    PushGateway pg = new PushGateway(new URL("https://k8s-log-c8b5d0a212ffa41cxxxx.cn-hangzhou.log.aliyuncs.com/prometheus/k8s-log-c8b5d0a212ffa41c0a5xxxx/aliyun-prom-c8b5d0a212ffa41c0xxxx/api/v1/pushgateway"));       
    pg.setConnectionFactory(new BasicAuthHttpConnectionFactory("ak", "sk"));  
    pg.pushAdd(registry, "my_batch_job");
}
說明
  • 在您使用開源SDK時,填入從Prometheus監控控制台上擷取的Pushgateway地址後,系統會自動補齊類似/metrics/job/<JOB_NAME>{/<LABEL_NAME>/<LABEL_VALUE>}的尾碼。如果您使用的不是開源SDK,那麼需要您自行拼接尾碼,否則會報404錯誤。

  • 若您需要為可觀測監控 Prometheus 版雲端服務共用租戶叢集中推送資料,要求推送的指標需統一攜帶tenant_userid=****標籤,標籤值是該指標隸屬的阿里雲帳號ID(即主帳號ID),用於區分指標的隸屬關係。

  • API介面支援STS鑒權。此時BasicAuth中的Password格式為{AccessKey Secret}${STS Token}

使用curl命令推送資料

重要

目前不支援application/x-www-form-urlencoded類型的Request,在curl命令中,需要增加Header,指定Content-Type: text/plain; version=0.0.4; charset=utf-8

echo "some_metric 3.14" | curl -u 'ak:sk' -H "Content-Type: text/plain; version=0.0.4; charset=utf-8" --data-binary @- https://k8s-log-c8b5d0a212ffa41cxxxx.cn-hangzhou.log.aliyuncs.com/prometheus/k8s-log-c8b5d0a212ffa41c0a5xxxx/aliyun-prom-c8b5d0a212ffa41c0xxxx/api/v1/pushgateway/metrics/job/job_name/label_key_1/label_value_1/label_key_2/label_value_2
說明
  • 多個Label可以在URL後面拼接,但需要注意整體URL長度。

  • 根據實際情況,修改URL中“job_name”為實際的job名。

結果驗證

您可以通過Grafana查詢資料是否成功推送。

  1. 進入ApiServer大盤:在左側導覽列,選擇大盤列表,然後單擊名稱為ApiServer的大盤超連結,系統會跳轉至大盤頁面。

  2. 選擇Explore查看資料:在大盤頁面的左側導覽列將滑鼠移至上方在eu表徵圖上(表徵圖①),並在彈框中單擊Explore,然後在Explore頁面右側的下拉框中(表徵圖②)選擇對應Explore查看資料是否推送成功。

    wt