Search in sources :

Example 1 with Timer

use of io.prometheus.client.Histogram.Timer in project promregator by promregator.

the class CFMetricsFetcher method call.

@Override
public HashMap<String, MetricFamilySamples> call() throws Exception {
    HttpGet httpget = new HttpGet(this.endpointUrl);
    if (this.config != null) {
        httpget.setConfig(this.config);
    }
    // see also https://docs.cloudfoundry.org/concepts/http-routing.html
    httpget.setHeader(HTTP_HEADER_CF_APP_INSTANCE, this.instanceId);
    if (ae != null) {
        ae.enrichWithAuthentication(httpget);
    }
    CloseableHttpResponse response = null;
    boolean available = false;
    try {
        Timer timer = null;
        if (this.mfm.getLatencyRequest() != null) {
            timer = this.mfm.getLatencyRequest().startTimer();
        }
        response = httpclient.execute(httpget);
        if (timer != null) {
            timer.observeDuration();
        }
        if (response.getStatusLine().getStatusCode() != 200) {
            log.warn(String.format("Target server at '%s' and instance '%s' responded with a non-200 status code: %d", this.endpointUrl, this.instanceId, response.getStatusLine().getStatusCode()));
            return null;
        }
        String result = EntityUtils.toString(response.getEntity());
        TextFormat004Parser parser = new TextFormat004Parser(result);
        HashMap<String, MetricFamilySamples> emfs = parser.parse();
        // we got a proper response
        available = true;
        emfs = this.mfse.determineEnumerationOfMetricFamilySamples(emfs);
        return emfs;
    } catch (ClientProtocolException e) {
        log.warn("Client communication error while fetching metrics from target server", e);
        return null;
    } catch (IOException e) {
        log.warn("IO Exception while fetching metrics from target server", e);
        return null;
    } finally {
        if (response != null) {
            try {
                response.close();
            } catch (IOException e) {
                log.info("Unable to properly close Metrics fetch HTTP connection", e);
            // bad luck!
            }
        }
        if (this.mfm.getUp() != null) {
            if (available) {
                this.mfm.getUp().set(1.0);
            } else {
                if (this.mfm.getFailedRequests() != null)
                    this.mfm.getFailedRequests().inc();
                this.mfm.getUp().set(0.0);
            }
        }
    }
}
Also used : Timer(io.prometheus.client.Histogram.Timer) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

MetricFamilySamples (io.prometheus.client.Collector.MetricFamilySamples)1 Timer (io.prometheus.client.Histogram.Timer)1 IOException (java.io.IOException)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1