containerpope / nifi-prometheus-reporter

A reporting task in Nifi which is capable of sending monitoring statistics as prometheus metrics to a prometheus pushgateway.
Apache License 2.0
52 stars 32 forks source link

Added URL authentication #1

Closed celoibarros closed 6 years ago

celoibarros commented 6 years ago

Hi there,

This repo saved me a lot of time. Just made a small modification in order to add authentication on the url connection

static final PropertyDescriptor USERNAME = new PropertyDescriptor.Builder() .name("Username") .description("Username for URL authentication") .build(); static final PropertyDescriptor PASSWORD = new PropertyDescriptor.Builder() .name("Password") .description("Password for URL authentication") .sensitive(true) .build();

properties.add(USERNAME); properties.add(PASSWORD);

'final PushGatewayExt pushGateway = new PushGatewayExt(metricsCollectorUrl , context.getProperty(USERNAME) != null ? context.getProperty(USERNAME).toString() : null , context.getProperty(PASSWORD) != null ? context.getProperty(PASSWORD).toString() : null);'

created a new class

`package org.apache.nifi.reporting.prometheus.extend;

import io.prometheus.client.CollectorRegistry; import io.prometheus.client.exporter.PushGateway; import io.prometheus.client.exporter.common.TextFormat;

import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Iterator; import java.util.Map; import sun.misc.BASE64Encoder;

public class PushGatewayExt extends PushGateway { protected final String username; protected final String password;

public PushGatewayExt(String address, String username, String password) {
    super(address);
    this.username = username;
    this.password = password;
}

@Override
public void pushAdd(CollectorRegistry registry, String job) throws IOException {
    this.doAuthRequest(registry, job, (Map)null, "POST");
}

void doAuthRequest(CollectorRegistry registry, String job, Map<String, String> groupingKey, String method) throws IOException {
    String url = this.gatewayBaseURL + URLEncoder.encode(job, "UTF-8");
    Map.Entry entry;
    if (groupingKey != null) {
        for(Iterator var6 = groupingKey.entrySet().iterator(); var6.hasNext(); url = url + "/" + (String)entry.getKey() + "/" + URLEncoder.encode((String)entry.getValue(), "UTF-8")) {
            entry = (Map.Entry)var6.next();
        }
    }

    HttpURLConnection connection = (HttpURLConnection)(new URL(url)).openConnection();
    connection.setRequestProperty("Content-Type", "text/plain; version=0.0.4; charset=utf-8");
    if (!method.equals("DELETE")) {
        connection.setDoOutput(true);
    }

    if (this.username != null || this.password != null) {
        String userPass = this.username + ":" + this.password;
        String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userPass.getBytes());
        connection.setRequestProperty("Authorization", basicAuth);
    }

    connection.setRequestMethod(method);
    connection.setConnectTimeout(10000);
    connection.setReadTimeout(10000);
    connection.connect();

    try {
        if (!method.equals("DELETE")) {
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
            TextFormat.write004(writer, registry.metricFamilySamples());
            writer.flush();
            writer.close();
        }

        int response = connection.getResponseCode();
        if (response != 202) {
            throw new IOException("Response code from " + url + " was " + response);
        }
    } finally {
        connection.disconnect();
    }
}

} ` Hope it helps someone.

Best regards and thanks.

containerpope commented 6 years ago

Happy to hear, that it helped you. A lot to do recently but we will implement it into the ReportingTask. Thanks for the feedback :)

Daniel-Seifert commented 6 years ago

I've currently browsed through the repository 'prometheus/client_java' and found out they've added this feature in commit #398. I've already contacted brian-brazil and am currently waiting for his response regarding the next release containing this feature.

Daniel-Seifert commented 6 years ago

brian-brazil answered that he wants to release the requested features next week.

Daniel-Seifert commented 6 years ago

Feature is implemented now