Search in sources :

Example 11 with Sample

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

the class TextFormat004Parser method parseMetric.

private void parseMetric(String line) {
    Matcher mMetricName = PATTERN_TOKEN_WITH_SPACE_SEPARATOR.matcher(line);
    if (!mMetricName.find()) {
        log.warn("Detected metric line without proper metric name: " + line);
        return;
    }
    String metricName = mMetricName.group(1);
    String rest = line.substring(mMetricName.end());
    // skip spaces if there...
    Matcher mSkipSpaces = PATTERN_SKIP_SPACES.matcher(rest);
    if (mSkipSpaces.find()) {
        rest = rest.substring(mSkipSpaces.end());
    }
    // check if the metric has an optional block of labels
    Matcher mLabelBlock = PATTERN_PARSE_LABELBLOCK.matcher(rest);
    Labels labels = null;
    if (mLabelBlock.find()) {
        labels = this.parseLabelBlock(mLabelBlock.group(1));
        rest = rest.substring(mLabelBlock.end());
        mSkipSpaces = PATTERN_SKIP_SPACES.matcher(rest);
        if (mSkipSpaces.find()) {
            rest = rest.substring(mSkipSpaces.end());
        }
    }
    double value = 0.0f;
    String valueString = null;
    Matcher mValueText = PATTERN_PARSE_VALUETEXT.matcher(rest);
    int end = 0;
    if (mValueText.find()) {
        valueString = mValueText.group(1);
        // NB: an exception cannot be thrown here (and the exception in fact is an Error)
        value = this.parseGoDouble(valueString);
        end = mValueText.end();
    } else {
        Matcher mValue = PATTERN_PARSE_VALUE.matcher(rest);
        if (!mValue.find()) {
            log.warn("Unable to parse value in metric line: " + line);
            return;
        }
        valueString = mValue.group(1);
        try {
            value = this.parseGoDouble(valueString);
        } catch (NumberFormatException nfe) {
            log.warn("Unable to parse value in metrics line properly: " + line, nfe);
            return;
        }
        end = mValue.end();
    }
    rest = rest.substring(end);
    /*
		 * currently not supported in java simpleclient!
		 */
    // optional timestamp
    /*
		double timestamp = 0.0;
		if (!"".equals(rest)) {
			try {
				timestamp = this.parseGoDouble(rest);
			} catch (NumberFormatException nfe) {
				log.warn("Unable to parse timestamp in metrics line properly: "+line, nfe);
				return;
			}
		}*/
    Collector.Type type = determineType(metricName);
    List<String> labelNames = labels == null ? new LinkedList<String>() : labels.getNames();
    List<String> labelValues = labels == null ? new LinkedList<String>() : labels.getValues();
    Sample sample = new Sample(metricName, labelNames, labelValues, value);
    if (type.equals(Collector.Type.COUNTER) || type.equals(Collector.Type.GAUGE) || type.equals(Collector.Type.UNTYPED)) {
        List<Sample> samples = new LinkedList<Sample>();
        samples.add(sample);
        String docString = this.mapHelps.get(metricName);
        Collector.MetricFamilySamples mfs = new Collector.MetricFamilySamples(metricName, type, docString, samples);
        this.mapMFS.put(metricName, mfs);
    } else if (type.equals(Collector.Type.HISTOGRAM)) {
        String baseMetricName = determineBaseMetricName(metricName);
        // is this already in our Map?
        Collector.MetricFamilySamples mfs = this.mapMFS.get(baseMetricName);
        if (mfs == null) {
            // no, we have to create a new one
            String docString = this.mapHelps.get(baseMetricName);
            mfs = new Collector.MetricFamilySamples(baseMetricName, type, docString, new LinkedList<>());
            this.mapMFS.put(baseMetricName, mfs);
        }
        mfs.samples.add(sample);
    } else if (type.equals(Collector.Type.SUMMARY)) {
        String baseMetricName = determineBaseMetricName(metricName);
        // is this already in our Map?
        Collector.MetricFamilySamples mfs = this.mapMFS.get(baseMetricName);
        if (mfs == null) {
            // no, we have to create a new one
            String docString = this.mapHelps.get(baseMetricName);
            mfs = new Collector.MetricFamilySamples(baseMetricName, type, docString, new LinkedList<>());
            this.mapMFS.put(baseMetricName, mfs);
        }
        mfs.samples.add(sample);
    } else {
        log.warn(String.format("Unknown type %s; unclear how to handle this; skipping", type.toString()));
        return;
    }
}
Also used : Matcher(java.util.regex.Matcher) Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) LinkedList(java.util.LinkedList) Type(io.prometheus.client.Collector.Type) Collector(io.prometheus.client.Collector)

