use of io.prometheus.client.Collector.MetricFamilySamples in project bookkeeper by apache.
the class PrometheusTextFormatUtil method writeMetricsCollectedByPrometheusClient.
static void writeMetricsCollectedByPrometheusClient(Writer w, CollectorRegistry registry) throws IOException {
Enumeration<MetricFamilySamples> metricFamilySamples = registry.metricFamilySamples();
while (metricFamilySamples.hasMoreElements()) {
MetricFamilySamples metricFamily = metricFamilySamples.nextElement();
for (int i = 0; i < metricFamily.samples.size(); i++) {
Sample sample = metricFamily.samples.get(i);
w.write(sample.name);
w.write('{');
for (int j = 0; j < sample.labelNames.size(); j++) {
if (j != 0) {
w.write(", ");
}
w.write(sample.labelNames.get(j));
w.write("=\"");
w.write(sample.labelValues.get(j));
w.write('"');
}
w.write("} ");
w.write(Collector.doubleToGoString(sample.value));
w.write('\n');
}
}
}
use of io.prometheus.client.Collector.MetricFamilySamples in project incubator-pulsar by apache.
the class PrometheusMetricsGenerator method generateSystemMetrics.
private static void generateSystemMetrics(SimpleTextOutputStream stream, String cluster) {
Enumeration<MetricFamilySamples> metricFamilySamples = CollectorRegistry.defaultRegistry.metricFamilySamples();
while (metricFamilySamples.hasMoreElements()) {
MetricFamilySamples metricFamily = metricFamilySamples.nextElement();
for (int i = 0; i < metricFamily.samples.size(); i++) {
Sample sample = metricFamily.samples.get(i);
stream.write(sample.name);
stream.write("{cluster=\"").write(cluster).write('"');
for (int j = 0; j < sample.labelNames.size(); j++) {
stream.write(", ");
stream.write(sample.labelNames.get(j));
stream.write("=\"");
stream.write(sample.labelValues.get(j));
stream.write('"');
}
stream.write("} ");
stream.write(Collector.doubleToGoString(sample.value));
stream.write('\n');
}
}
}
use of io.prometheus.client.Collector.MetricFamilySamples in project promregator by promregator.
the class MetricsEndpointTest method testGetMetrics.
@Test
public void testGetMetrics() {
Assert.assertNotNull(subject);
String response = subject.getMetrics();
Assert.assertNotNull(response);
Assert.assertNotEquals("", response);
TextFormat004Parser parser = new TextFormat004Parser(response);
HashMap<String, MetricFamilySamples> mapMFS = parser.parse();
Assert.assertNotNull(mapMFS.get("metric_unittestapp"));
Assert.assertNotNull(mapMFS.get("metric_unittestapp2"));
}
use of io.prometheus.client.Collector.MetricFamilySamples in project promregator by promregator.
the class TextFormat004ParserTest method compareEMFS.
public static void compareEMFS(Enumeration<MetricFamilySamples> expected, Enumeration<MetricFamilySamples> actual) {
HashMap<String, MetricFamilySamples> expectedMap = MFSUtils.convertToEMFSToHashMap(expected);
HashMap<String, MetricFamilySamples> actualMap = MFSUtils.convertToEMFSToHashMap(actual);
Assert.assertTrue(EqualsBuilder.reflectionEquals(actualMap.keySet(), expectedMap.keySet(), false));
for (String metricName : expectedMap.keySet()) {
MetricFamilySamples actualMFS = actualMap.get(metricName);
MetricFamilySamples expectedMFS = expectedMap.get(metricName);
Assert.assertTrue(EqualsBuilder.reflectionEquals(actualMFS, expectedMFS));
}
}
use of io.prometheus.client.Collector.MetricFamilySamples in project promregator by promregator.
the class TextFormat004ParserTest method testSimpleNaN.
@Test
public void testSimpleNaN() {
String textToParse = "# Minimalistic line:\n" + "\n" + "metric_without_labels NaN 123456789012345600\n";
TextFormat004Parser subject = new TextFormat004Parser(textToParse);
HashMap<String, Collector.MetricFamilySamples> resultMap = subject.parse();
Enumeration<Collector.MetricFamilySamples> result = Collections.enumeration(resultMap.values());
// compareEMFS does not properly work with NaN values
// Thus, we have to check this explicitly here
MetricFamilySamples mfs = result.nextElement();
Assert.assertFalse(result.hasMoreElements());
Assert.assertEquals("metric_without_labels", mfs.name);
Assert.assertEquals(1, mfs.samples.size());
Sample actualSample = mfs.samples.get(0);
Assert.assertEquals("metric_without_labels", actualSample.name);
Assert.assertTrue(Double.isNaN(actualSample.value));
}
Aggregations