## 推送度量指標
---
偶爾你需要監控不能被獲取的實例。它們可能被防火墻保護,或者它們生命周期太短而不能通過拉模式獲取數據。Prometheus的Pushgateway允許你將這些實例的時間序列數據推送到Prometheus的代理任務中。結合Prometheus簡單的文本導出格式,這使得即使沒有客戶庫,也能使用shell腳本獲取數據。
- shell實現用例,查看[Readme](https://github.com/prometheus/pushgateway/blob/master/README.md)
- Java, 詳見[PushGateway](https://prometheus.io/client_java/io/prometheus/client/exporter/PushGateway.html)類
- Go,詳見[Push](http://godoc.org/github.com/prometheus/client_golang/prometheus#Push)和[PushAdd](http://godoc.org/github.com/prometheus/client_golang/prometheus#PushAdd)
- Python, 詳見[Pushgateway](https://github.com/prometheus/client_python#exporting-to-a-pushgateway)
- Ruby, 詳見[Pushgateway](https://github.com/prometheus/client_ruby#pushgateway)
### Java批量任務例子
這個例子主要說明, 如何執行一個批處理任務,并且在沒有執行成功時報警
如果使用Maven,添加下面的代碼到`pom.xml`文件中:
```Java
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>0.0.10</version>
</dependency>
```
執行批量作業的代碼:
```Java
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.PushGateway;
void executeBatchJob() throws Exception {
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 is not overwritten on failure.
Gauge lastSuccess = Gauge.build()
.name("my_batch_job_last_success_unixtime")
.help("Last time my batch job succeeded, in unixtime.")
.register(registry);
lastSuccess.setToCurrentTime();
} finally {
durationTimer.setDuration();
PushGateway pg = new PushGateway("127.0.0.1:9091");
pg.pushAdd(registry, "my_batch_job");
}
}
```
警報一個Pushgateway,如果需要的話,修改host和port
如果任務最近沒有運行,請創建一個警報到Alertmanager。將以下內容添加到Pushgateway的Prometheus服務的記錄規則中:
```record rules
ALERT MyBatchJobNotCompleted
IF min(time() - my_batch_job_last_success_unixtime{job="my_batch_job"}) > 60 * 60
FOR 5m
WITH { severity="page" }
SUMMARY "MyBatchJob has not completed successfully in over an hour"
```