Class PushGateway
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");
}
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate HttpConnectionFactoryprotected final Stringprivate static final int -
Constructor Summary
ConstructorsConstructorDescriptionPushGateway(String address) Construct a Pushgateway, with the given address.PushGateway(URL serverBaseURL) Construct a Pushgateway, with the given URL. -
Method Summary
Modifier and TypeMethodDescriptionprivate static Stringprivate static URLcreateURLSneakily(String urlString) Creates a URL instance from a String representation of a URL without throwing a checked exception.voidDeletes metrics from the Pushgateway.voidDeletes metrics from the Pushgateway.(package private) voidReturns a grouping key with the instance label set to the machine's IP address.voidpush(CollectorRegistry registry, String job) Pushes all metrics in a registry, replacing all those with the same job and no grouping key.voidPushes all metrics in a registry, replacing all those with the same job and grouping key.voidPushes all metrics in a Collector, replacing all those with the same job and no grouping key.voidPushes all metrics in a Collector, replacing all those with the same job and grouping key.voidpushAdd(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.voidPushes all metrics in a registry, replacing only previously pushed metrics of the same name, job and grouping key.voidPushes all metrics in a Collector, replacing only previously pushed metrics of the same name and job and no grouping key.voidPushes all metrics in a Collector, replacing only previously pushed metrics of the same name, job and grouping key.private static StringvoidsetConnectionFactory(HttpConnectionFactory connectionFactory)
-
Field Details
-
MILLISECONDS_PER_SECOND
private static final int MILLISECONDS_PER_SECOND- See Also:
-
gatewayBaseURL
-
connectionFactory
-
-
Constructor Details
-
PushGateway
Construct a Pushgateway, with the given address.- Parameters:
address- host:port or ip:port of the Pushgateway.
-
PushGateway
Construct a Pushgateway, with the given URL.- Parameters:
serverBaseURL- the base URL and optional context path of the Pushgateway server.
-
-
Method Details
-
setConnectionFactory
-
createURLSneakily
Creates a URL instance from a String representation of a URL without throwing a checked exception. Required because you can't wrap a call to another constructor in a try statement.- Parameters:
urlString- the String representation of the URL.- Returns:
- The URL instance.
-
push
Pushes all metrics in a registry, replacing all those with the same job and no grouping key.This uses the PUT HTTP method.
- Throws:
IOException
-
push
Pushes all metrics in a Collector, replacing all those with the same job and no grouping key.This is useful for pushing a single Gauge.
This uses the PUT HTTP method.
- Throws:
IOException
-
push
public void push(CollectorRegistry registry, String job, Map<String, String> groupingKey) throws IOExceptionPushes all metrics in a registry, replacing all those with the same job and grouping key.This uses the PUT HTTP method.
- Throws:
IOException
-
push
public void push(Collector collector, String job, Map<String, String> groupingKey) throws IOExceptionPushes all metrics in a Collector, replacing all those with the same job and grouping key.This is useful for pushing a single Gauge.
This uses the PUT HTTP method.
- Throws:
IOException
-
pushAdd
Pushes all metrics in a registry, replacing only previously pushed metrics of the same name and job and no grouping key.This uses the POST HTTP method.
- Throws:
IOException
-
pushAdd
Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name and job and no grouping key.This is useful for pushing a single Gauge.
This uses the POST HTTP method.
- Throws:
IOException
-
pushAdd
public void pushAdd(CollectorRegistry registry, String job, Map<String, String> groupingKey) throws IOExceptionPushes all metrics in a registry, replacing only previously pushed metrics of the same name, job and grouping key.This uses the POST HTTP method.
- Throws:
IOException
-
pushAdd
public void pushAdd(Collector collector, String job, Map<String, String> groupingKey) throws IOExceptionPushes all metrics in a Collector, replacing only previously pushed metrics of the same name, job and grouping key.This is useful for pushing a single Gauge.
This uses the POST HTTP method.
- Throws:
IOException
-
delete
Deletes metrics from the Pushgateway.Deletes metrics with no grouping key and the provided job. This uses the DELETE HTTP method.
- Throws:
IOException
-
delete
Deletes metrics from the Pushgateway.Deletes metrics with the provided job and grouping key. This uses the DELETE HTTP method.
- Throws:
IOException
-
doRequest
void doRequest(CollectorRegistry registry, String job, Map<String, String> groupingKey, String method) throws IOException- Throws:
IOException
-
base64url
-
instanceIPGroupingKey
Returns a grouping key with the instance label set to the machine's IP address.This is a convenience function, and should only be used where you want to push per-instance metrics rather than cluster/job level metrics.
- Throws:
UnknownHostException
-
readFromStream
- Throws:
IOException
-