Java Code Examples for io.prometheus.client.Collector#MetricFamilySamples

The following examples show how to use io.prometheus.client.Collector#MetricFamilySamples . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimple() {
	String textToParse = "# Minimalistic line:\n" + 
			"metric_without_timestamp_and_labels 12.47\n";
	
	Parser subject = new Parser(textToParse);
	HashMap<String, Collector.MetricFamilySamples> resultMap = subject.parse();
	Enumeration<Collector.MetricFamilySamples> result = Collections.enumeration(resultMap.values());
	
	// creating expected result
	LinkedList<Collector.MetricFamilySamples> expectedList = new LinkedList<>();

	List<Sample> samples = new LinkedList<>();
	Sample sample = new Sample("metric_without_timestamp_and_labels", new LinkedList<String>(), new LinkedList<String>(), 12.47);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_timestamp_and_labels", Type.UNTYPED, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example 2
Source File: RateCount.java    From maestro-java with Apache License 2.0 6 votes vote down vote up
public List<Collector.MetricFamilySamples> collect() {
    List<Collector.MetricFamilySamples> mfs = new ArrayList<>();

    GaugeMetricFamily labeledGauge = new GaugeMetricFamily("maestro_rate",
            "Rate", Arrays.asList("peer", "type"));

    logger.trace("Number of values to process: {}", records.values().size());
    for (StatsResponse stats : records.values()) {

        logger.trace("Adding record for {}/{}", stats.getPeerInfo().prettyName(), stats.getId());
        labeledGauge.addMetric(Arrays.asList(stats.getPeerInfo().peerName(), stats.getPeerInfo().peerHost()), stats.getRate());
    }

    mfs.add(labeledGauge);
    records.clear();
    return mfs;
}
 
Example 3
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleNaN() {
	String textToParse = "# Minimalistic line:\n" + 
			"\n"+
			"metric_without_labels NaN 123456789012345600\n";
	
	Parser subject = new Parser(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));
}
 
Example 4
Source File: ParserTest.java    From promregator with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleWithEFormat() {
	String textToParse = "# Minimalistic line:\n" + 
			"\n"+
			"metric_without_labels 1.7560473e+07\n";
	
	Parser subject = new Parser(textToParse);
	HashMap<String, Collector.MetricFamilySamples> resultMap = subject.parse();
	Enumeration<Collector.MetricFamilySamples> result = Collections.enumeration(resultMap.values());

	// creating expected result
	LinkedList<Collector.MetricFamilySamples> expectedList = new LinkedList<>();

	List<Sample> samples = new LinkedList<>();
	Sample sample = new Sample("metric_without_labels", new LinkedList<String>(), new LinkedList<String>(), 1.7560473e+07);
	samples.add(sample);
	
	Collector.MetricFamilySamples expectedMFS = new Collector.MetricFamilySamples("metric_without_labels", Type.UNTYPED, "", samples);
	expectedList.add(expectedMFS);
	
	Enumeration<Collector.MetricFamilySamples> expected = Collections.enumeration(expectedList);
	
	// compare
	compareEMFS(expected, result);
}
 
Example 5
Source File: PrometheusMetricsTest.java    From garmadon with Apache License 2.0 5 votes vote down vote up
private void checkLabelNotExist(String label, String value) {
    Enumeration<Collector.MetricFamilySamples> samples = CollectorRegistry.defaultRegistry.metricFamilySamples();
    while (samples.hasMoreElements()) {
        Collector.MetricFamilySamples thoseSamples = samples.nextElement();
        thoseSamples.samples.forEach(sample -> {
            int idx = sample.labelNames.indexOf(label);
            if (idx >= 0) {
                assertThat(sample.labelValues.get(idx), not(value));
            }
        });
    }
}
 
Example 6
Source File: MetricSamplesTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void asList() {
  MetricSamples samples = new MetricSamples(Maps.newHashMap(ImmutableMap.<String, Collector.MetricFamilySamples>builder()
      .put("test1", samples("test1", Collector.Type.GAUGE, sample("test1", 1.0), sample("test1", 2.0)))
      .put("test2", samples("test2", Collector.Type.GAUGE, sample("test2", 1.0)))
      .build()));

  List<Collector.MetricFamilySamples> output = samples.asList();

  assertEquals(2, output.size());

  validateMetricSamples(output, "test1", Arrays.asList(1.0, 2.0));
  validateMetricSamples(output, "test2", Collections.singletonList(1.0));
}
 
