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

View File

@ -26,8 +26,6 @@ import (
"k8s.io/klog"
)
var hiddenFlag = "metricsshouldbehidden"
// Desc is a prometheus.Desc extension.
//
// Use NewDesc to create new Desc instances.
@ -72,20 +70,14 @@ type Desc struct {
func NewDesc(fqName string, help string, variableLabels []string, constLabels Labels,
stabilityLevel StabilityLevel, deprecatedVersion string) *Desc {
d := &Desc{
fqName: fqName,
help: help,
variableLabels: variableLabels,
constLabels: constLabels,
fqName: fqName,
help: help,
variableLabels: variableLabels,
constLabels: constLabels,
stabilityLevel: stabilityLevel,
deprecatedVersion: deprecatedVersion,
}
// 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
d.stabilityLevel.setDefaults()
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() {
d.annotateOnce.Do(func() {
d.help = fmt.Sprintf("[%v] %v", d.stabilityLevel, d.help)
@ -172,8 +156,3 @@ func (d *Desc) initializeDeprecatedDesc() {
d.markDeprecated()
d.initialize()
}
func (d *Desc) initializeHiddenDesc() {
d.markHidden()
d.initializeDeprecatedDesc()
}

View File

@ -16,7 +16,9 @@ limitations under the License.
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.
type ValueType int
@ -33,7 +35,12 @@ func (vt *ValueType) toPromValueType() prometheus.ValueType {
return prometheus.ValueType(*vt)
}
// MustNewConstMetric is a wrapper of prometheus.MustNewConstMetric
func MustNewConstMetric(desc *Desc, valueType ValueType, value float64, labelValues ...string) Metric {
// NewLazyConstMetric is a helper of MustNewConstMetric.
//
// 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...)
}