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