Example 7
Source File: Parser.java    From promregator with Apache License 2.0 5 votes vote down vote up
public HashMap<String, Collector.MetricFamilySamples> parse() {
	this.reset();
	
	StringTokenizer lines = new StringTokenizer(this.textFormat004data, "\n");
	
	while(lines.hasMoreTokens()) {
		String line = lines.nextToken();
		
		// warning! Order of IF tests matter!
		if (this.isEmptyLine(line)) {
			continue;
		} else if (this.isHelpLine(line)) {
			this.parseHelpLine(line);
			continue;
		} else if (this.isTypeLine(line)) {
			this.parseTypeLine(line);
			continue;
		} else if (this.isCommentLine(line)) {
			continue;
		}
		
		// we need to assume that this is a metric line
		this.parseMetric(line);
	}
	
	return this.mapMFS;
}
 
Example 8
Source File: RegistryHelper.java    From java-grpc-prometheus with Apache License 2.0 5 votes vote down vote up
public static Optional<Collector.MetricFamilySamples> findRecordedMetric(
    String name, CollectorRegistry collectorRegistry) {
  Enumeration<Collector.MetricFamilySamples> samples = collectorRegistry.metricFamilySamples();
  while (samples.hasMoreElements()) {
    Collector.MetricFamilySamples sample = samples.nextElement();
    if (sample.name.equals(name)) {
      return Optional.of(sample);
    }
  }
  return Optional.empty();
}
 
Example 9
Source File: MetricSamplesTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void validateMetricSamples(
    List<Collector.MetricFamilySamples> allMetrics,
    String metricName,
    List<Double> expectedValues) {

  Collector.MetricFamilySamples test1 = allMetrics.stream()
      .filter(s -> s.name.equals(metricName))
      .findFirst()
      .orElseThrow(() -> new RuntimeException(String.format(Locale.ROOT, "Unable to find item %s", metricName)));

  assertTrue(Iterables.elementsEqual(expectedValues, test1.samples.stream().map(s -> s.value).collect(Collectors.toList())));
}
 
Example 10
Source File: RMQMetricsServiceImpl.java    From rocketmq-exporter with Apache License 2.0 5 votes vote down vote up
public void writeEscapedHelp(Writer writer, Enumeration<Collector.MetricFamilySamples> mfs) throws IOException {
    while (mfs.hasMoreElements()) {
        Collector.MetricFamilySamples metricFamilySamples = mfs.nextElement();
        for (Iterator var3 = metricFamilySamples.samples.iterator(); var3.hasNext(); writer.write(10)) {
            Collector.MetricFamilySamples.Sample sample = (Collector.MetricFamilySamples.Sample) var3.next();
            writer.write(sample.name);
            if (sample.labelNames.size() > 0) {
                writer.write(123);

                for (int i = 0; i < sample.labelNames.size(); ++i) {
                    writer.write((String) sample.labelNames.get(i));
                    writer.write("=\"");
                    writeEscapedLabelValue(writer, (String) sample.labelValues.get(i));
                    writer.write("\",");
                }

                writer.write(125);
            }

            writer.write(32);
            writer.write(Collector.doubleToGoString(sample.value));
            if (sample.timestampMs != null) {
                writer.write(32);
                writer.write(sample.timestampMs.toString());
            }
        }
    }

}
 
Example 11
Source File: TestAlibabaMetricsExports.java    From metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testExportFastCompass() {
    AlibabaMetricsExports exports = new AlibabaMetricsExports(clock);
    FastCompass fc = new FastCompassImpl(1, 5, clock, 5);
    MetricManager.getIMetricManager().register("test",
            MetricName.build("prom.test.fastcompass").level(MetricLevel.CRITICAL), fc);

    int successSum = 0;
    int failureSum = 0;

    for (int i = 0; i < 10; i++) {
        int value = (int)(1000 * Math.random());
        if (i % 2 == 0) {
            fc.record(value, "success");
            successSum += value;
        } else {
            fc.record(value, "failure");
            failureSum += value;
        }
    }
    clock.addSeconds(1);

    List<Collector.MetricFamilySamples> samples = exports.collect();
    Assert.assertEquals(1, samples.size());
    Assert.assertEquals("prom_test_fastcompass_bucket_count", samples.get(0).name);
    Assert.assertEquals("should contain success/failure count and sum", 4, samples.get(0).samples.size());
    Assert.assertEquals("prom_test_fastcompass_bucket_count", samples.get(0).samples.get(0).name);
    Assert.assertEquals("success count", 5, samples.get(0).samples.get(0).value, 0.0001d);
    Assert.assertEquals("failure count", 5, samples.get(0).samples.get(1).value, 0.0001d);
    Assert.assertEquals("success sum", successSum, samples.get(0).samples.get(2).value, 0.0001d);
    Assert.assertEquals("failure sum", failureSum, samples.get(0).samples.get(3).value, 0.0001d);
}
 
