Skip to content
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

Merged
merged 20 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
reformat the code
  • Loading branch information
qdongxu committed Aug 19, 2023
commit 149c05210ec568ad8ec7b773ce12799e45455eb9
7 changes: 3 additions & 4 deletions conf/constlables.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ import (
)

// MergeConstLabels merge const labels from all probers.
//
// Prometheus requires all metric of the same name have the same set of labels in a collector
// 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() {
for k := range p.LabelMap() {
constLabels[k] = true
}
}
Expand All @@ -44,7 +43,7 @@ func buildConstLabels(p probe.Prober, constLabels map[string]bool) {
p.SetLabelMap(ls)
}

for k, _ := range constLabels {
for k := range constLabels {
if _, ok := ls[k]; !ok {
ls[k] = ""
}
Expand Down
7 changes: 3 additions & 4 deletions metric/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func NewCounter(namespace, subsystem, name, metric string,
mergeLabels(labels, constLabels),
)

// registries[len(registries)-1].MustRegister(m)
prometheus.MustRegister(counterMap[metricName])
log.Infof("[%s] Counter <%s> is created!", module, metricName)
return counterMap[metricName]
Expand Down Expand Up @@ -129,16 +128,16 @@ func mergeLabels(labels []string, constLabels prometheus.Labels) []string {
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)
return "", fmt.Errorf("invalid metric name: %s", metricName)
}

for _, l := range labels {
if ValidLabelName(l) == false {
return "", fmt.Errorf("Invalid label name: %s", l)
return "", fmt.Errorf("invalid label name: %s", l)
}
}

for l, _ := range constLabels {
for l := range constLabels {
if !ValidLabelName(l) {
return "", fmt.Errorf("invalid const label name: %s", l)
}
Expand Down
Loading