meter: fix internal labels sorting

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-02-18 15:57:42 +03:00
parent 47e75c31c7
commit 7eb6d030dc
4 changed files with 50 additions and 44 deletions

View File

@ -175,7 +175,7 @@ func (n *noopClient) Call(ctx context.Context, req Request, rsp interface{}, opt
} }
func (n *noopClient) NewRequest(service, endpoint string, req interface{}, opts ...RequestOption) Request { func (n *noopClient) NewRequest(service, endpoint string, req interface{}, opts ...RequestOption) Request {
return &noopRequest{} return &noopRequest{service: service, endpoint: endpoint}
} }
func (n *noopClient) NewMessage(topic string, msg interface{}, opts ...MessageOption) Message { func (n *noopClient) NewMessage(topic string, msg interface{}, opts ...MessageOption) Message {

View File

@ -82,25 +82,6 @@ type Labels struct {
vals []string vals []string
} }
type labels Labels
func (ls labels) sort() {
sort.Sort(ls)
}
func (ls labels) Len() int {
return len(ls.keys)
}
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.vals[i] < ls.vals[j]
}
// Append adds labels to label set // Append adds labels to label set
func (ls Labels) Append(nls Labels) Labels { func (ls Labels) Append(nls Labels) Labels {
for n := range nls.keys { for n := range nls.keys {
@ -110,10 +91,30 @@ func (ls Labels) Append(nls Labels) Labels {
return ls return ls
} }
// Len returns number of labels
func (ls Labels) Len() int { func (ls Labels) Len() int {
return len(ls.keys) return len(ls.keys)
} }
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 // LabelIter holds the
type LabelIter struct { type LabelIter struct {
labels Labels labels Labels
@ -123,7 +124,7 @@ type LabelIter struct {
// Iter returns labels iterator // Iter returns labels iterator
func (ls Labels) Iter() *LabelIter { func (ls Labels) Iter() *LabelIter {
labels(ls).sort() labels(ls).Sort()
return &LabelIter{labels: ls, cnt: len(ls.keys)} return &LabelIter{labels: ls, cnt: len(ls.keys)}
} }

View File

@ -5,12 +5,12 @@ import (
) )
func TestNoopMeter(t *testing.T) { func TestNoopMeter(t *testing.T) {
meter := NewMeter(Path("/noop")) m := NewMeter(Path("/noop"))
if "/noop" != meter.Options().Path { if "/noop" != m.Options().Path {
t.Fatalf("invalid options parsing: %v", meter.Options()) t.Fatalf("invalid options parsing: %v", m.Options())
} }
cnt := meter.Counter("counter", Label("server", "noop")) cnt := m.Counter("counter", Label("server", "noop"))
cnt.Inc() cnt.Inc()
} }
@ -32,15 +32,22 @@ func TestLabelsAppend(t *testing.T) {
} }
func TestIterator(t *testing.T) { func TestIterator(t *testing.T) {
var ls Labels options := NewOptions(
ls.keys = []string{"type", "server", "register"} Label("name", "svc1"),
ls.vals = []string{"noop", "http", "gossip"} Label("version", "0.0.1"),
Label("id", "12345"),
Label("type", "noop"),
Label("server", "http"),
Label("register", "gossip"),
Label("aa", "kk"),
Label("zz", "kk"),
)
iter := ls.Iter() iter := options.Labels.Iter()
var k, v string var k, v string
cnt := 0 cnt := 0
for iter.Next(&k, &v) { for iter.Next(&k, &v) {
if cnt == 1 && (k != "server" || v != "http") { if cnt == 4 && (k != "server" || v != "http") {
t.Fatalf("iter error: %s != %s || %s != %s", k, "server", v, "http") t.Fatalf("iter error: %s != %s || %s != %s", k, "server", v, "http")
} }
cnt++ cnt++

View File

@ -31,16 +31,14 @@ var (
) )
type Options struct { type Options struct {
Meter meter.Meter Meter meter.Meter
Name string lopts []meter.Option
Version string
ID string
} }
type Option func(*Options) type Option func(*Options)
func NewOptions(opts ...Option) Options { func NewOptions(opts ...Option) Options {
options := Options{} options := Options{lopts: make([]meter.Option, 0, 5)}
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)
} }
@ -49,19 +47,19 @@ func NewOptions(opts ...Option) Options {
func ServiceName(name string) Option { func ServiceName(name string) Option {
return func(o *Options) { return func(o *Options) {
o.Name = name o.lopts = append(o.lopts, meter.Label("name", name))
} }
} }
func ServiceVersion(version string) Option { func ServiceVersion(version string) Option {
return func(o *Options) { return func(o *Options) {
o.Version = version o.lopts = append(o.lopts, meter.Label("version", version))
} }
} }
func ServiceID(id string) Option { func ServiceID(id string) Option {
return func(o *Options) { return func(o *Options) {
o.ID = id o.lopts = append(o.lopts, meter.Label("id", id))
} }
} }
@ -104,7 +102,7 @@ func (w *wrapper) CallFunc(ctx context.Context, addr string, req client.Request,
err := w.callFunc(ctx, addr, req, rsp, opts) err := w.callFunc(ctx, addr, req, rsp, opts)
te := time.Since(ts) te := time.Since(ts)
lopts := make([]meter.Option, 0, 2) lopts := w.opts.lopts
lopts = append(lopts, meter.Label(labelEndpoint, endpoint)) lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
@ -127,7 +125,7 @@ func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{},
err := w.Client.Call(ctx, req, rsp, opts...) err := w.Client.Call(ctx, req, rsp, opts...)
te := time.Since(ts) te := time.Since(ts)
lopts := make([]meter.Option, 0, 2) lopts := w.opts.lopts
lopts = append(lopts, meter.Label(labelEndpoint, endpoint)) lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
@ -150,7 +148,7 @@ func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client
stream, err := w.Client.Stream(ctx, req, opts...) stream, err := w.Client.Stream(ctx, req, opts...)
te := time.Since(ts) te := time.Since(ts)
lopts := make([]meter.Option, 0, 2) lopts := w.opts.lopts
lopts = append(lopts, meter.Label(labelEndpoint, endpoint)) lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
@ -173,7 +171,7 @@ func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.
err := w.Client.Publish(ctx, p, opts...) err := w.Client.Publish(ctx, p, opts...)
te := time.Since(ts) te := time.Since(ts)
lopts := make([]meter.Option, 0, 2) lopts := w.opts.lopts
lopts = append(lopts, meter.Label(labelEndpoint, endpoint)) lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
@ -204,7 +202,7 @@ func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
err := fn(ctx, req, rsp) err := fn(ctx, req, rsp)
te := time.Since(ts) te := time.Since(ts)
lopts := make([]meter.Option, 0, 2) lopts := w.opts.lopts
lopts = append(lopts, meter.Label(labelEndpoint, endpoint)) lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
@ -236,7 +234,7 @@ func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc
err := fn(ctx, msg) err := fn(ctx, msg)
te := time.Since(ts) te := time.Since(ts)
lopts := make([]meter.Option, 0, 2) lopts := w.opts.lopts
lopts = append(lopts, meter.Label(labelEndpoint, endpoint)) lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))