public class PushGateway extends Object
The Prometheus Pushgateway exists to allow ephemeral and batch jobs to expose their metrics to Prometheus.
Since these kinds of jobs may not exist long enough to be scraped, they can instead push their metrics
to a Pushgateway. This class allows pushing the contents of a CollectorRegistry to
a Pushgateway.
Example usage:
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 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("127.0.0.1:9091");
pg.pushAdd(registry, "my_batch_job");
}
}
| Modifier and Type | Field and Description |
|---|---|
protected String |
gatewayBaseURL |
| Constructor and Description |
|---|
PushGateway(String address)
Construct a Pushgateway, with the given address.
|
PushGateway(URL serverBaseURL)
Construct a Pushgateway, with the given URL.
|
| Modifier and Type | Method and Description |
|---|---|
void |
delete(String job)
Deletes metrics from the Pushgateway.
|
void |
delete(String job,
Map<String,String> groupingKey)
Deletes metrics from the Pushgateway.
|
void |
delete(String job,
String instance)
Deprecated.
|
static Map<String,String> |
instanceIPGroupingKey()
Returns a grouping key with the instance label set to the machine's IP address.
|
void |
push(io.prometheus.client.CollectorRegistry registry,
String job)
Pushes all metrics in a registry, replacing all those with the same job and no grouping key.
|
void |
push(io.prometheus.client.CollectorRegistry registry,
String job,
Map<String,String> groupingKey)
Pushes all metrics in a registry, replacing all those with the same job and grouping key.
|
void |
push(io.prometheus.client.CollectorRegistry registry,
String job,
String instance)
Deprecated.
|
void |
push(io.prometheus.client.Collector collector,
String job)
Pushes all metrics in a Collector, replacing all those with the same job and no grouping key.
|
void |
push(io.prometheus.client.Collector collector,
String job,
Map<String,String> groupingKey)
Pushes all metrics in a Collector, replacing all those with the same job and grouping key.
|
void |
push(io.prometheus.client.Collector collector,
String job,
String instance)
Deprecated.
|
void |
pushAdd(io.prometheus.client.CollectorRegistry registry,
String job)
Pushes all metrics in a registry, replacing only previously pushed metrics of the same name and job and no grouping key.
|
void |
pushAdd(io.prometheus.client.CollectorRegistry registry,
String job,
Map<String,String> groupingKey)
Pushes all metrics in a registry, replacing only previously pushed metrics of the same name, job and grouping key.
|
void |
pushAdd(io.prometheus.client.CollectorRegistry registry,
String job,
String instance)
Deprecated.
|
void |
pushAdd(io.prometheus.client.Collector collector,
String job)
Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name and job and no grouping key.
|
void |
pushAdd(io.prometheus.client.Collector collector,
String job,
Map<String,String> groupingKey)
Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name, job and grouping key.
|
void |
pushAdd(io.prometheus.client.Collector collector,
String job,
String instance)
Deprecated.
|
void |
setConnectionFactory(HttpConnectionFactory connectionFactory) |
protected final String gatewayBaseURL
public PushGateway(String address)
address - host:port or ip:port of the Pushgateway.public PushGateway(URL serverBaseURL)
serverBaseURL - the base URL and optional context path of the Pushgateway server.public void setConnectionFactory(HttpConnectionFactory connectionFactory)
public void push(io.prometheus.client.CollectorRegistry registry, String job) throws IOException
This uses the PUT HTTP method.
IOExceptionpublic void push(io.prometheus.client.Collector collector, String job) throws IOException
This is useful for pushing a single Gauge.
This uses the PUT HTTP method.
IOExceptionpublic void push(io.prometheus.client.CollectorRegistry registry, String job, Map<String,String> groupingKey) throws IOException
This uses the PUT HTTP method.
IOExceptionpublic void push(io.prometheus.client.Collector collector, String job, Map<String,String> groupingKey) throws IOException
This is useful for pushing a single Gauge.
This uses the PUT HTTP method.
IOExceptionpublic void pushAdd(io.prometheus.client.CollectorRegistry registry, String job) throws IOException
This uses the POST HTTP method.
IOExceptionpublic void pushAdd(io.prometheus.client.Collector collector, String job) throws IOException
This is useful for pushing a single Gauge.
This uses the POST HTTP method.
IOExceptionpublic void pushAdd(io.prometheus.client.CollectorRegistry registry, String job, Map<String,String> groupingKey) throws IOException
This uses the POST HTTP method.
IOExceptionpublic void pushAdd(io.prometheus.client.Collector collector, String job, Map<String,String> groupingKey) throws IOException
This is useful for pushing a single Gauge.
This uses the POST HTTP method.
IOExceptionpublic void delete(String job) throws IOException
Deletes metrics with no grouping key and the provided job. This uses the DELETE HTTP method.
IOExceptionpublic void delete(String job, Map<String,String> groupingKey) throws IOException
Deletes metrics with the provided job and grouping key. This uses the DELETE HTTP method.
IOException@Deprecated public void push(io.prometheus.client.CollectorRegistry registry, String job, String instance) throws IOException
push(CollectorRegistry, String, Map)This uses the PUT HTTP method.
IOException@Deprecated public void push(io.prometheus.client.Collector collector, String job, String instance) throws IOException
push(Collector, String, Map)This is useful for pushing a single Gauge.
This uses the PUT HTTP method.
IOException@Deprecated public void pushAdd(io.prometheus.client.CollectorRegistry registry, String job, String instance) throws IOException
pushAdd(CollectorRegistry, String, Map)This uses the POST HTTP method.
IOException@Deprecated public void pushAdd(io.prometheus.client.Collector collector, String job, String instance) throws IOException
pushAdd(Collector, String, Map)This is useful for pushing a single Gauge.
This uses the POST HTTP method.
IOException@Deprecated public void delete(String job, String instance) throws IOException
delete(String, Map)This uses the DELETE HTTP method.
IOExceptionpublic static Map<String,String> instanceIPGroupingKey() throws UnknownHostException
This is a convenience function, and should only be used where you want to push per-instance metrics rather than cluster/job level metrics.
UnknownHostExceptionCopyright © 2018. All rights reserved.