Example 12
Source File: TextFormat.java    From client_java with Apache License 2.0 5 votes vote down vote up
/**
 * Write out the text version 0.0.4 of the given MetricFamilySamples.
 */
public static void write004(Writer writer, Enumeration<Collector.MetricFamilySamples> mfs) throws IOException {
  /* See http://prometheus.io/docs/instrumenting/exposition_formats/
   * for the output format specification. */
  while(mfs.hasMoreElements()) {
    Collector.MetricFamilySamples metricFamilySamples = mfs.nextElement();
    writer.write("# HELP ");
    writer.write(metricFamilySamples.name);
    writer.write(' ');
    writeEscapedHelp(writer, metricFamilySamples.help);
    writer.write('\n');

    writer.write("# TYPE ");
    writer.write(metricFamilySamples.name);
    writer.write(' ');
    writer.write(typeString(metricFamilySamples.type));
    writer.write('\n');

    for (Collector.MetricFamilySamples.Sample sample: metricFamilySamples.samples) {
      writer.write(sample.name);
      if (sample.labelNames.size() > 0) {
        writer.write('{');
        for (int i = 0; i < sample.labelNames.size(); ++i) {
          writer.write(sample.labelNames.get(i));
          writer.write("=\"");
          writeEscapedLabelValue(writer, sample.labelValues.get(i));
          writer.write("\",");
        }
        writer.write('}');
      }
      writer.write(' ');
      writer.write(Collector.doubleToGoString(sample.value));
      if (sample.timestampMs != null){
        writer.write(' ');
        writer.write(sample.timestampMs.toString());
      }
      writer.write('\n');
    }
  }
}
 
Example 13
Source File: MetricSamples.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void addSampleIfMetricExists(String metricName, Collector.MetricFamilySamples.Sample sample) {
  Collector.MetricFamilySamples sampleFamily = samplesByMetricName.get(metricName);

  if (sampleFamily == null) {
    return;
  }

  if (!sampleFamily.samples.contains(sample)) {
    sampleFamily.samples.add(sample);
  }
}
 
Example 14
Source File: TestAlibabaMetricsExports.java    From metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testExportHistogram() {
    AlibabaMetricsExports exports = new AlibabaMetricsExports(clock);
    Histogram his = new HistogramImpl(ReservoirType.UNIFORM, 1, 5, clock);
    MetricManager.getIMetricManager().register("test",
            MetricName.build("prom.test.histogram").level(MetricLevel.CRITICAL), his);

    int min = Integer.MAX_VALUE;
    int max = 0;
    for (int i = 0; i < 10; i++) {
        int value = (int)(1000 * Math.random());
        his.update(value);
        if (value < min) {
            min = value;
        }
        if (value > max) {
            max = value;
        }
    }

    List<Collector.MetricFamilySamples> samples = exports.collect();
    Assert.assertEquals(6, samples.size());
    Assert.assertEquals("prom_test_histogram_summary", samples.get(0).name);
    Assert.assertEquals("should contain p50/p75/p95/p99 percentiles", 4, samples.get(0).samples.size());
    Assert.assertEquals("prom_test_histogram_min", samples.get(1).name);
    Assert.assertEquals(min, samples.get(1).samples.get(0).value, 0.0001d);
    Assert.assertEquals("prom_test_histogram_max", samples.get(2).name);
    Assert.assertEquals(max, samples.get(2).samples.get(0).value, 0.0001d);
    Assert.assertEquals("prom_test_histogram_mean", samples.get(3).name);
    Assert.assertEquals("prom_test_histogram_stddev", samples.get(4).name);
    Assert.assertEquals("prom_test_histogram_count", samples.get(5).name);
}
 
