From 05a9c3cd0b1a53e1e9a5beede7bdd37680249815 Mon Sep 17 00:00:00 2001 From: "Matt T. Proud" Date: Thu, 21 Mar 2013 12:07:13 +0100 Subject: [PATCH] Improve the base label representation in /status. The base label representation under /status needs improvement to enhance readability; namely, add sorting and make the label representation concise. --- model/metric.go | 48 +++++++++++++++++++++++++++++++++++++++ web/templates/status.html | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/model/metric.go b/model/metric.go index 13275937e9..ca4c7f0d93 100644 --- a/model/metric.go +++ b/model/metric.go @@ -14,6 +14,7 @@ package model import ( + "bytes" "crypto/md5" "encoding/hex" "fmt" @@ -44,6 +45,53 @@ type LabelValue string // match. type LabelSet map[LabelName]LabelValue +func (l LabelSet) String() string { + var ( + buffer bytes.Buffer + labels LabelNames + labelCount int = len(l) + ) + + for name := range l { + labels = append(labels, name) + } + + sort.Sort(labels) + + fmt.Fprintf(&buffer, "{") + for i := 0; i < labelCount; i++ { + var ( + label = labels[i] + value = l[label] + ) + + switch i { + case labelCount - 1: + fmt.Fprintf(&buffer, "%s=%s", label, value) + default: + fmt.Fprintf(&buffer, "%s=%s, ", label, value) + } + } + + fmt.Fprintf(&buffer, "}") + + return buffer.String() +} + +type LabelNames []LabelName + +func (l LabelNames) Len() int { + return len(l) +} + +func (l LabelNames) Less(i, j int) bool { + return l[i] < l[j] +} + +func (l LabelNames) Swap(i, j int) { + l[i], l[j] = l[j], l[i] +} + // A Metric is similar to a LabelSet, but the key difference is that a Metric is // a singleton and refers to one and only one stream of samples. type Metric map[LabelName]LabelValue diff --git a/web/templates/status.html b/web/templates/status.html index 7d3bf90720..62712d5c24 100644 --- a/web/templates/status.html +++ b/web/templates/status.html @@ -33,7 +33,7 @@