All Products
Search
Document Center

Managed Service for Prometheus:Use Pushgateway to push metric data

Last Updated:Mar 12, 2024

This topic describes how to use the Pushgateway feature of Managed Service for Prometheus.

Prerequisites

Background information

Alibaba Cloud Managed Service for Prometheus provides the Pushgateway feature. You can use the Pushgateway API, open source Pushgateway SDK, and Managed Service for Prometheus console to deliver metrics.

Step 1: Obtain the Pushgateway endpoint

https://cn-heyuan.arms.aliyuncs.com/prometheus/${password}/${userid}/${clusterid}/cn-heyuan/api/v2 is a sample Pushgateway endpoint. You can perform the following steps to obtain a Pushgateway endpoint.

  1. Log on to the Managed Service for Prometheus console.

  2. In the left-side navigation pane, click Instances.

  3. Click the name of the Prometheus instance.

  4. In the left-side navigation pane, click Settings. On the Settings tab, obtain the public endpoint in the Push Gateway Address section.

    Important

    The Pushgateway endpoint provided in the Managed Service for Prometheus console is used together with the open source Pushgateway SDK. When you use the open source Pushgateway SDK, enter the Pushgateway endpoint. The system automatically replaces the values of suffixes. For example, the system automatically replaces the values of suffixes in /metrics/job/<JOB_NAME>{/<LABEL_NAME>/<LABEL_VALUE>}. If you do not use the Pushgateway SDK, you must manually replace the values of suffixes. Otherwise, a 404 error is reported.

Step 2: Report data

Important

Only the Text Format and Protobuf Delimited specifications are supported. The Protobuf Text, Protobuf Compact Text, and OpenMetrics specifications are not supported. By default, the Pushgateway SDK is used with Protobuf Delimited.

Run the curl command to push metric data

You can run the curl command to debug the data pushing feature. Example:

Important

Requests of the application/x-www-form-urlencoded type are not supported. In the curl command, you must add a header to specify 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/some_job

Use the open source Pushgateway SDK to push metric data

After you obtain the Pushgateway endpoint, you can use the open source Pushgateway SDK to push metric data. In this example, Pushgateway SDK for Go and Pushgateway SDK for Java are used.

Pushgateway SDK for 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)
}

Pushgateway SDK for 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");
}
Note

If you need to push metric data to a cluster that is shared by multiple tenants and monitored by Managed Service for Prometheus, attach the tenant_userid=**** tag to the metrics. The tag value is used to distinguish the relationships between the metrics. Set the tag value to the ID of the Alibaba Cloud account to which the metrics belong.

Step 3: Configure data protection (optional)

The standard Pushgateway protocol does not define data protection. All clients can push data whenever they obtain the Pushgateway endpoint. This cannot meet the requirements for high security.

Note

Data protection is not part of the standard Pushgateway protocol. Therefore, the Pushgateway SDK does not provide common or advanced authentication methods except basic authentication.

Alibaba Cloud Managed Service for Prometheus implements the standard JSON web token(JWT) authentication to ensure your data security. You can enable JWT authentication and obtain a token by using the Managed Service for Prometheus console or OpenAPI Explorer with simple configurations. In this example, a token is obtained in the Managed Service for Prometheus console.

  1. Log on to the Managed Service for Prometheus console.

  2. In the left-side navigation pane, click Instances.

  3. Click the name of the Prometheus instance.

  4. In the left-side navigation pane, click Settings. On the Settings tab, click Generate token in the Token section.

    After a token is generated, you can view the value of the token. Then, use one of the following methods to pass the token:

    • Method 1: Add the token to the request header of the client request. Then, you can push metric data. Otherwise, the system rejects data writes. The following figure shows how to add the token to the request header of the client request.vr

    • Method 2: The Pushgateway SDK supports only basic authentication and does not support JWT authentication. Therefore, the preceding method may increase your development cost. If you want to use the Pushgateway SDK and implement advanced authentication at the same time, you can use the BasicAuth interface and set the Password field to Token. The service side is compatible with the authentication method. Pushgateway SDK for Go is used as an example:

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

Step 4: Use Grafana to check whether the metric data is pushed

  1. Log on to the Managed Service for Prometheus console.

  2. In the left-side navigation pane, click Instances.

  3. Click the name of the Prometheus instance.

  4. In the left-side navigation pane, click Dashboards. On the Dashboards page, click the ApiServer dashboard. The dashboard details page appears.

  5. In the left-side navigation pane, move the pointer over the ce icon (marked with 1) and click Explore. In the top drop-down list of the Explore page (marked with 2), check whether the metric data exists.

    wt