2021-01-29 13:17:32 +03:00
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/meter"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
)
|
|
|
|
|
2021-02-18 14:35:10 +03:00
|
|
|
var (
|
|
|
|
ClientRequestDurationSeconds = "client_request_duration_seconds"
|
|
|
|
ClientRequestLatencyMicroseconds = "client_request_latency_microseconds"
|
|
|
|
ClientRequestTotal = "client_request_total"
|
|
|
|
ServerRequestDurationSeconds = "server_request_duration_seconds"
|
|
|
|
ServerRequestLatencyMicroseconds = "server_request_latency_microseconds"
|
|
|
|
ServerRequestTotal = "server_request_total"
|
|
|
|
PublishMessageDurationSeconds = "publish_message_duration_seconds"
|
|
|
|
PublishMessageLatencyMicroseconds = "publish_message_latency_microseconds"
|
|
|
|
PublishMessageTotal = "publish_message_total"
|
|
|
|
SubscribeMessageDurationSeconds = "subscribe_message_duration_seconds"
|
|
|
|
SubscribeMessageLatencyMicroseconds = "subscribe_message_latency_microseconds"
|
|
|
|
SubscribeMessageTotal = "subscribe_message_total"
|
|
|
|
|
|
|
|
labelSuccess = "success"
|
|
|
|
labelFailure = "failure"
|
|
|
|
labelStatus = "status"
|
|
|
|
labelEndpoint = "endpoint"
|
|
|
|
)
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
type Options struct {
|
2021-02-18 15:57:42 +03:00
|
|
|
Meter meter.Meter
|
|
|
|
lopts []meter.Option
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
2021-02-18 14:35:10 +03:00
|
|
|
func NewOptions(opts ...Option) Options {
|
2021-03-05 17:40:03 +03:00
|
|
|
options := Options{
|
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
lopts: make([]meter.Option, 0, 5),
|
|
|
|
}
|
2021-02-18 14:35:10 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
return options
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
func ServiceName(name string) Option {
|
|
|
|
return func(o *Options) {
|
2021-02-18 15:57:42 +03:00
|
|
|
o.lopts = append(o.lopts, meter.Label("name", name))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServiceVersion(version string) Option {
|
|
|
|
return func(o *Options) {
|
2021-02-18 15:57:42 +03:00
|
|
|
o.lopts = append(o.lopts, meter.Label("version", version))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServiceID(id string) Option {
|
|
|
|
return func(o *Options) {
|
2021-02-18 15:57:42 +03:00
|
|
|
o.lopts = append(o.lopts, meter.Label("id", id))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type wrapper struct {
|
|
|
|
client.Client
|
2021-03-06 19:45:13 +03:00
|
|
|
callFunc client.CallFunc
|
|
|
|
opts Options
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWrapper(opts ...Option) client.Wrapper {
|
|
|
|
return func(c client.Client) client.Client {
|
|
|
|
handler := &wrapper{
|
2021-02-18 14:35:10 +03:00
|
|
|
opts: NewOptions(opts...),
|
2021-01-29 13:17:32 +03:00
|
|
|
Client: c,
|
|
|
|
}
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCallWrapper(opts ...Option) client.CallWrapper {
|
|
|
|
return func(fn client.CallFunc) client.CallFunc {
|
|
|
|
handler := &wrapper{
|
2021-02-18 14:35:10 +03:00
|
|
|
opts: NewOptions(opts...),
|
2021-01-29 13:17:32 +03:00
|
|
|
callFunc: fn,
|
|
|
|
}
|
|
|
|
return handler.CallFunc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) CallFunc(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
|
|
|
endpoint := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.callFunc(ctx, addr, req, rsp, opts)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
lopts := w.opts.lopts
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
|
|
|
|
|
|
|
|
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
w.opts.Meter.Histogram(ClientRequestDurationSeconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelSuccess))
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelFailure))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-02-18 14:35:10 +03:00
|
|
|
w.opts.Meter.Counter(ClientRequestTotal, lopts...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
endpoint := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
lopts := w.opts.lopts
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
|
|
|
|
|
|
|
|
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
w.opts.Meter.Histogram(ClientRequestDurationSeconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelSuccess))
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelFailure))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-02-18 14:35:10 +03:00
|
|
|
w.opts.Meter.Counter(ClientRequestTotal, lopts...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
|
|
|
endpoint := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
stream, err := w.Client.Stream(ctx, req, opts...)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
lopts := w.opts.lopts
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
|
|
|
|
|
|
|
|
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
w.opts.Meter.Histogram(ClientRequestDurationSeconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelSuccess))
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelFailure))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-02-18 14:35:10 +03:00
|
|
|
w.opts.Meter.Counter(ClientRequestTotal, lopts...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
|
|
|
|
return stream, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
|
|
|
endpoint := p.Topic()
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.Client.Publish(ctx, p, opts...)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
lopts := w.opts.lopts
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
|
|
|
|
|
|
|
|
w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
w.opts.Meter.Histogram(PublishMessageDurationSeconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelSuccess))
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelFailure))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-02-18 14:35:10 +03:00
|
|
|
w.opts.Meter.Counter(PublishMessageTotal, lopts...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
|
|
|
|
handler := &wrapper{
|
2021-02-18 14:35:10 +03:00
|
|
|
opts: NewOptions(opts...),
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
return handler.HandlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
|
|
|
|
return func(ctx context.Context, req server.Request, rsp interface{}) error {
|
|
|
|
endpoint := req.Endpoint()
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := fn(ctx, req, rsp)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
lopts := w.opts.lopts
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
|
|
|
|
|
|
|
|
w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
w.opts.Meter.Histogram(ServerRequestDurationSeconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelSuccess))
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelFailure))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-02-18 14:35:10 +03:00
|
|
|
w.opts.Meter.Counter(ServerRequestTotal, lopts...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
|
|
|
|
handler := &wrapper{
|
2021-02-18 14:35:10 +03:00
|
|
|
opts: NewOptions(opts...),
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
return handler.SubscriberFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc {
|
|
|
|
return func(ctx context.Context, msg server.Message) error {
|
|
|
|
endpoint := msg.Topic()
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := fn(ctx, msg)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
2021-02-18 15:57:42 +03:00
|
|
|
lopts := w.opts.lopts
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelEndpoint, endpoint))
|
|
|
|
|
|
|
|
w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
w.opts.Meter.Histogram(SubscribeMessageDurationSeconds, lopts...).Update(float64(te.Seconds()))
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelSuccess))
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-02-18 14:35:10 +03:00
|
|
|
lopts = append(lopts, meter.Label(labelStatus, labelFailure))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-02-18 14:35:10 +03:00
|
|
|
w.opts.Meter.Counter(SubscribeMessageTotal, lopts...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|