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 Unknown (Untyped) metric.
009 */
010public final class UnknownSnapshot extends MetricSnapshot {
011
012    /**
013     * To create a new {@link UnknownSnapshot}, you can either call the constructor directly or use
014     * the builder with {@link UnknownSnapshot#builder()}.
015     *
016     * @param metadata required name and optional help and unit.
017     *                 See {@link MetricMetadata} for naming conventions.
018     * @param data     the constructor will create a sorted copy of the collection.
019     */
020    public UnknownSnapshot(MetricMetadata metadata, Collection<UnknownDataPointSnapshot> data) {
021        super(metadata, data);
022    }
023
024    @Override
025    public List<UnknownDataPointSnapshot> getDataPoints() {
026        return (List<UnknownDataPointSnapshot>) dataPoints;
027    }
028
029    public static final class UnknownDataPointSnapshot extends DataPointSnapshot {
030
031        private final double value;
032        private final Exemplar exemplar; // may be null
033
034        /**
035         * To create a new {@link UnknownDataPointSnapshot}, you can either call the constructor directly or use the
036         * Builder with {@link UnknownDataPointSnapshot#builder()}.
037         *
038         * @param value    the value.
039         * @param labels   must not be null. Use {@link Labels#EMPTY} if there are no labels.
040         * @param exemplar may be null.
041         */
042        public UnknownDataPointSnapshot(double value, Labels labels, Exemplar exemplar) {
043            this(value, labels, exemplar, 0);
044        }
045
046        /**
047         * Constructor with an additional scrape timestamp.
048         * This is only useful in rare cases as the scrape timestamp is usually set by the Prometheus server
049         * during scraping. Exceptions include mirroring metrics with given timestamps from other metric sources.
050         */
051        public UnknownDataPointSnapshot(double value, Labels labels, Exemplar exemplar, long scrapeTimestampMillis) {
052            super(labels, 0L, scrapeTimestampMillis);
053            this.value = value;
054            this.exemplar = exemplar;
055        }
056
057        public double getValue() {
058            return value;
059        }
060
061        /**
062         * May return {@code null}.
063         */
064        public Exemplar getExemplar() {
065            return exemplar;
066        }
067
068        public static Builder builder() {
069            return new Builder();
070        }
071
072        public static class Builder extends DataPointSnapshot.Builder<Builder> {
073
074            private Exemplar exemplar = null;
075            private Double value = null;
076
077            private Builder() {
078            }
079
080            /**
081             * required.
082             */
083            public Builder value(double value) {
084                this.value = value;
085                return this;
086            }
087
088            /**
089             * Optional
090             */
091            public Builder exemplar(Exemplar exemplar) {
092                this.exemplar = exemplar;
093                return this;
094            }
095
096            public UnknownDataPointSnapshot build() {
097                if (value == null) {
098                    throw new IllegalArgumentException("Missing required field: value is null.");
099                }
100                return new UnknownDataPointSnapshot(value, labels, exemplar, scrapeTimestampMillis);
101            }
102
103            @Override
104            protected Builder self() {
105                return this;
106            }
107        }
108    }
109
110    public static Builder builder() {
111        return new Builder();
112    }
113
114    public static class Builder extends MetricSnapshot.Builder<Builder> {
115
116        private final List<UnknownDataPointSnapshot> dataPoints = new ArrayList<>();
117
118        private Builder() {
119        }
120
121        /**
122         * Add a data point. Call multiple times to add multiple data points.
123         */
124        public Builder dataPoint(UnknownDataPointSnapshot data) {
125            dataPoints.add(data);
126            return this;
127        }
128
129        @Override
130        public UnknownSnapshot build() {
131            return new UnknownSnapshot(buildMetadata(), dataPoints);
132        }
133
134        @Override
135        protected Builder self() {
136            return this;
137        }
138    }
139}