meter: rework labels

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-03-15 00:44:13 +03:00
parent 723ceb4f32
commit dbbdb24631
4 changed files with 43 additions and 105 deletions

View File

@ -3,6 +3,7 @@ package meter
import ( import (
"io" "io"
"reflect"
"sort" "sort"
"time" "time"
) )
@ -76,66 +77,36 @@ type Summary interface {
UpdateDuration(time.Time) UpdateDuration(time.Time)
} }
// Labels holds the metrics labels with k, v type byKey []string
type Labels struct {
keys []string func (k byKey) Len() int { return len(k) / 2 }
vals []string func (k byKey) Less(i, j int) bool { return k[i*2] < k[j*2] }
func (k byKey) Swap(i, j int) {
k[i*2], k[i*2+1], k[j*2], k[j*2+1] = k[j*2], k[j*2+1], k[i*2], k[i*2+1]
} }
// Append adds labels to label set func Sort(slice *[]string) {
func (ls Labels) Append(nls Labels) Labels { bk := byKey(*slice)
for n := range nls.keys { if bk.Len() <= 1 {
ls.keys = append(ls.keys, nls.keys[n]) return
ls.vals = append(ls.vals, nls.vals[n])
} }
return ls sort.Sort(bk)
v := reflect.ValueOf(slice).Elem()
cnt := 0
key := 0
val := 1
for key < v.Len() {
if len(bk) > key+2 && bk[key] == bk[key+2] {
key += 2
val += 2
continue
} }
v.Index(cnt).Set(v.Index(key))
// Len returns number of labels cnt++
func (ls Labels) Len() int { v.Index(cnt).Set(v.Index(val))
return len(ls.keys) cnt++
key += 2
val += 2
} }
v.SetLen(cnt)
type labels Labels
func (ls labels) Len() int {
return len(ls.keys)
}
func (ls labels) Sort() {
sort.Sort(ls)
}
func (ls labels) Swap(i, j int) {
ls.keys[i], ls.keys[j] = ls.keys[j], ls.keys[i]
ls.vals[i], ls.vals[j] = ls.vals[j], ls.vals[i]
}
func (ls labels) Less(i, j int) bool {
return ls.keys[i] < ls.keys[j]
}
// LabelIter holds the
type LabelIter struct {
labels Labels
cnt int
cur int
}
// Iter returns labels iterator
func (ls Labels) Iter() *LabelIter {
labels(ls).Sort()
return &LabelIter{labels: ls, cnt: len(ls.keys)}
}
// Next advance itarator to new pos
func (iter *LabelIter) Next(k, v *string) bool {
if iter.cur+1 > iter.cnt {
return false
}
*k = iter.labels.keys[iter.cur]
*v = iter.labels.vals[iter.cur]
iter.cur++
return true
} }

View File

@ -10,46 +10,15 @@ func TestNoopMeter(t *testing.T) {
t.Fatalf("invalid options parsing: %v", m.Options()) t.Fatalf("invalid options parsing: %v", m.Options())
} }
cnt := m.Counter("counter", Label("server", "noop")) cnt := m.Counter("counter", Labels("server", "noop"))
cnt.Inc() cnt.Inc()
} }
func TestLabelsAppend(t *testing.T) { func TestLabelsSort(t *testing.T) {
var ls Labels ls := []string{"server", "http", "register", "mdns", "broker", "broker1", "broker", "broker2", "server", "tcp"}
ls.keys = []string{"type", "server"} Sort(&ls)
ls.vals = []string{"noop", "http"}
var nls Labels if ls[0] != "broker" || ls[1] != "broker2" {
nls.keys = []string{"register"} t.Fatalf("sort error: %v", ls)
nls.vals = []string{"gossip"}
ls = ls.Append(nls)
//ls.Sort()
if ls.keys[0] != "type" || ls.vals[0] != "noop" {
t.Fatalf("append error: %v", ls)
}
}
func TestIterator(t *testing.T) {
options := NewOptions(
Label("name", "svc1"),
Label("version", "0.0.1"),
Label("id", "12345"),
Label("type", "noop"),
Label("server", "http"),
Label("register", "gossip"),
Label("aa", "kk"),
Label("zz", "kk"),
)
iter := options.Labels.Iter()
var k, v string
cnt := 0
for iter.Next(&k, &v) {
if cnt == 4 && (k != "server" || v != "http") {
t.Fatalf("iter error: %s != %s || %s != %s", k, "server", v, "http")
}
cnt++
} }
} }

View File

@ -107,7 +107,7 @@ func (r *noopMeter) String() string {
} }
type noopCounter struct { type noopCounter struct {
labels Labels labels []string
} }
func (r *noopCounter) Add(int) { func (r *noopCounter) Add(int) {
@ -131,7 +131,7 @@ func (r *noopCounter) Set(uint64) {
} }
type noopFloatCounter struct { type noopFloatCounter struct {
labels Labels labels []string
} }
func (r *noopFloatCounter) Add(float64) { func (r *noopFloatCounter) Add(float64) {
@ -151,7 +151,7 @@ func (r *noopFloatCounter) Sub(float64) {
} }
type noopGauge struct { type noopGauge struct {
labels Labels labels []string
} }
func (r *noopGauge) Get() float64 { func (r *noopGauge) Get() float64 {
@ -159,7 +159,7 @@ func (r *noopGauge) Get() float64 {
} }
type noopSummary struct { type noopSummary struct {
labels Labels labels []string
} }
func (r *noopSummary) Update(float64) { func (r *noopSummary) Update(float64) {
@ -171,7 +171,7 @@ func (r *noopSummary) UpdateDuration(time.Time) {
} }
type noopHistogram struct { type noopHistogram struct {
labels Labels labels []string
} }
func (r *noopHistogram) Reset() { func (r *noopHistogram) Reset() {

View File

@ -26,7 +26,7 @@ type Options struct {
// LabelPrefix holds the prefix for all labels // LabelPrefix holds the prefix for all labels
LabelPrefix string LabelPrefix string
// Labels holds the default labels // Labels holds the default labels
Labels Labels Labels []string
// WriteProcessMetrics flag to write process metrics // WriteProcessMetrics flag to write process metrics
WriteProcessMetrics bool WriteProcessMetrics bool
// WriteFDMetrics flag to write fd metrics // WriteFDMetrics flag to write fd metrics
@ -88,11 +88,9 @@ func Logger(l logger.Logger) Option {
} }
} }
// Label sets the label func Labels(ls ...string) Option {
func Label(key, val string) Option {
return func(o *Options) { return func(o *Options) {
o.Labels.keys = append(o.Labels.keys, key) o.Labels = ls
o.Labels.vals = append(o.Labels.vals, val)
} }
} }