Search in sources :

Example 6 with MetricFamilySamples

use of io.prometheus.client.Collector.MetricFamilySamples 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)

Example 7 with MetricFamilySamples

use of io.prometheus.client.Collector.MetricFamilySamples in project promregator by promregator.

the class AbstractMetricFamilySamplesEnricher method determineEnumerationOfMetricFamilySamples.

public HashMap<String, Collector.MetricFamilySamples> determineEnumerationOfMetricFamilySamples(HashMap<String, Collector.MetricFamilySamples> emfs) {
    if (emfs == null) {
        return null;
    }
    HashMap<String, Collector.MetricFamilySamples> newMap = new HashMap<String, Collector.MetricFamilySamples>();
    for (Entry<String, MetricFamilySamples> entry : emfs.entrySet()) {
        MetricFamilySamples mfs = entry.getValue();
        List<Collector.MetricFamilySamples.Sample> newSamples = new LinkedList<Collector.MetricFamilySamples.Sample>();
        for (Collector.MetricFamilySamples.Sample sample : mfs.samples) {
            Collector.MetricFamilySamples.Sample newSample = new Collector.MetricFamilySamples.Sample(sample.name, this.getEnrichedLabelNames(sample.labelNames), this.getEnrichedLabelValues(sample.labelValues), sample.value);
            newSamples.add(newSample);
        }
        Collector.MetricFamilySamples newEntry = new Collector.MetricFamilySamples(mfs.name, mfs.type, mfs.help, newSamples);
        newMap.put(entry.getKey(), newEntry);
    }
    return newMap;
}
Also used : HashMap(java.util.HashMap) Collector(io.prometheus.client.Collector) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) LinkedList(java.util.LinkedList) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples)

Example 8 with MetricFamilySamples

use of io.prometheus.client.Collector.MetricFamilySamples in project promregator by promregator.

the class AbstractMetricsEndpoint method waitForMetricsFetchers.

private MergableMetricFamilySamples waitForMetricsFetchers(LinkedList<Future<HashMap<String, MetricFamilySamples>>> futures) {
    long starttime = System.currentTimeMillis();
    MergableMetricFamilySamples mmfs = new MergableMetricFamilySamples();
    for (Future<HashMap<String, MetricFamilySamples>> future : futures) {
        long maxWaitTime = starttime + this.maxProcessingTime - System.currentTimeMillis();
        try {
            if (maxWaitTime < 0 && !future.isDone()) {
                // only process those, which are already completed
                continue;
            }
            HashMap<String, MetricFamilySamples> emfs = future.get(maxWaitTime, TimeUnit.MILLISECONDS);
            if (emfs != null) {
                mmfs.merge(emfs);
            }
        } catch (InterruptedException e) {
            continue;
        } catch (ExecutionException e) {
            log.warn("Exception thrown while fetching Metrics data from target", e);
            continue;
        } catch (TimeoutException e) {
            log.info("Timeout while fetching metrics data from target", e);
            // process the other's as well!
            continue;
        }
    }
    return mmfs;
}
Also used : MergableMetricFamilySamples(org.cloudfoundry.promregator.rewrite.MergableMetricFamilySamples) HashMap(java.util.HashMap) MergableMetricFamilySamples(org.cloudfoundry.promregator.rewrite.MergableMetricFamilySamples) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 9 with MetricFamilySamples

use of io.prometheus.client.Collector.MetricFamilySamples in project promregator by promregator.

the class AbstractMetricsEndpoint method handleRequest.

public String handleRequest() {
    Instant start = Instant.now();
    this.up.clear();
    List<Instance> instanceList = this.appInstanceScanner.determineInstancesFromTargets(this.promregatorConfiguration.getTargets());
    instanceList = this.filterInstanceList(instanceList);
    List<MetricsFetcher> callablesPrep = this.createMetricsFetchers(instanceList);
    LinkedList<Future<HashMap<String, MetricFamilySamples>>> futures = this.startMetricsFetchers(callablesPrep);
    MergableMetricFamilySamples mmfs = waitForMetricsFetchers(futures);
    Instant stop = Instant.now();
    Duration duration = Duration.between(start, stop);
    this.scrape_duration.set(duration.toMillis() / 1000.0);
    if (this.isIncludeGlobalMetrics()) {
        // also add our own (global) metrics
        mmfs.merge(this.gmfspr.determineEnumerationOfMetricFamilySamples(this.collectorRegistry));
    }
    // add also our own request-specific metrics
    mmfs.merge(this.gmfspr.determineEnumerationOfMetricFamilySamples(this.requestRegistry));
    return mmfs.toType004String();
}
Also used : MergableMetricFamilySamples(org.cloudfoundry.promregator.rewrite.MergableMetricFamilySamples) Instance(org.cloudfoundry.promregator.scanner.Instance) Instant(java.time.Instant) Future(java.util.concurrent.Future) Duration(java.time.Duration) MergableMetricFamilySamples(org.cloudfoundry.promregator.rewrite.MergableMetricFamilySamples) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) MetricsFetcher(org.cloudfoundry.promregator.fetcher.MetricsFetcher) CFMetricsFetcher(org.cloudfoundry.promregator.fetcher.CFMetricsFetcher)

Example 10 with MetricFamilySamples

use of io.prometheus.client.Collector.MetricFamilySamples in project promregator by promregator.

the class MergableMetricFamilySamplesTest method testStraightFowardEnumeration.

@Test
public void testStraightFowardEnumeration() {
    MergableMetricFamilySamples subject = new MergableMetricFamilySamples();
    List<Sample> samples = new LinkedList<>();
    MetricFamilySamples mfs = new MetricFamilySamples("dummy", Type.COUNTER, "somehelp", samples);
    List<MetricFamilySamples> list = new LinkedList<>();
    list.add(mfs);
    Enumeration<MetricFamilySamples> emfs = new Vector<MetricFamilySamples>(list).elements();
    subject.merge(emfs);
    Enumeration<MetricFamilySamples> returnedEMFS = subject.getEnumerationMetricFamilySamples();
    Assert.assertTrue(returnedEMFS.hasMoreElements());
    MetricFamilySamples element = returnedEMFS.nextElement();
    Assert.assertFalse(returnedEMFS.hasMoreElements());
    Assert.assertEquals(mfs, element);
    HashMap<String, MetricFamilySamples> returnedHMMFS = subject.getEnumerationMetricFamilySamplesInHashMap();
    Assert.assertEquals(1, returnedHMMFS.size());
    Assert.assertEquals(mfs, returnedHMMFS.get("dummy"));
}
Also used : Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

MetricFamilySamples (io.prometheus.client.Collector.MetricFamilySamples)31 Sample (io.prometheus.client.Collector.MetricFamilySamples.Sample)17 Test (org.junit.Test)15 HashMap (java.util.HashMap)9 LinkedList (java.util.LinkedList)8 MergableMetricFamilySamples (org.cloudfoundry.promregator.rewrite.MergableMetricFamilySamples)4 IOException (java.io.IOException)3 Collector (io.prometheus.client.Collector)2 Type (io.prometheus.client.Collector.Type)2 Collector.doubleToGoString (io.prometheus.client.Collector.doubleToGoString)2 StringWriter (java.io.StringWriter)2 Writer (java.io.Writer)2 Future (java.util.concurrent.Future)2 CFMetricsFetcher (org.cloudfoundry.promregator.fetcher.CFMetricsFetcher)2 MetricsFetcher (org.cloudfoundry.promregator.fetcher.MetricsFetcher)2 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)1 Timer (io.prometheus.client.Histogram.Timer)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ExecutionException (java.util.concurrent.ExecutionException)1