Search in sources :

Example 1 with BasicAuthHttpConnectionFactory

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);
        }
    }
}
Also used : DropwizardExports(io.prometheus.client.dropwizard.DropwizardExports) CollectorRegistry(io.prometheus.client.CollectorRegistry) PushGateway(io.prometheus.client.exporter.PushGateway) BasicAuthHttpConnectionFactory(io.prometheus.client.exporter.BasicAuthHttpConnectionFactory) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with BasicAuthHttpConnectionFactory

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");
    }
}
Also used : CollectorRegistry(io.prometheus.client.CollectorRegistry) PushGateway(io.prometheus.client.exporter.PushGateway) BasicAuthHttpConnectionFactory(io.prometheus.client.exporter.BasicAuthHttpConnectionFactory) Gauge(io.prometheus.client.Gauge)

Aggregations

CollectorRegistry (io.prometheus.client.CollectorRegistry)2 BasicAuthHttpConnectionFactory (io.prometheus.client.exporter.BasicAuthHttpConnectionFactory)2 PushGateway (io.prometheus.client.exporter.PushGateway)2 Gauge (io.prometheus.client.Gauge)1 DropwizardExports (io.prometheus.client.dropwizard.DropwizardExports)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 LinkedHashMap (java.util.LinkedHashMap)1