-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add constant labels for the metrics (#385) #386
Changes from 16 commits
bfc7c43
503c362
afb32cd
edba6ff
1965a9f
9428e39
999da10
8b4b522
fa9d68e
3330d3d
a9849d0
79864b9
b682634
ae1eac3
d5b98bd
5f893d4
ef29e36
bbb1a29
2ac06e3
149c052
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package conf | ||
|
||
import ( | ||
"github.com/megaease/easeprobe/probe" | ||
) | ||
|
||
// MergeConstLabels merge const labels from all probers. | ||
// | ||
// Prometheus requires all metric of the same name have the same set of labels in a collector | ||
func MergeConstLabels(ps []probe.Prober) { | ||
var constLabels = make(map[string]bool) | ||
for _, p := range ps { | ||
for k, _ := range p.LabelMap() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please fix this and the following code lint warnings There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. |
||
constLabels[k] = true | ||
} | ||
} | ||
|
||
for _, p := range ps { | ||
buildConstLabels(p, constLabels) | ||
} | ||
} | ||
|
||
func buildConstLabels(p probe.Prober, constLabels map[string]bool) { | ||
ls := p.LabelMap() | ||
if ls == nil { | ||
ls = make(map[string]string) | ||
p.SetLabelMap(ls) | ||
} | ||
|
||
for k, _ := range constLabels { | ||
if _, ok := ls[k]; !ok { | ||
ls[k] = "" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package conf | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/megaease/easeprobe/probe" | ||
"github.com/megaease/easeprobe/probe/base" | ||
"github.com/megaease/easeprobe/probe/http" | ||
"github.com/megaease/easeprobe/probe/shell" | ||
"github.com/megaease/easeprobe/probe/tcp" | ||
) | ||
|
||
func TestMergeConstLabels(t *testing.T) { | ||
|
||
ps := []probe.Prober{ | ||
&http.HTTP{ | ||
DefaultProbe: base.DefaultProbe{ | ||
Labels: prometheus.Labels{"service": "service_a"}, | ||
}, | ||
}, | ||
&tcp.TCP{ | ||
DefaultProbe: base.DefaultProbe{ | ||
Labels: prometheus.Labels{"host": "host_b"}, | ||
}, | ||
}, | ||
&shell.Shell{}, | ||
} | ||
|
||
MergeConstLabels(ps) | ||
|
||
assert.Equal(t, 2, len(ps[0].LabelMap())) | ||
assert.Equal(t, "service_a", ps[0].LabelMap()["service"]) | ||
assert.Equal(t, "", ps[0].LabelMap()["host"]) | ||
|
||
assert.Equal(t, 2, len(ps[1].LabelMap())) | ||
assert.Equal(t, "", ps[1].LabelMap()["service"]) | ||
assert.Equal(t, "host_b", ps[1].LabelMap()["host"]) | ||
|
||
assert.Equal(t, 2, len(ps[2].LabelMap())) | ||
assert.Equal(t, "", ps[2].LabelMap()["service"]) | ||
assert.Equal(t, "", ps[2].LabelMap()["host"]) | ||
} |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -34,6 +34,7 @@ type MetricsType interface { | |||
} | ||||
|
||||
var ( | ||||
registries = make([]*prometheus.Registry, 0) | ||||
counterMap = make(map[string]*prometheus.CounterVec) | ||||
gaugeMap = make(map[string]*prometheus.GaugeVec) | ||||
histogramMap = make(map[string]*prometheus.HistogramVec) | ||||
|
@@ -57,11 +58,12 @@ func Gauge(key string) *prometheus.GaugeVec { | |||
|
||||
// NewCounter create the counter metric | ||||
func NewCounter(namespace, subsystem, name, metric string, | ||||
help string, labels []string) *prometheus.CounterVec { | ||||
help string, labels []string, constLabels prometheus.Labels) *prometheus.CounterVec { | ||||
|
||||
metricName, err := getAndValid(namespace, subsystem, name, metric, labels) | ||||
metricName, err := getAndValid(namespace, subsystem, name, metric, labels, constLabels) | ||||
if err != nil { | ||||
log.Errorf("[%s] %v", module, err) | ||||
log.Errorf("[namespace: %s, subsystem: %s, name: %s, metric: %s] %v", | ||||
namespace, subsystem, name, metric, err) | ||||
return nil | ||||
} | ||||
|
||||
|
@@ -75,23 +77,25 @@ func NewCounter(namespace, subsystem, name, metric string, | |||
Name: metricName, | ||||
Help: help, | ||||
}, | ||||
labels, | ||||
mergeLabels(labels, constLabels), | ||||
) | ||||
prometheus.MustRegister(counterMap[metricName]) | ||||
|
||||
// registries[len(registries)-1].MustRegister(m) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated. |
||||
prometheus.MustRegister(counterMap[metricName]) | ||||
log.Infof("[%s] Counter <%s> is created!", module, metricName) | ||||
return counterMap[metricName] | ||||
} | ||||
|
||||
// NewGauge create the gauge metric | ||||
func NewGauge(namespace, subsystem, name, metric string, | ||||
help string, labels []string) *prometheus.GaugeVec { | ||||
help string, labels []string, constLabels prometheus.Labels) *prometheus.GaugeVec { | ||||
|
||||
metricName, err := getAndValid(namespace, subsystem, name, metric, labels) | ||||
metricName, err := getAndValid(namespace, subsystem, name, metric, labels, constLabels) | ||||
if err != nil { | ||||
log.Errorf("[%s] %v", module, err) | ||||
return nil | ||||
} | ||||
|
||||
if m, find := gaugeMap[metricName]; find { | ||||
log.Debugf("[%s] Gauge <%s> already created!", module, metricName) | ||||
return m | ||||
|
@@ -102,15 +106,27 @@ func NewGauge(namespace, subsystem, name, metric string, | |||
Name: metricName, | ||||
Help: help, | ||||
}, | ||||
labels, | ||||
mergeLabels(labels, constLabels), | ||||
) | ||||
|
||||
prometheus.MustRegister(gaugeMap[metricName]) | ||||
|
||||
log.Infof("[%s] Gauge <%s> is created!", module, metricName) | ||||
return gaugeMap[metricName] | ||||
} | ||||
|
||||
func getAndValid(namespace, subsystem, name, metric string, labels []string) (string, error) { | ||||
func mergeLabels(labels []string, constLabels prometheus.Labels) []string { | ||||
l := make([]string, 0, len(labels)+len(constLabels)) | ||||
l = append(l, labels...) | ||||
|
||||
for k, _ := range constLabels { | ||||
l = append(l, k) | ||||
} | ||||
|
||||
return l | ||||
} | ||||
|
||||
func getAndValid(namespace, subsystem, name, metric string, labels []string, constLabels prometheus.Labels) (string, error) { | ||||
metricName := GetName(namespace, subsystem, name, metric) | ||||
if ValidMetricName(metricName) == false { | ||||
return "", fmt.Errorf("Invalid metric name: %s", metricName) | ||||
|
@@ -121,6 +137,19 @@ func getAndValid(namespace, subsystem, name, metric string, labels []string) (st | |||
return "", fmt.Errorf("Invalid label name: %s", l) | ||||
} | ||||
} | ||||
|
||||
for l, _ := range constLabels { | ||||
if !ValidLabelName(l) { | ||||
return "", fmt.Errorf("invalid const label name: %s", l) | ||||
} | ||||
} | ||||
|
||||
for _, l := range labels { | ||||
if _, ok := constLabels[l]; ok { | ||||
return "", fmt.Errorf("label '%s' is duplicated", l) | ||||
} | ||||
} | ||||
|
||||
return metricName, nil | ||||
} | ||||
|
||||
|
@@ -179,3 +208,10 @@ func RemoveInvalidChars(name string) string { | |||
} | ||||
return string(result) | ||||
} | ||||
|
||||
func AddConstLabels(labels prometheus.Labels, constLabels prometheus.Labels) prometheus.Labels { | ||||
for k, v := range constLabels { | ||||
labels[k] = v | ||||
} | ||||
Comment on lines
+212
to
+214
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should check if a label exists both in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated |
||||
return labels | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated