Search in sources :

Example 1 with Type

use of io.prometheus.client.Collector.Type 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 2 with Type

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

the class TextFormat004Parser method parseTypeLine.

private void parseTypeLine(String line) {
    Matcher m = PATTERN_PARSE_TYPE.matcher(line);
    if (!m.matches()) {
        log.warn("TYPE line could not be properly matched: " + line);
        return;
    }
    String metricName = unescapeToken(m.group(1));
    String typeString = m.group(2);
    Collector.Type type = null;
    if (typeString.equalsIgnoreCase("gauge")) {
        type = Collector.Type.GAUGE;
    } else if (typeString.equalsIgnoreCase("counter")) {
        type = Collector.Type.COUNTER;
    } else if (typeString.equalsIgnoreCase("summary")) {
        type = Collector.Type.SUMMARY;
    } else if (typeString.equalsIgnoreCase("histogram")) {
        type = Collector.Type.HISTOGRAM;
    } else if (typeString.equalsIgnoreCase("untyped")) {
        type = Collector.Type.UNTYPED;
    } else {
        log.warn("Unable to parse type from TYPE line: " + line);
        return;
    }
    this.mapTypes.put(metricName, type);
}
Also used : Matcher(java.util.regex.Matcher) Type(io.prometheus.client.Collector.Type) Collector(io.prometheus.client.Collector)

Example 3 with Type

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

the class TextFormat004Parser method determineType.

private Type determineType(String metricName) {
    Collector.Type type = null;
    // first check if the metric is typed natively.
    type = this.mapTypes.get(metricName);
    if (type != null) {
        return type;
    }
    // try to get the baseMetricName
    String baseMetricName = determineBaseMetricName(metricName);
    type = this.mapTypes.get(baseMetricName);
    // check that this also really makes sense and is a type, which requires baseMetricNames
    if (type == Type.HISTOGRAM || type == Type.SUMMARY) {
        return type;
    }
    // we have no clue what this metric is all about
    log.info("Definition of metric without type information (assuming untyped)");
    return Collector.Type.UNTYPED;
}
Also used : Type(io.prometheus.client.Collector.Type) Collector(io.prometheus.client.Collector)

Example 4 with Type

use of io.prometheus.client.Collector.Type 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 5 with Type

use of io.prometheus.client.Collector.Type 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

Type (io.prometheus.client.Collector.Type)5 Collector (io.prometheus.client.Collector)3 MetricFamilySamples (io.prometheus.client.Collector.MetricFamilySamples)2 Sample (io.prometheus.client.Collector.MetricFamilySamples.Sample)2 Collector.doubleToGoString (io.prometheus.client.Collector.doubleToGoString)2 Matcher (java.util.regex.Matcher)2 MetricDescriptor (io.opencensus.metrics.export.MetricDescriptor)1 LinkedList (java.util.LinkedList)1