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