Stop create hidden metrics for custom metrics

This commit is contained in:
RainbowMango 2019-11-05 13:04:20 +08:00
parent 70b245e8ae
commit d20e223b4b
4 changed files with 28 additions and 46 deletions

View File

@ -18,7 +18,6 @@ package metrics
import ( import (
"fmt" "fmt"
"strings"
"github.com/blang/semver" "github.com/blang/semver"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -46,7 +45,6 @@ type StableCollector interface {
type BaseStableCollector struct { type BaseStableCollector struct {
descriptors []*Desc // stores all Desc collected from DescribeWithStability(). descriptors []*Desc // stores all Desc collected from DescribeWithStability().
registrable []*Desc // stores registrable Desc(not be hidden), is a subset of descriptors. registrable []*Desc // stores registrable Desc(not be hidden), is a subset of descriptors.
hidden []*Desc // stores hidden Desc
self StableCollector self StableCollector
} }
@ -59,7 +57,7 @@ func (bsc *BaseStableCollector) DescribeWithStability(ch chan<- *Desc) {
// Describe sends all descriptors to the provided channel. // Describe sends all descriptors to the provided channel.
// It intend to be called by prometheus registry. // It intend to be called by prometheus registry.
func (bsc *BaseStableCollector) Describe(ch chan<- *prometheus.Desc) { func (bsc *BaseStableCollector) Describe(ch chan<- *prometheus.Desc) {
for _, d := range bsc.descriptors { for _, d := range bsc.registrable {
ch <- d.toPrometheusDesc() ch <- d.toPrometheusDesc()
} }
} }
@ -80,10 +78,8 @@ func (bsc *BaseStableCollector) Collect(ch chan<- prometheus.Metric) {
}() }()
for m := range mch { for m := range mch {
// Hidden metrics should be ignored. // nil Metric usually means hidden metrics
// TODO(RainbowMango): There is no convenient method to identify if the metrics should be ignored. if m == nil {
// Use a temporary solution here. (try to search in hidden list)
if strings.Contains(m.Desc().String(), hiddenFlag) {
continue continue
} }
@ -127,16 +123,16 @@ func (bsc *BaseStableCollector) Create(version *semver.Version, self StableColle
d.createLock.Lock() d.createLock.Lock()
defer d.createLock.Unlock() defer d.createLock.Unlock()
d.isCreated = true if d.IsHidden() {
if d.IsHidden() { // hidden metrics also needs initialize because user may send them in CollectWithStability method. // do nothing for hidden metrics
d.initializeHiddenDesc()
bsc.hidden = append(bsc.hidden, d)
} else if d.IsDeprecated() { } else if d.IsDeprecated() {
d.initializeDeprecatedDesc() d.initializeDeprecatedDesc()
bsc.registrable = append(bsc.registrable, d) bsc.registrable = append(bsc.registrable, d)
d.isCreated = true
} else { } else {
d.initialize() d.initialize()
bsc.registrable = append(bsc.registrable, d) bsc.registrable = append(bsc.registrable, d)
d.isCreated = true
} }
}) })
} }

View File

