|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package metrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/blang/semver" |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | +) |
| 25 | + |
| 26 | +// StableCollector extends the prometheus.Collector interface to allow customization of the |
| 27 | +// metric registration process, it's especially intend to be used in scenario of custom collector. |
| 28 | +type StableCollector interface { |
| 29 | + prometheus.Collector |
| 30 | + |
| 31 | + // DescribeWithStability sends the super-set of all possible metrics.Desc collected |
| 32 | + // by this StableCollector to the provided channel. |
| 33 | + DescribeWithStability(chan<- *Desc) |
| 34 | + |
| 35 | + // CollectWithStability sends each collected metrics.Metric via the provide channel. |
| 36 | + CollectWithStability(chan<- Metric) |
| 37 | + |
| 38 | + // Create will initialize all Desc and it intends to be called by registry. |
| 39 | + Create(version *semver.Version, self StableCollector) bool |
| 40 | +} |
| 41 | + |
| 42 | +// BaseStableCollector which implements almost all of the methods defined by StableCollector |
| 43 | +// is a convenient assistant for custom collectors. |
| 44 | +// It is recommend that inherit BaseStableCollector when implementing custom collectors. |
| 45 | +type BaseStableCollector struct { |
| 46 | + descriptors []*Desc // stores all Desc collected from DescribeWithStability(). |
| 47 | + registrable []*Desc // stores registrable Desc(not be hidden), is a subset of descriptors. |
| 48 | + self StableCollector |
| 49 | +} |
| 50 | + |
| 51 | +// DescribeWithStability sends all descriptors to the provided channel. |
| 52 | +// Every custom collector should over-write this method. |
| 53 | +func (bsc *BaseStableCollector) DescribeWithStability(ch chan<- *Desc) { |
| 54 | + panic(fmt.Errorf("custom collector should over-write DescribeWithStability method")) |
| 55 | +} |
| 56 | + |
| 57 | +// Describe sends all descriptors to the provided channel. |
| 58 | +// It intend to be called by prometheus registry. |
| 59 | +func (bsc *BaseStableCollector) Describe(ch chan<- *prometheus.Desc) { |
| 60 | + for _, d := range bsc.registrable { |
| 61 | + ch <- d.toPrometheusDesc() |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// CollectWithStability sends all metrics to the provided channel. |
| 66 | +// Every custom collector should over-write this method. |
| 67 | +func (bsc *BaseStableCollector) CollectWithStability(ch chan<- Metric) { |
| 68 | + panic(fmt.Errorf("custom collector should over-write CollectWithStability method")) |
| 69 | +} |
| 70 | + |
| 71 | +// Collect is called by the Prometheus registry when collecting metrics. |
| 72 | +func (bsc *BaseStableCollector) Collect(ch chan<- prometheus.Metric) { |
| 73 | + mch := make(chan Metric) |
| 74 | + |
| 75 | + go func() { |
| 76 | + bsc.self.CollectWithStability(mch) |
| 77 | + close(mch) |
| 78 | + }() |
| 79 | + |
| 80 | + for m := range mch { |
| 81 | + // nil Metric usually means hidden metrics |
| 82 | + if m == nil { |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + ch <- prometheus.Metric(m) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func (bsc *BaseStableCollector) add(d *Desc) { |
| 91 | + bsc.descriptors = append(bsc.descriptors, d) |
| 92 | +} |
| 93 | + |
| 94 | +// Init intends to be called by registry. |
| 95 | +func (bsc *BaseStableCollector) init(self StableCollector) { |
| 96 | + bsc.self = self |
| 97 | + |
| 98 | + dch := make(chan *Desc) |
| 99 | + |
| 100 | + // collect all possible descriptions from custom side |
| 101 | + go func() { |
| 102 | + bsc.self.DescribeWithStability(dch) |
| 103 | + close(dch) |
| 104 | + }() |
| 105 | + |
| 106 | + for d := range dch { |
| 107 | + bsc.add(d) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Create intends to be called by registry. |
| 112 | +// Create will return true as long as there is one or more metrics not be hidden. |
| 113 | +// Otherwise return false, that means the whole collector will be ignored by registry. |
| 114 | +func (bsc *BaseStableCollector) Create(version *semver.Version, self StableCollector) bool { |
| 115 | + bsc.init(self) |
| 116 | + |
| 117 | + for _, d := range bsc.descriptors { |
| 118 | + if version != nil { |
| 119 | + d.determineDeprecationStatus(*version) |
| 120 | + } |
| 121 | + |
| 122 | + d.createOnce.Do(func() { |
| 123 | + d.createLock.Lock() |
| 124 | + defer d.createLock.Unlock() |
| 125 | + |
| 126 | + if d.IsHidden() { |
| 127 | + // do nothing for hidden metrics |
| 128 | + } else if d.IsDeprecated() { |
| 129 | + d.initializeDeprecatedDesc() |
| 130 | + bsc.registrable = append(bsc.registrable, d) |
| 131 | + d.isCreated = true |
| 132 | + } else { |
| 133 | + d.initialize() |
| 134 | + bsc.registrable = append(bsc.registrable, d) |
| 135 | + d.isCreated = true |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | + |
| 140 | + if len(bsc.registrable) > 0 { |
| 141 | + return true |
| 142 | + } |
| 143 | + |
| 144 | + return false |
| 145 | +} |
| 146 | + |
| 147 | +// Check if our BaseStableCollector implements necessary interface |
| 148 | +var _ StableCollector = &BaseStableCollector{} |
0 commit comments