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);
}
}
}
}
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;
}
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;
}
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();
}
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"));
}
Aggregations