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