Search in sources :

Example 26 with MetricFamilySamples

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

the class MergableMetricFamilySamplesTest method testStraightFowardHashMap.

@Test
public void testStraightFowardHashMap() {
    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);
    HashMap<String, MetricFamilySamples> hmmfs = new HashMap<>();
    hmmfs.put("dummy", mfs);
    subject.merge(hmmfs);
    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 : HashMap(java.util.HashMap) Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 27 with MetricFamilySamples

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

the class MetricFamilySamplesEnricherTest method testSimple.

@Test
public void testSimple() {
    AbstractMetricFamilySamplesEnricher subject = new CFMetricFamilySamplesEnricher("testOrgName", "testSpaceName", "testComponent", "testInstance:42");
    List<Sample> samples = new LinkedList<>();
    Sample s = new Sample("dummyname", Arrays.asList(new String[] { "labelName" }), Arrays.asList(new String[] { "labelValue" }), 1.0);
    samples.add(s);
    MetricFamilySamples mfs = new MetricFamilySamples("dummyname", Type.GAUGE, "dummyHelp", samples);
    HashMap<String, MetricFamilySamples> map = new HashMap<>();
    map.put("metricName", mfs);
    HashMap<String, MetricFamilySamples> result = subject.determineEnumerationOfMetricFamilySamples(map);
    Assert.assertEquals(1, result.size());
    MetricFamilySamples testMFS = result.get("metricName");
    Assert.assertNotNull(testMFS);
    Assert.assertEquals(1, testMFS.samples.size());
    Sample testSample = testMFS.samples.get(0);
    Assert.assertNotNull(testSample);
    List<String> labelNamesList = testSample.labelNames;
    String[] labelNames = labelNamesList.toArray(new String[0]);
    Assert.assertEquals("labelName", labelNames[0]);
    Assert.assertEquals("org_name", labelNames[1]);
    Assert.assertEquals("space_name", labelNames[2]);
    Assert.assertEquals("app_name", labelNames[3]);
    Assert.assertEquals("cf_instance_id", labelNames[4]);
    Assert.assertEquals("cf_instance_number", labelNames[5]);
    List<String> labelValuesList = testSample.labelValues;
    String[] labelValues = labelValuesList.toArray(new String[0]);
    Assert.assertEquals("labelValue", labelValues[0]);
    Assert.assertEquals("testOrgName", labelValues[1]);
    Assert.assertEquals("testSpaceName", labelValues[2]);
    Assert.assertEquals("testComponent", labelValues[3]);
    Assert.assertEquals("testInstance:42", labelValues[4]);
    Assert.assertEquals("42", labelValues[5]);
}
Also used : HashMap(java.util.HashMap) Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 28 with MetricFamilySamples

use of io.prometheus.client.Collector.MetricFamilySamples in project spring-boot by spring-projects.

the class PrometheusScrapeEndpoint method scrape.

@ReadOperation(producesFrom = TextOutputFormat.class)
public WebEndpointResponse<String> scrape(TextOutputFormat format, @Nullable Set<String> includedNames) {
    try {
        Writer writer = new StringWriter();
        Enumeration<MetricFamilySamples> samples = (includedNames != null) ? this.collectorRegistry.filteredMetricFamilySamples(includedNames) : this.collectorRegistry.metricFamilySamples();
        format.write(writer, samples);
        return new WebEndpointResponse<>(writer.toString(), format);
    } catch (IOException ex) {
        // This actually never happens since StringWriter doesn't throw an IOException
        throw new IllegalStateException("Writing metrics failed", ex);
    }
}
Also used : StringWriter(java.io.StringWriter) WebEndpointResponse(org.springframework.boot.actuate.endpoint.web.WebEndpointResponse) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) IOException(java.io.IOException) StringWriter(java.io.StringWriter) Writer(java.io.Writer) ReadOperation(org.springframework.boot.actuate.endpoint.annotation.ReadOperation)

Example 29 with MetricFamilySamples

use of io.prometheus.client.Collector.MetricFamilySamples in project instrumentation-java by census-instrumentation.

the class PrometheusExportUtils method createMetricFamilySamples.

// Converts a Metric to a Prometheus MetricFamilySamples.
static MetricFamilySamples createMetricFamilySamples(Metric metric, String namespace) {
    MetricDescriptor metricDescriptor = metric.getMetricDescriptor();
    String name = getNamespacedName(metricDescriptor.getName(), namespace);
    Type type = getType(metricDescriptor.getType());
    List<String> labelNames = convertToLabelNames(metricDescriptor.getLabelKeys());
    List<Sample> samples = Lists.newArrayList();
    for (io.opencensus.metrics.export.TimeSeries timeSeries : metric.getTimeSeriesList()) {
        for (io.opencensus.metrics.export.Point point : timeSeries.getPoints()) {
            samples.addAll(getSamples(name, labelNames, timeSeries.getLabelValues(), point.getValue()));
        }
    }
    return new MetricFamilySamples(name, type, metricDescriptor.getDescription(), samples);
}
Also used : MetricDescriptor(io.opencensus.metrics.export.MetricDescriptor) Type(io.prometheus.client.Collector.Type) Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) Collector.doubleToGoString(io.prometheus.client.Collector.doubleToGoString) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples)

Example 30 with MetricFamilySamples

use of io.prometheus.client.Collector.MetricFamilySamples in project instrumentation-java by census-instrumentation.

the class PrometheusExportUtils method createDescribableMetricFamilySamples.

// Converts a MetricDescriptor to a Prometheus MetricFamilySamples.
// Used only for Prometheus metric registry, should not contain any actual samples.
static MetricFamilySamples createDescribableMetricFamilySamples(MetricDescriptor metricDescriptor, String namespace) {
    String name = getNamespacedName(metricDescriptor.getName(), namespace);
    Type type = getType(metricDescriptor.getType());
    List<String> labelNames = convertToLabelNames(metricDescriptor.getLabelKeys());
    if (containsDisallowedLeLabelForHistogram(labelNames, type)) {
        throw new IllegalStateException("Prometheus Histogram cannot have a label named 'le', " + "because it is a reserved label for bucket boundaries. " + "Please remove this key from your view.");
    }
    if (containsDisallowedQuantileLabelForSummary(labelNames, type)) {
        throw new IllegalStateException("Prometheus Summary cannot have a label named 'quantile', " + "because it is a reserved label. Please remove this key from your view.");
    }
    return new MetricFamilySamples(name, type, metricDescriptor.getDescription(), Collections.<Sample>emptyList());
}
Also used : Type(io.prometheus.client.Collector.Type) Collector.doubleToGoString(io.prometheus.client.Collector.doubleToGoString) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples)

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