meter: fix labels

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2021-04-26 23:13:36 +03:00
parent 6dfdff7fd8
commit 3bdfdd8fd2
5 changed files with 62 additions and 86 deletions

View File

@ -29,13 +29,13 @@ var (
type Meter interface { type Meter interface {
Name() string Name() string
Init(opts ...Option) error Init(opts ...Option) error
Counter(name string, opts ...Option) Counter Counter(name string, labels ...string) Counter
FloatCounter(name string, opts ...Option) FloatCounter FloatCounter(name string, labels ...string) FloatCounter
Gauge(name string, fn func() float64, opts ...Option) Gauge Gauge(name string, fn func() float64, labels ...string) Gauge
Set(opts ...Option) Meter Set(opts ...Option) Meter
Histogram(name string, opts ...Option) Histogram Histogram(name string, labels ...string) Histogram
Summary(name string, opts ...Option) Summary Summary(name string, labels ...string) Summary
SummaryExt(name string, window time.Duration, quantiles []float64, opts ...Option) Summary SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) Summary
Write(w io.Writer, opts ...Option) error Write(w io.Writer, opts ...Option) error
Options() Options Options() Options
String() string String() string

View File

@ -10,7 +10,7 @@ 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", Labels("server", "noop")) cnt := m.Counter("counter", "server", "noop")
cnt.Inc() cnt.Inc()
} }

View File

@ -28,57 +28,33 @@ func (r *noopMeter) Init(opts ...Option) error {
} }
// Counter implements the Meter interface // Counter implements the Meter interface
func (r *noopMeter) Counter(name string, opts ...Option) Counter { func (r *noopMeter) Counter(name string, labels ...string) Counter {
options := Options{} return &noopCounter{labels: labels}
for _, o := range opts {
o(&options)
}
return &noopCounter{labels: options.Labels}
} }
// FloatCounter implements the Meter interface // FloatCounter implements the Meter interface
func (r *noopMeter) FloatCounter(name string, opts ...Option) FloatCounter { func (r *noopMeter) FloatCounter(name string, labels ...string) FloatCounter {
options := Options{} return &noopFloatCounter{labels: labels}
for _, o := range opts {
o(&options)
}
return &noopFloatCounter{labels: options.Labels}
} }
// Gauge implements the Meter interface // Gauge implements the Meter interface
func (r *noopMeter) Gauge(name string, f func() float64, opts ...Option) Gauge { func (r *noopMeter) Gauge(name string, f func() float64, labels ...string) Gauge {
options := Options{} return &noopGauge{labels: labels}
for _, o := range opts {
o(&options)
}
return &noopGauge{labels: options.Labels}
} }
// Summary implements the Meter interface // Summary implements the Meter interface
func (r *noopMeter) Summary(name string, opts ...Option) Summary { func (r *noopMeter) Summary(name string, labels ...string) Summary {
options := Options{} return &noopSummary{labels: labels}
for _, o := range opts {
o(&options)
}
return &noopSummary{labels: options.Labels}
} }
// SummaryExt implements the Meter interface // SummaryExt implements the Meter interface
func (r *noopMeter) SummaryExt(name string, window time.Duration, quantiles []float64, opts ...Option) Summary { func (r *noopMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) Summary {
options := Options{} return &noopSummary{labels: labels}
for _, o := range opts {
o(&options)
}
return &noopSummary{labels: options.Labels}
} }
// Histogram implements the Meter interface // Histogram implements the Meter interface
func (r *noopMeter) Histogram(name string, opts ...Option) Histogram { func (r *noopMeter) Histogram(name string, labels ...string) Histogram {
options := Options{} return &noopHistogram{labels: labels}
for _, o := range opts {
o(&options)
}
return &noopHistogram{labels: options.Labels}
} }
// Set implements the Meter interface // Set implements the Meter interface

View File

@ -90,7 +90,7 @@ func Logger(l logger.Logger) Option {
func Labels(ls ...string) Option { func Labels(ls ...string) Option {
return func(o *Options) { return func(o *Options) {
o.Labels = ls o.Labels = append(o.Labels, ls...)
} }
} }

View File

@ -120,18 +120,18 @@ 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 := w.opts.lopts labels := make([]string, 0, 4)
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint)) labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ClientRequestDurationSeconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil { if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess)) labels = append(labels, labelStatus, labelSuccess)
} else { } else {
lopts = append(lopts, meter.Labels(labelStatus, labelFailure)) labels = append(labels, labelStatus, labelFailure)
} }
w.opts.Meter.Counter(ClientRequestTotal, lopts...).Inc() w.opts.Meter.Counter(ClientRequestTotal, labels...).Inc()
return err return err
} }
@ -148,18 +148,18 @@ 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 := w.opts.lopts labels := make([]string, 0, 4)
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint)) labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ClientRequestDurationSeconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil { if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess)) labels = append(labels, labelStatus, labelSuccess)
} else { } else {
lopts = append(lopts, meter.Labels(labelStatus, labelFailure)) labels = append(labels, labelStatus, labelFailure)
} }
w.opts.Meter.Counter(ClientRequestTotal, lopts...).Inc() w.opts.Meter.Counter(ClientRequestTotal, labels...).Inc()
return err return err
} }
@ -176,18 +176,18 @@ 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 := w.opts.lopts labels := make([]string, 0, 4)
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint)) labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ClientRequestDurationSeconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil { if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess)) labels = append(labels, labelStatus, labelSuccess)
} else { } else {
lopts = append(lopts, meter.Labels(labelStatus, labelFailure)) labels = append(labels, labelStatus, labelFailure)
} }
w.opts.Meter.Counter(ClientRequestTotal, lopts...).Inc() w.opts.Meter.Counter(ClientRequestTotal, labels...).Inc()
return stream, err return stream, err
} }
@ -199,18 +199,18 @@ 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 := w.opts.lopts labels := make([]string, 0, 4)
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint)) labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(PublishMessageDurationSeconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Histogram(PublishMessageDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil { if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess)) labels = append(labels, labelStatus, labelSuccess)
} else { } else {
lopts = append(lopts, meter.Labels(labelStatus, labelFailure)) labels = append(labels, labelStatus, labelFailure)
} }
w.opts.Meter.Counter(PublishMessageTotal, lopts...).Inc() w.opts.Meter.Counter(PublishMessageTotal, labels...).Inc()
return err return err
} }
@ -235,18 +235,18 @@ 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 := w.opts.lopts labels := make([]string, 0, 4)
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint)) labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ServerRequestDurationSeconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Histogram(ServerRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil { if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess)) labels = append(labels, labelStatus, labelSuccess)
} else { } else {
lopts = append(lopts, meter.Labels(labelStatus, labelFailure)) labels = append(labels, labelStatus, labelFailure)
} }
w.opts.Meter.Counter(ServerRequestTotal, lopts...).Inc() w.opts.Meter.Counter(ServerRequestTotal, labels...).Inc()
return err return err
} }
@ -267,18 +267,18 @@ 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 := w.opts.lopts labels := make([]string, 0, 4)
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint)) labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(SubscribeMessageDurationSeconds, lopts...).Update(float64(te.Seconds())) w.opts.Meter.Histogram(SubscribeMessageDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil { if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess)) labels = append(labels, labelStatus, labelSuccess)
} else { } else {
lopts = append(lopts, meter.Labels(labelStatus, labelFailure)) labels = append(labels, labelStatus, labelFailure)
} }
w.opts.Meter.Counter(SubscribeMessageTotal, lopts...).Inc() w.opts.Meter.Counter(SubscribeMessageTotal, labels...).Inc()
return err return err
} }