1
// Copyright 2018 The CubeFS Authors.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
// implied. See the License for the specific language governing
13
// permissions and limitations under the License.
21
"github.com/cubefs/cubefs/util/log"
22
"github.com/prometheus/client_golang/prometheus"
31
GaugeCh = make(chan *Gauge, ChSize)
36
// log.LogDebugf("collect metric %v", m)
42
labels map[string]string
46
func NewGauge(name string) (g *Gauge) {
48
g.name = metricsName(name)
52
func (c *Gauge) Key() (key string) {
53
return stringMD5(c.Name())
56
func (g *Gauge) Name() string {
57
return fmt.Sprintf("{%s: %s}", g.name, stringMapToString(g.labels))
60
func (g *Gauge) String() string {
61
return fmt.Sprintf("{name: %s, labels: %s, val: %v}", g.name, stringMapToString(g.labels), g.val)
64
func (c *Gauge) Metric() prometheus.Gauge {
65
metric := prometheus.NewGauge(
68
ConstLabels: c.labels,
71
actualMetric, load := GaugeGroup.LoadOrStore(key, metric)
73
return actualMetric.(prometheus.Gauge)
77
registry.MustRegister(actualMetric.(prometheus.Collector))
78
return actualMetric.(prometheus.Gauge)
81
err := prometheus.Register(actualMetric.(prometheus.Collector))
83
log.LogInfof("register metric %v", c.Name())
85
log.LogErrorf("register metric %v, %v", c.Name(), err)
88
return actualMetric.(prometheus.Gauge)
91
func (g *Gauge) Set(val float64) {
92
if !enabledPrometheus {
99
func (c *Gauge) publish() {
106
func (g *Gauge) SetWithLabels(val float64, labels map[string]string) {
107
if !enabledPrometheus {
114
type GaugeVec struct {
118
func NewGaugeVec(name, help string, labels []string) *GaugeVec {
119
if !enabledPrometheus {
122
v := prometheus.NewGaugeVec(
123
prometheus.GaugeOpts{
124
Name: metricsName(name),
130
if err := prometheus.Register(v); err != nil {
131
log.LogErrorf("prometheus register gaugevec name:%v, labels:{%v} error: %v", name, labels, err)
135
return &GaugeVec{GaugeVec: v}
138
func (v *GaugeVec) SetWithLabelValues(val float64, lvs ...string) {
139
if m, err := v.GetMetricWithLabelValues(lvs...); err == nil {
144
func (v *GaugeVec) SetBoolWithLabelValues(val bool, lvs ...string) {
146
v.SetWithLabelValues(float64(1), lvs...)
148
v.SetWithLabelValues(0, lvs...)