Batch jobs, short-lived processes, and network-isolated environments cannot expose a stable HTTP endpoint for Prometheus to scrape. Pushgateway solves this by accepting pushed metrics and holding them for the Prometheus server to pull on its regular scrape interval. Use Pushgateway in Managed Service for Prometheus to collect metrics from these data sources without modifying your network topology.
How it works
Your data source pushes metrics to Pushgateway, and the Prometheus server periodically pulls the data from Pushgateway.
To set this up:
Get the Pushgateway endpoint from the Managed Service for Prometheus console.
Push metric data using curl or a Pushgateway SDK (Go, Java).
(Optional) Enable token-based authentication to restrict who can push data.
Prerequisites
Before you begin, make sure you have:
A Prometheus instance for your monitoring target. To create one, see:
Supported data formats
| Format | Status |
|---|---|
| Text Format | Supported |
| Protobuf Delimited | Supported (SDK default) |
| Protobuf Text | Not supported |
| Protobuf Compact Text | Not supported |
| OpenMetrics | Not supported |
The Help field in metric metadata does not support Chinese characters. Pushing metrics with Chinese characters in this field causes data reporting to fail.
Step 1: Get the Pushgateway endpoint
Log on to the ARMS console. In the left-side navigation pane, choose Managed Service for Prometheus > Instances.
Click the name of the Prometheus instance to open its details page.

In the left-side navigation pane, click Settings. On the Settings tab, copy the public URL from the Push Gateway URL section.

Step 2: Push metric data
Push metric data using a Pushgateway SDK or curl.
Use a Pushgateway SDK
All SDK examples below push metrics to the Pushgateway endpoint from Step 1. The SDK automatically appends a URL suffix in the format /metrics/job/<JOB_NAME>{/<LABEL_NAME>/<LABEL_VALUE>}.
If you are not using an open-source Pushgateway SDK, append this suffix manually. Otherwise, a 404 error is returned.
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)
}Replace the url value with the Pushgateway endpoint from your console.
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");
}Replace the URL in new PushGateway(new URL(...)) with the Pushgateway endpoint from your console.
Use curl
Requests with content type application/x-www-form-urlencoded are not supported. Set Content-Type: text/plain; version=0.0.4; charset=utf-8 in the request header.
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/label_key_1/label_value_1/label_key_2/label_value_2Replace the URL with the Pushgateway endpoint from your console. Append labels as key-value pairs at the end of the URL path. Keep the total URL length within reasonable limits.
Push to a multi-tenant cluster
To push metric data to a cluster shared by multiple tenants, attach the tenant_userid=**** tag to each metric. Set the tag value to the Alibaba Cloud account ID that owns the metrics.
Step 3 (optional): Secure Pushgateway with token authentication
By default, any client with the Pushgateway endpoint can push data. To restrict access, generate a JSON Web Token (JWT) and include it in every request.
Generate a token
In the left-side navigation pane, click Settings.
On the Settings tab, click Generate Token in the Token section.

Pass the token in requests
Use one of the following methods to include the token:
Method 1: Add the token to the request header
Add the JWT to the Authorization header of every push request. Requests without a valid token are rejected.

Method 2: Use BasicAuth (SDK-compatible)
Method 1 may increase your development cost. Alternatively, the Pushgateway SDK supports basic authentication but not JWT directly. To work around this, pass the token through the BasicAuth interface by setting the password field to your token value. The server accepts this method.
pusher := push.New(url, "test").
Collector(completionTime).Client(http.DefaultClient).
Grouping("key1", "test1").Grouping("key2", "dfdf/sdsd/").
BasicAuth("admin", "<your-token-value>").
Format(expfmt.FmtProtoDelim)Replace <your-token-value> with the token generated in the console.
Verify the result
Confirm that metric data is arriving by querying it in Grafana.
In the left-side navigation pane, click Dashboards, then click ApiServer.
Hover over the
icon in the left-side navigation pane and click Explore.Select your resource from the drop-down list and query the metrics you pushed.