@ -52,25 +52,25 @@ func (tc *testCustomCollector) DescribeWithStability(ch chan<- *Desc) {
} }
func (tc *testCustomCollector) CollectWithStability(ch chan<- Metric) { func (tc *testCustomCollector) CollectWithStability(ch chan<- Metric) {
ch <- MustNewConstMetric( ch <- NewLazyConstMetric(
alphaDesc, alphaDesc,
GaugeValue, GaugeValue,
1, 1,
"value", "value",
) )
ch <- MustNewConstMetric( ch <- NewLazyConstMetric(
stableDesc, stableDesc,
GaugeValue, GaugeValue,
1, 1,
"value", "value",
) )
ch <- MustNewConstMetric( ch <- NewLazyConstMetric(
deprecatedDesc, deprecatedDesc,
GaugeValue, GaugeValue,
1, 1,
"value", "value",
) )
ch <- MustNewConstMetric( ch <- NewLazyConstMetric(
hiddenDesc, hiddenDesc,
GaugeValue, GaugeValue,
1, 1,

View File

@ -26,8 +26,6 @@ import (
"k8s.io/klog" "k8s.io/klog"
) )
var hiddenFlag = "metricsshouldbehidden"
// Desc is a prometheus.Desc extension. // Desc is a prometheus.Desc extension.
// //
// Use NewDesc to create new Desc instances. // Use NewDesc to create new Desc instances.
@ -72,20 +70,14 @@ type Desc struct {
func NewDesc(fqName string, help string, variableLabels []string, constLabels Labels, func NewDesc(fqName string, help string, variableLabels []string, constLabels Labels,
stabilityLevel StabilityLevel, deprecatedVersion string) *Desc { stabilityLevel StabilityLevel, deprecatedVersion string) *Desc {
d := &Desc{ d := &Desc{
fqName: fqName, fqName: fqName,
help: help, help: help,
variableLabels: variableLabels, variableLabels: variableLabels,
constLabels: constLabels, constLabels: constLabels,
stabilityLevel: stabilityLevel,
deprecatedVersion: deprecatedVersion,
} }
d.stabilityLevel.setDefaults()
// TODO(RainbowMango): replace by stabilityLevel.setDefault() after PR(https://github.com/kubernetes/kubernetes/pull/82957) be merged.
if stabilityLevel == "" {
d.stabilityLevel = ALPHA
} else {
d.stabilityLevel = stabilityLevel
}
d.deprecatedVersion = deprecatedVersion
return d return d
} }
@ -148,14 +140,6 @@ func (d *Desc) markDeprecated() {
}) })
} }
// markHidden mark a special flag to 'HELP'.
// The metrics marked with this flag will be ignored when collecting automatically.
func (d *Desc) markHidden() {
d.hideOnce.Do(func() {
d.help = fmt.Sprintf("(%s) %s", hiddenFlag, d.help)
})
}
func (d *Desc) annotateStabilityLevel() { func (d *Desc) annotateStabilityLevel() {
d.annotateOnce.Do(func() { d.annotateOnce.Do(func() {
d.help = fmt.Sprintf("[%v] %v", d.stabilityLevel, d.help) d.help = fmt.Sprintf("[%v] %v", d.stabilityLevel, d.help)
@ -172,8 +156,3 @@ func (d *Desc) initializeDeprecatedDesc() {
d.markDeprecated() d.markDeprecated()
d.initialize() d.initialize()
} }
func (d *Desc) initializeHiddenDesc() {
d.markHidden()
d.initializeDeprecatedDesc()
}

View File

@ -16,7 +16,9 @@ limitations under the License.
package metrics package metrics
import "github.com/prometheus/client_golang/prometheus" import (
"github.com/prometheus/client_golang/prometheus"
)
// ValueType is an enumeration of metric types that represent a simple value. // ValueType is an enumeration of metric types that represent a simple value.
type ValueType int type ValueType int
@ -33,7 +35,12 @@ func (vt *ValueType) toPromValueType() prometheus.ValueType {
return prometheus.ValueType(*vt) return prometheus.ValueType(*vt)
} }
// MustNewConstMetric is a wrapper of prometheus.MustNewConstMetric // NewLazyConstMetric is a helper of MustNewConstMetric.
func MustNewConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) Metric { //
// Note: If the metrics described by the desc is hidden, the metrics will not be created.
func NewLazyConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) Metric {
if desc.IsHidden() {
return nil
}
return prometheus.MustNewConstMetric(desc.toPrometheusDesc(), valueType.toPromValueType(), value, labelValues...) return prometheus.MustNewConstMetric(desc.toPrometheusDesc(), valueType.toPromValueType(), value, labelValues...)
} }