Search in sources :

Example 1 with MetricFamilySamples

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');
        }
    }
}
Also used : Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples)

Example 2 with MetricFamilySamples

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');
        }
    }
}
Also used : Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples)

Example 3 with MetricFamilySamples

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"));
}
Also used : TextFormat004Parser(org.cloudfoundry.promregator.fetcher.TextFormat004Parser) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 4 with MetricFamilySamples

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));
    }
}
Also used : MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples)

Example 5 with MetricFamilySamples

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));
}
Also used : Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) 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