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 {
Name() string
Init(opts ...Option) error
Counter(name string, opts ...Option) Counter
FloatCounter(name string, opts ...Option) FloatCounter
Gauge(name string, fn func() float64, opts ...Option) Gauge
Counter(name string, labels ...string) Counter
FloatCounter(name string, labels ...string) FloatCounter
Gauge(name string, fn func() float64, labels ...string) Gauge
Set(opts ...Option) Meter
Histogram(name string, opts ...Option) Histogram
Summary(name string, opts ...Option) Summary
SummaryExt(name string, window time.Duration, quantiles []float64, opts ...Option) Summary
Histogram(name string, labels ...string) Histogram
Summary(name string, labels ...string) Summary
SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) Summary
Write(w io.Writer, opts ...Option) error
Options() Options
String() string

View File

@ -10,7 +10,7 @@ func TestNoopMeter(t *testing.T) {
t.Fatalf("invalid options parsing: %v", m.Options())
}
cnt := m.Counter("counter", Labels("server", "noop"))
cnt := m.Counter("counter", "server", "noop")
cnt.Inc()
}

View File

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

View File

@ -90,7 +90,7 @@ func Logger(l logger.Logger) Option {
func Labels(ls ...string) Option {
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)
te := time.Since(ts)
lopts := w.opts.lopts
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint))
labels := make([]string, 0, 4)
labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ClientRequestDurationSeconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess))
labels = append(labels, labelStatus, labelSuccess)
} 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
}
@ -148,18 +148,18 @@ func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{},
err := w.Client.Call(ctx, req, rsp, opts...)
te := time.Since(ts)
lopts := w.opts.lopts
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint))
labels := make([]string, 0, 4)
labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ClientRequestDurationSeconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess))
labels = append(labels, labelStatus, labelSuccess)
} 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
}
@ -176,18 +176,18 @@ func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client
stream, err := w.Client.Stream(ctx, req, opts...)
te := time.Since(ts)
lopts := w.opts.lopts
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint))
labels := make([]string, 0, 4)
labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ClientRequestDurationSeconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess))
labels = append(labels, labelStatus, labelSuccess)
} 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
}
@ -199,18 +199,18 @@ func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.
err := w.Client.Publish(ctx, p, opts...)
te := time.Since(ts)
lopts := w.opts.lopts
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint))
labels := make([]string, 0, 4)
labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(PublishMessageDurationSeconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(PublishMessageDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess))
labels = append(labels, labelStatus, labelSuccess)
} 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
}
@ -235,18 +235,18 @@ func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
err := fn(ctx, req, rsp)
te := time.Since(ts)
lopts := w.opts.lopts
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint))
labels := make([]string, 0, 4)
labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ServerRequestDurationSeconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(ServerRequestDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess))
labels = append(labels, labelStatus, labelSuccess)
} 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
}
@ -267,18 +267,18 @@ func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc
err := fn(ctx, msg)
te := time.Since(ts)
lopts := w.opts.lopts
lopts = append(lopts, meter.Labels(labelEndpoint, endpoint))
labels := make([]string, 0, 4)
labels = append(labels, labelEndpoint, endpoint)
w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(SubscribeMessageDurationSeconds, lopts...).Update(float64(te.Seconds()))
w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, labels...).Update(float64(te.Seconds()))
w.opts.Meter.Histogram(SubscribeMessageDurationSeconds, labels...).Update(float64(te.Seconds()))
if err == nil {
lopts = append(lopts, meter.Labels(labelStatus, labelSuccess))
labels = append(labels, labelStatus, labelSuccess)
} 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
}