khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | // Copyright 2014 The Prometheus Authors |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package prometheus |
| 15 | |
| 16 | import ( |
| 17 | "strings" |
| 18 | "time" |
| 19 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame^] | 20 | //nolint:staticcheck // Ignore SA1019. Need to keep deprecated package for compatibility. |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 21 | "github.com/golang/protobuf/proto" |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame^] | 22 | "github.com/prometheus/common/model" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 23 | |
| 24 | dto "github.com/prometheus/client_model/go" |
| 25 | ) |
| 26 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame^] | 27 | var separatorByteSlice = []byte{model.SeparatorByte} // For convenient use with xxhash. |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 28 | |
| 29 | // A Metric models a single sample value with its meta data being exported to |
| 30 | // Prometheus. Implementations of Metric in this package are Gauge, Counter, |
| 31 | // Histogram, Summary, and Untyped. |
| 32 | type Metric interface { |
| 33 | // Desc returns the descriptor for the Metric. This method idempotently |
| 34 | // returns the same descriptor throughout the lifetime of the |
| 35 | // Metric. The returned descriptor is immutable by contract. A Metric |
| 36 | // unable to describe itself must return an invalid descriptor (created |
| 37 | // with NewInvalidDesc). |
| 38 | Desc() *Desc |
| 39 | // Write encodes the Metric into a "Metric" Protocol Buffer data |
| 40 | // transmission object. |
| 41 | // |
| 42 | // Metric implementations must observe concurrency safety as reads of |
| 43 | // this metric may occur at any time, and any blocking occurs at the |
| 44 | // expense of total performance of rendering all registered |
| 45 | // metrics. Ideally, Metric implementations should support concurrent |
| 46 | // readers. |
| 47 | // |
| 48 | // While populating dto.Metric, it is the responsibility of the |
| 49 | // implementation to ensure validity of the Metric protobuf (like valid |
| 50 | // UTF-8 strings or syntactically valid metric and label names). It is |
| 51 | // recommended to sort labels lexicographically. Callers of Write should |
| 52 | // still make sure of sorting if they depend on it. |
| 53 | Write(*dto.Metric) error |
| 54 | // TODO(beorn7): The original rationale of passing in a pre-allocated |
| 55 | // dto.Metric protobuf to save allocations has disappeared. The |
| 56 | // signature of this method should be changed to "Write() (*dto.Metric, |
| 57 | // error)". |
| 58 | } |
| 59 | |
| 60 | // Opts bundles the options for creating most Metric types. Each metric |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame^] | 61 | // implementation XXX has its own XXXOpts type, but in most cases, it is just |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 62 | // an alias of this type (which might change when the requirement arises.) |
| 63 | // |
| 64 | // It is mandatory to set Name to a non-empty string. All other fields are |
| 65 | // optional and can safely be left at their zero value, although it is strongly |
| 66 | // encouraged to set a Help string. |
| 67 | type Opts struct { |
| 68 | // Namespace, Subsystem, and Name are components of the fully-qualified |
| 69 | // name of the Metric (created by joining these components with |
| 70 | // "_"). Only Name is mandatory, the others merely help structuring the |
| 71 | // name. Note that the fully-qualified name of the metric must be a |
| 72 | // valid Prometheus metric name. |
| 73 | Namespace string |
| 74 | Subsystem string |
| 75 | Name string |
| 76 | |
| 77 | // Help provides information about this metric. |
| 78 | // |
| 79 | // Metrics with the same fully-qualified name must have the same Help |
| 80 | // string. |
| 81 | Help string |
| 82 | |
| 83 | // ConstLabels are used to attach fixed labels to this metric. Metrics |
| 84 | // with the same fully-qualified name must have the same label names in |
| 85 | // their ConstLabels. |
| 86 | // |
| 87 | // ConstLabels are only used rarely. In particular, do not use them to |
| 88 | // attach the same labels to all your metrics. Those use cases are |
| 89 | // better covered by target labels set by the scraping Prometheus |
| 90 | // server, or by one specific metric (e.g. a build_info or a |
| 91 | // machine_role metric). See also |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame^] | 92 | // https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels-not-static-scraped-labels |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 93 | ConstLabels Labels |
| 94 | } |
| 95 | |
| 96 | // BuildFQName joins the given three name components by "_". Empty name |
| 97 | // components are ignored. If the name parameter itself is empty, an empty |
| 98 | // string is returned, no matter what. Metric implementations included in this |
| 99 | // library use this function internally to generate the fully-qualified metric |
| 100 | // name from the name component in their Opts. Users of the library will only |
| 101 | // need this function if they implement their own Metric or instantiate a Desc |
| 102 | // (with NewDesc) directly. |
| 103 | func BuildFQName(namespace, subsystem, name string) string { |
| 104 | if name == "" { |
| 105 | return "" |
| 106 | } |
| 107 | switch { |
| 108 | case namespace != "" && subsystem != "": |
| 109 | return strings.Join([]string{namespace, subsystem, name}, "_") |
| 110 | case namespace != "": |
| 111 | return strings.Join([]string{namespace, name}, "_") |
| 112 | case subsystem != "": |
| 113 | return strings.Join([]string{subsystem, name}, "_") |
| 114 | } |
| 115 | return name |
| 116 | } |
| 117 | |
| 118 | // labelPairSorter implements sort.Interface. It is used to sort a slice of |
| 119 | // dto.LabelPair pointers. |
| 120 | type labelPairSorter []*dto.LabelPair |
| 121 | |
| 122 | func (s labelPairSorter) Len() int { |
| 123 | return len(s) |
| 124 | } |
| 125 | |
| 126 | func (s labelPairSorter) Swap(i, j int) { |
| 127 | s[i], s[j] = s[j], s[i] |
| 128 | } |
| 129 | |
| 130 | func (s labelPairSorter) Less(i, j int) bool { |
| 131 | return s[i].GetName() < s[j].GetName() |
| 132 | } |
| 133 | |
| 134 | type invalidMetric struct { |
| 135 | desc *Desc |
| 136 | err error |
| 137 | } |
| 138 | |
| 139 | // NewInvalidMetric returns a metric whose Write method always returns the |
| 140 | // provided error. It is useful if a Collector finds itself unable to collect |
| 141 | // a metric and wishes to report an error to the registry. |
| 142 | func NewInvalidMetric(desc *Desc, err error) Metric { |
| 143 | return &invalidMetric{desc, err} |
| 144 | } |
| 145 | |
| 146 | func (m *invalidMetric) Desc() *Desc { return m.desc } |
| 147 | |
| 148 | func (m *invalidMetric) Write(*dto.Metric) error { return m.err } |
| 149 | |
| 150 | type timestampedMetric struct { |
| 151 | Metric |
| 152 | t time.Time |
| 153 | } |
| 154 | |
| 155 | func (m timestampedMetric) Write(pb *dto.Metric) error { |
| 156 | e := m.Metric.Write(pb) |
| 157 | pb.TimestampMs = proto.Int64(m.t.Unix()*1000 + int64(m.t.Nanosecond()/1000000)) |
| 158 | return e |
| 159 | } |
| 160 | |
| 161 | // NewMetricWithTimestamp returns a new Metric wrapping the provided Metric in a |
| 162 | // way that it has an explicit timestamp set to the provided Time. This is only |
| 163 | // useful in rare cases as the timestamp of a Prometheus metric should usually |
| 164 | // be set by the Prometheus server during scraping. Exceptions include mirroring |
| 165 | // metrics with given timestamps from other metric |
| 166 | // sources. |
| 167 | // |
| 168 | // NewMetricWithTimestamp works best with MustNewConstMetric, |
| 169 | // MustNewConstHistogram, and MustNewConstSummary, see example. |
| 170 | // |
| 171 | // Currently, the exposition formats used by Prometheus are limited to |
| 172 | // millisecond resolution. Thus, the provided time will be rounded down to the |
| 173 | // next full millisecond value. |
| 174 | func NewMetricWithTimestamp(t time.Time, m Metric) Metric { |
| 175 | return timestampedMetric{Metric: m, t: t} |
| 176 | } |