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

hotfix: fix the incorrect format of prometheus label #1223

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 1 addition & 12 deletions metrics/label.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package metrics

import "encoding/json"

// Label hold an map[string]interface{} value that can be set arbitrarily.
type Label interface {
Value() map[string]interface{}
String() string
Mark(map[string]interface{})
}

Expand All @@ -26,8 +23,7 @@ func NewStandardLabel() *StandardLabel {

// StandardLabel is the standard implementation of a Label.
type StandardLabel struct {
value map[string]interface{}
jsonStr string
value map[string]interface{}
}

// Value returns label values.
Expand All @@ -37,12 +33,5 @@ func (l *StandardLabel) Value() map[string]interface{} {

// Mark records the label.
func (l *StandardLabel) Mark(value map[string]interface{}) {
buf, _ := json.Marshal(value)
l.jsonStr = string(buf)
l.value = value
}

// String returns label by JSON format.
func (l *StandardLabel) String() string {
return l.jsonStr
}
10 changes: 8 additions & 2 deletions metrics/prometheus/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ func (c *collector) addResettingTimer(name string, m metrics.ResettingTimer) {
}

func (c *collector) addLabel(name string, m metrics.Label) {
c.writeLabel(mutateKey(name), m.String())
labels := make([]string, 0, len(m.Value()))
for k, v := range m.Value() {
labels = append(labels, fmt.Sprintf(`%s="%s"`, mutateKey(k), fmt.Sprint(v)))
}
c.writeLabel(mutateKey(name), "{"+strings.Join(labels, ", ")+"}")
}

func (c *collector) writeLabel(name string, value interface{}) {
Expand All @@ -126,5 +130,7 @@ func (c *collector) writeSummaryPercentile(name, p string, value interface{}) {
}

func mutateKey(key string) string {
return strings.Replace(key, "/", "_", -1)
key = strings.Replace(key, "/", "_", -1)
key = strings.Replace(key, "-", "_", -1)
return key
}