001package io.prometheus.metrics.model.snapshots;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.List;
006
007/**
008 * Immutable snapshot of an Info metric.
009 */
010public final class InfoSnapshot extends MetricSnapshot {
011
012    /**
013     * To create a new {@link InfoSnapshot}, you can either call the constructor directly or use
014     * the builder with {@link InfoSnapshot#builder()}.
015     *
016     * @param metadata the metric name in metadata must not include the {@code _info} suffix.
017     *                 See {@link MetricMetadata} for more naming conventions.
018     *                 The metadata must not have a unit.
019     * @param data     the constructor will create a sorted copy of the collection.
020     */
021    public InfoSnapshot(MetricMetadata metadata, Collection<InfoDataPointSnapshot> data) {
022        super(metadata, data);
023        if (metadata.hasUnit()) {
024            throw new IllegalArgumentException("An Info metric cannot have a unit.");
025        }
026    }
027
028    @Override
029    public List<InfoDataPointSnapshot> getDataPoints() {
030        return (List<InfoDataPointSnapshot>) dataPoints;
031    }
032
033    public static class InfoDataPointSnapshot extends DataPointSnapshot {
034
035        /**
036         * To create a new {@link InfoDataPointSnapshot}, you can either call the constructor directly or use the
037         * Builder with {@link InfoDataPointSnapshot#builder()}.
038         *
039         * @param labels must not be null. Use {@link Labels#EMPTY} if there are no labels.
040         */
041        public InfoDataPointSnapshot(Labels labels) {
042            this(labels, 0L);
043        }
044
045        /**
046         * Constructor with an additional scrape timestamp.
047         * This is only useful in rare cases as the scrape timestamp is usually set by the Prometheus server
048         * during scraping. Exceptions include mirroring metrics with given timestamps from other metric sources.
049         */
050        public InfoDataPointSnapshot(Labels labels, long scrapeTimestampMillis) {
051            super(labels, 0L, scrapeTimestampMillis);
052        }
053
054        public static Builder builder() {
055            return new Builder();
056        }
057
058        public static class Builder extends DataPointSnapshot.Builder<Builder> {
059
060            private Builder() {
061            }
062
063            public InfoDataPointSnapshot build() {
064                return new InfoDataPointSnapshot(labels, scrapeTimestampMillis);
065            }
066
067            @Override
068            protected Builder self() {
069                return this;
070            }
071        }
072    }
073
074    public static Builder builder() {
075        return new Builder();
076    }
077
078    public static class Builder extends MetricSnapshot.Builder<Builder> {
079
080        private final List<InfoDataPointSnapshot> dataPoints = new ArrayList<>();
081
082        private Builder() {
083        }
084
085        /**
086         * Add a data point. Call multiple times for adding multiple data points.
087         */
088        public Builder dataPoint(InfoDataPointSnapshot dataPoint) {
089            dataPoints.add(dataPoint);
090            return this;
091        }
092
093        @Override
094        public Builder unit(Unit unit) {
095            throw new IllegalArgumentException("Info metric cannot have a unit.");
096        }
097
098        @Override
099        public InfoSnapshot build() {
100            return new InfoSnapshot(buildMetadata(), dataPoints);
101        }
102
103        @Override
104        protected Builder self() {
105            return this;
106        }
107    }
108}