Java Code Examples for io.prometheus.client.CollectorRegistry#metricFamilySamples()

The following examples show how to use io.prometheus.client.CollectorRegistry#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: RegistryHelper.java    From java-grpc-prometheus with Apache License 2.0 6 votes vote down vote up
public static int countSamples(
    String metricName, String sampleName, CollectorRegistry collectorRegistry) {
  Enumeration<Collector.MetricFamilySamples> samples = collectorRegistry.metricFamilySamples();
  while (samples.hasMoreElements()) {
    Collector.MetricFamilySamples sample = samples.nextElement();
    if (sample.name.equals(metricName)) {
      int result = 0;
      for (Collector.MetricFamilySamples.Sample s : sample.samples) {
        if (s.name.equals(sampleName)) {
          ++result;
        }
      }
      return result;
    }
  }
  throw new IllegalArgumentException("Could not find sample family with name: " + metricName);
}
 
Example 2
Source File: PrometheusTextFormatUtil.java    From pulsar with Apache License 2.0 6 votes vote down vote up
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');
        }
    }
}
 
Example 3
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 4
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
void assertMetricTimestampEquals(CollectorRegistry registry, String name, Long expectedTimestamp) {
  Enumeration<Collector.MetricFamilySamples> metricFamilySamplesEnumeration = registry.metricFamilySamples();
  Set<String> metricNames = new HashSet<String>();
  while(metricFamilySamplesEnumeration.hasMoreElements()) {
    Collector.MetricFamilySamples samples = metricFamilySamplesEnumeration.nextElement();
    for(Collector.MetricFamilySamples.Sample s: samples.samples) {
      metricNames.add(s.name);
      if(s.name.equals(name)) {
        assertEquals(expectedTimestamp, (Long)s.timestampMs);
        return;
      }
    }
  }
  fail(String.format("Metric %s not found in registry. Metrics found: %s", name, metricNames));
}
 
Example 5
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
void assertMetricTimestampEquals(CollectorRegistry registry, String name, Long expectedTimestamp) {
  Enumeration<Collector.MetricFamilySamples> metricFamilySamplesEnumeration = registry.metricFamilySamples();
  Set<String> metricNames = new HashSet<String>();
  while(metricFamilySamplesEnumeration.hasMoreElements()) {
    Collector.MetricFamilySamples samples = metricFamilySamplesEnumeration.nextElement();
    for(Collector.MetricFamilySamples.Sample s: samples.samples) {
      metricNames.add(s.name);
      if(s.name.equals(name)) {
        assertEquals(expectedTimestamp, (Long)s.timestampMs);
        return;
      }
    }
  }
  fail(String.format("Metric %s not found in registry. Metrics found: %s", name, metricNames));
}
 
Example 6
Source File: GenericMetricFamilySamplesPrefixRewriter.java    From promregator with Apache License 2.0 4 votes vote down vote up
public HashMap<String, Collector.MetricFamilySamples> determineEnumerationOfMetricFamilySamples(CollectorRegistry cr) {
	Enumeration<MetricFamilySamples> rawMFS = cr.metricFamilySamples();
	return this.determineEnumerationOfMetricFamilySamples(MFSUtils.convertToEMFSToHashMap(rawMFS));
}
 
Example 7
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());
  }
}