Example 15
Source File: MetricsFilterTest.java    From client_java with Apache License 2.0 4 votes vote down vote up
@Test
public void testBucketsAndName() throws Exception {
    HttpServletRequest req = mock(HttpServletRequest.class);
    final String path = "/foo/bar/baz/bang";
    when(req.getRequestURI()).thenReturn(path);
    when(req.getMethod()).thenReturn(HttpMethods.POST);

    FilterChain c = mock(FilterChain.class);
    doAnswer(new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
            Thread.sleep(100);
            return null;
        }
    }).when(c).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));

    final String buckets = "0.01,0.05,0.1,0.15,0.25";
    FilterConfig cfg = mock(FilterConfig.class);
    when(cfg.getInitParameter(MetricsFilter.BUCKET_CONFIG_PARAM)).thenReturn(buckets);
    when(cfg.getInitParameter(MetricsFilter.METRIC_NAME_PARAM)).thenReturn("foo");

    HttpServletResponse res = mock(HttpServletResponse.class);

    f.init(cfg);

    f.doFilter(req, res, c);

    final Double sum = CollectorRegistry.defaultRegistry.getSampleValue("foo_sum", new String[]{"path", "method"}, new String[]{"/foo", HttpMethods.POST});
    assertEquals(0.1, sum, 0.01);

    final Double le05 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", HttpMethods.POST, "0.05"});
    assertNotNull(le05);
    assertEquals(0, le05, 0.01);
    final Double le15 = CollectorRegistry.defaultRegistry.getSampleValue("foo_bucket", new String[]{"path", "method", "le"}, new String[]{"/foo", HttpMethods.POST, "0.15"});
    assertNotNull(le15);
    assertEquals(1, le15, 0.01);


    final Enumeration<Collector.MetricFamilySamples> samples = CollectorRegistry.defaultRegistry.metricFamilySamples();
    Collector.MetricFamilySamples sample = null;
    while(samples.hasMoreElements()) {
        sample = samples.nextElement();
        if (sample.name.equals("foo")) {
            break;
        }
    }

    assertNotNull(sample);

    int count = 0;
    for (Collector.MetricFamilySamples.Sample s : sample.samples) {
        if (s.name.equals("foo_bucket")) {
            count++;
        }
    }
    // +1 because of the final le=+infinity bucket
    assertEquals(buckets.split(",").length+1, count);
}
 
Example 16
Source File: TomcatMetricsServlet.java    From tomcat_exporter with Apache License 2.0 4 votes vote down vote up
private boolean initialized() {
    Enumeration<Collector.MetricFamilySamples> samples = CollectorRegistry.defaultRegistry.filteredMetricFamilySamples(new HashSet<String>(Arrays.asList("tomcat_info")));
    return samples.hasMoreElements();
}
 
Example 17
Source File: RegistryHelper.java    From java-grpc-prometheus with Apache License 2.0 4 votes vote down vote up
public static void printRegistry(CollectorRegistry collectorRegistry) {
  Enumeration<Collector.MetricFamilySamples> samples = collectorRegistry.metricFamilySamples();
  while (samples.hasMoreElements()) {
    printSamples(samples.nextElement());
  }
}
 
Example 18
Source File: SolrStandaloneScraperTest.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
@Test
public void metricsForHost() throws Exception {
  Map<String, MetricSamples> metricsByHost = solrScraper.metricsForAllHosts(configuration.getMetricsConfiguration().get(0));

  assertEquals(1, metricsByHost.size());

  List<Collector.MetricFamilySamples> replicaSamples = metricsByHost.get(restTestHarness.getAdminURL()).asList();

  assertEquals(1, replicaSamples.size());

  assertEquals(1, replicaSamples.size());
  assertEquals("solr_metrics_jvm_buffers", replicaSamples.get(0).name);
}
 
Example 19
Source File: SchedulerMetricsCollector.java    From lucene-solr with Apache License 2.0 votes vote down vote up
void metricsUpdated(List<Collector.MetricFamilySamples> samples); 
Example 20
Source File: ScheduledMetricEvaluator.java    From jira-prometheus-exporter with BSD 2-Clause "Simplified" License votes vote down vote up
List<Collector.MetricFamilySamples> collect();