Example 12 with Sample

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

the class MergableMetricFamilySamplesTest method testStraightFowardEnumeration.

@Test
public void testStraightFowardEnumeration() {
    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);
    Enumeration<MetricFamilySamples> emfs = new Vector<MetricFamilySamples>(list).elements();
    subject.merge(emfs);
    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 : Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 13 with Sample

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

the class PrometheusStatsCollectorTest method testDescribe_WithNamespace.

@Test
public void testDescribe_WithNamespace() {
    String namespace = "myorg";
    PrometheusStatsCollector collector = new PrometheusStatsCollector(mockMetricProducerManager, namespace);
    assertThat(collector.describe()).containsExactly(new MetricFamilySamples(namespace + '_' + METRIC_NAME, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
}
Also used : Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) Test(org.junit.Test)

Example 14 with Sample

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

the class PrometheusStatsCollectorTest method testDescribe.

@Test
public void testDescribe() {
    PrometheusStatsCollector collector = new PrometheusStatsCollector(mockMetricProducerManager, "");
    assertThat(collector.describe()).containsExactly(new MetricFamilySamples(METRIC_NAME, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
}
Also used : Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) Test(org.junit.Test)

Example 15 with Sample

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

the class PrometheusExportUtilsTest method createDescribableMetricFamilySamples.

@Test
public void createDescribableMetricFamilySamples() {
    assertThat(PrometheusExportUtils.createDescribableMetricFamilySamples(CUMULATIVE_METRIC_DESCRIPTOR, "")).isEqualTo(new MetricFamilySamples(METRIC_NAME, Type.COUNTER, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
    assertThat(PrometheusExportUtils.createDescribableMetricFamilySamples(SUMMARY_METRIC_DESCRIPTOR, "")).isEqualTo(new MetricFamilySamples(METRIC_NAME2, Type.SUMMARY, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
    assertThat(PrometheusExportUtils.createDescribableMetricFamilySamples(HISTOGRAM_METRIC_DESCRIPTOR, "")).isEqualTo(new MetricFamilySamples(METRIC_NAME3, Type.HISTOGRAM, METRIC_DESCRIPTION, Collections.<Sample>emptyList()));
}
Also used : Sample(io.prometheus.client.Collector.MetricFamilySamples.Sample) MetricFamilySamples(io.prometheus.client.Collector.MetricFamilySamples) Test(org.junit.Test)

Aggregations

Sample (io.prometheus.client.Collector.MetricFamilySamples.Sample)44 MetricFamilySamples (io.prometheus.client.Collector.MetricFamilySamples)34 Test (org.junit.Test)32 LinkedList (java.util.LinkedList)27 Collector (io.prometheus.client.Collector)18 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Type (io.prometheus.client.Collector.Type)2 Collector.doubleToGoString (io.prometheus.client.Collector.doubleToGoString)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Meter (com.netflix.spectator.api.Meter)1 Registry (com.netflix.spectator.api.Registry)1 Tag (com.netflix.spectator.api.Tag)1 LabelValue (io.opencensus.metrics.LabelValue)1 Distribution (io.opencensus.metrics.export.Distribution)1 BucketOptions (io.opencensus.metrics.export.Distribution.BucketOptions)1 ExplicitOptions (io.opencensus.metrics.export.Distribution.BucketOptions.ExplicitOptions)1 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)1 Summary (io.opencensus.metrics.export.Summary)1 ValueAtPercentile (io.opencensus.metrics.export.Summary.Snapshot.ValueAtPercentile)1