| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package prometheus |
| 4 | |
| 5 | import ( |
| 6 | "math" |
| 7 | "slices" |
| 8 | |
| 9 | "github.com/prometheus/common/model" |
| 10 | "github.com/prometheus/prometheus/model/labels" |
| 11 | ) |
| 12 | |
| 13 | type ( |
| 14 | MetricFamilies map[string]*MetricFamily |
| 15 | |
| 16 | MetricFamily struct { |
| 17 | name string |
| 18 | help string |
| 19 | typ model.MetricType |
| 20 | metrics []Metric |
| 21 | } |
| 22 | Metric struct { |
| 23 | labels []labels.Label |
| 24 | gauge *Gauge |
| 25 | counter *Counter |
| 26 | summary *Summary |
| 27 | histogram *Histogram |
| 28 | untyped *Untyped |
| 29 | } |
| 30 | Gauge struct { |
| 31 | value float64 |
| 32 | } |
| 33 | Counter struct { |
| 34 | value float64 |
| 35 | } |
| 36 | Summary struct { |
| 37 | sum float64 |
| 38 | count float64 |
| 39 | quantiles []Quantile |
| 40 | } |
| 41 | Quantile struct { |
| 42 | quantile float64 |
| 43 | value float64 |
| 44 | } |
| 45 | Histogram struct { |
| 46 | sum float64 |
| 47 | count float64 |
| 48 | buckets []Bucket |
| 49 | } |
| 50 | Bucket struct { |
| 51 | upperBound float64 |
| 52 | cumulativeCount float64 |
| 53 | } |
| 54 | Untyped struct { |
| 55 | value float64 |
| 56 | } |
| 57 | ) |
| 58 | |
| 59 | func (mfs MetricFamilies) Len() int { |
| 60 | return len(mfs) |
| 61 | } |
| 62 | |
| 63 | func (mfs MetricFamilies) Get(name string) *MetricFamily { |
| 64 | return (mfs)[name] |
| 65 | } |
| 66 | |
| 67 | func (mfs MetricFamilies) GetGauge(name string) *MetricFamily { |
| 68 | return mfs.get(name, model.MetricTypeGauge) |
| 69 | } |
| 70 | |
| 71 | func (mfs MetricFamilies) GetCounter(name string) *MetricFamily { |
| 72 | return mfs.get(name, model.MetricTypeCounter) |
| 73 | } |
| 74 | |
| 75 | func (mfs MetricFamilies) GetSummary(name string) *MetricFamily { |
| 76 | return mfs.get(name, model.MetricTypeSummary) |
| 77 | } |
| 78 | |
| 79 | func (mfs MetricFamilies) GetHistogram(name string) *MetricFamily { |
| 80 | return mfs.get(name, model.MetricTypeHistogram) |
| 81 | } |
| 82 | |
| 83 | func (mfs MetricFamilies) get(name string, typ model.MetricType) *MetricFamily { |
| 84 | mf := mfs.Get(name) |
| 85 | if mf == nil || mf.typ != typ { |
| 86 | return nil |
| 87 | } |
| 88 | return mf |
| 89 | } |
| 90 | |
| 91 | func (mf *MetricFamily) Name() string { return mf.name } |
| 92 | func (mf *MetricFamily) Help() string { return mf.help } |
| 93 | func (mf *MetricFamily) Type() model.MetricType { return mf.typ } |
| 94 | func (mf *MetricFamily) Metrics() []Metric { return mf.metrics } |
| 95 | |
| 96 | func (m *Metric) Labels() labels.Labels { return m.labels } |
| 97 | func (m *Metric) Gauge() *Gauge { return m.gauge } |
| 98 | func (m *Metric) Counter() *Counter { return m.counter } |
| 99 | func (m *Metric) Summary() *Summary { return m.summary } |
| 100 | func (m *Metric) Histogram() *Histogram { return m.histogram } |
| 101 | func (m *Metric) Untyped() *Untyped { return m.untyped } |
| 102 | |
| 103 | func (g Gauge) Value() float64 { return g.value } |
| 104 | func (c Counter) Value() float64 { return c.value } |
| 105 | func (u Untyped) Value() float64 { return u.value } |
| 106 | |
| 107 | func (s Summary) Count() float64 { return s.count } |
| 108 | func (s Summary) Sum() float64 { return s.sum } |
| 109 | func (s Summary) Quantiles() []Quantile { return s.quantiles } |
| 110 | |
| 111 | // IsNaN reports whether every quantile value is NaN, which a Prometheus summary emits for an |
| 112 | // empty observation window (a summary with no quantiles also reports true). Callers skip such |
| 113 | // a summary so a chart is not created until it carries a real value. |
| 114 | func (s Summary) IsNaN() bool { |
| 115 | return !slices.ContainsFunc(s.quantiles, func(q Quantile) bool { return !math.IsNaN(q.value) }) |
| 116 | } |
| 117 | |
| 118 | func (q Quantile) Quantile() float64 { return q.quantile } |
| 119 | func (q Quantile) Value() float64 { return q.value } |
| 120 | |
| 121 | func (h Histogram) Count() float64 { return h.count } |
| 122 | func (h Histogram) Sum() float64 { return h.sum } |
| 123 | func (h Histogram) Buckets() []Bucket { return h.buckets } |
| 124 | |
| 125 | func (b Bucket) UpperBound() float64 { return b.upperBound } |
| 126 | func (b Bucket) CumulativeCount() float64 { return b.cumulativeCount } |