From ecc12f6493665f5b2619f57e5d5f4d331159e529 Mon Sep 17 00:00:00 2001 From: j75689 Date: Thu, 1 Dec 2022 16:54:10 +0800 Subject: [PATCH] metrics: fix the label format of prometheus server --- metrics/label.go | 13 +------------ metrics/prometheus/collector.go | 6 +++++- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/metrics/label.go b/metrics/label.go index 6c6b02ed7..f59c149d0 100644 --- a/metrics/label.go +++ b/metrics/label.go @@ -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{}) } @@ -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. @@ -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 -} diff --git a/metrics/prometheus/collector.go b/metrics/prometheus/collector.go index ea85abc1b..1921a0c37 100644 --- a/metrics/prometheus/collector.go +++ b/metrics/prometheus/collector.go @@ -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{}) {