use of io.prometheus.client.exporter.BasicAuthHttpConnectionFactory in project dsbulk by datastax.
the class PrometheusManager method pushMetrics.
public void pushMetrics(Duration elapsed, boolean success) {
if (pushConfig != null) {
try {
CollectorRegistry collectorRegistry = new CollectorRegistry();
new DropwizardExports(registry, PUSH_METRIC_FILTER, sampleBuilder).register(collectorRegistry);
addFinalMetrics(elapsed, success, collectorRegistry);
PushGateway pg = new PushGateway(pushConfig.gatewayUrl);
if (!pushConfig.username.isEmpty() && !pushConfig.password.isEmpty()) {
pg.setConnectionFactory(new BasicAuthHttpConnectionFactory(pushConfig.username, pushConfig.password));
}
Map<String, String> groupingKeys = new LinkedHashMap<>();
if (pushConfig.groupByInstance) {
groupingKeys.putAll(PushGateway.instanceIPGroupingKey());
}
if (pushConfig.groupByOperation) {
groupingKeys.put(OPERATION_ID_LABEL, executionId);
}
groupingKeys.putAll(pushConfig.groupByKeys);
pg.pushAdd(collectorRegistry, jobName, groupingKeys);
} catch (Exception e) {
LOGGER.error(String.format("Push to Prometheus PushGateway %s failed. %s", pushConfig.gatewayUrl, ThrowableUtils.getSanitizedErrorMessage(e)), e);
}
}
}
use of io.prometheus.client.exporter.BasicAuthHttpConnectionFactory in project client_java by prometheus.
the class ExampleBatchJob method main.
// The following is copy-and-paste from README.md
// except that we added basic authentication to test the BasicAuthHttpConnectionFactory with different Java versions.
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.err.println("Usage: batch-job.jar <address> <user> <password>");
System.exit(-1);
}
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 {
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(args[0]);
pg.setConnectionFactory(new BasicAuthHttpConnectionFactory(args[1], args[2]));
pg.pushAdd(registry, "my_batch_job");
}
}
Aggregations