2021-10-02 19:55:07 +03:00
|
|
|
package wrapper // import "go.unistack.org/micro/v3/meter/wrapper"
|
2021-01-29 13:17:32 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-10-02 19:55:07 +03:00
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2021-01-29 13:17:32 +03:00
|
|
|
)
|
|
|
|
|
2021-02-18 14:35:10 +03:00
|
|
|
var (
|
2021-05-10 17:59:40 +03:00
|
|
|
ClientRequestDurationSeconds = "client_request_duration_seconds"
|
|
|
|
ClientRequestLatencyMicroseconds = "client_request_latency_microseconds"
|
|
|
|
ClientRequestTotal = "client_request_total"
|
|
|
|
ClientRequestInflight = "client_request_inflight"
|
|
|
|
|
|
|
|
ServerRequestDurationSeconds = "server_request_duration_seconds"
|
|
|
|
ServerRequestLatencyMicroseconds = "server_request_latency_microseconds"
|
|
|
|
ServerRequestTotal = "server_request_total"
|
|
|
|
ServerRequestInflight = "server_request_inflight"
|
|
|
|
|
|
|
|
PublishMessageDurationSeconds = "publish_message_duration_seconds"
|
|
|
|
PublishMessageLatencyMicroseconds = "publish_message_latency_microseconds"
|
|
|
|
PublishMessageTotal = "publish_message_total"
|
|
|
|
PublishMessageInflight = "publish_message_inflight"
|
|
|
|
|
2021-02-18 14:35:10 +03:00
|
|
|
SubscribeMessageDurationSeconds = "subscribe_message_duration_seconds"
|
|
|
|
SubscribeMessageLatencyMicroseconds = "subscribe_message_latency_microseconds"
|
|
|
|
SubscribeMessageTotal = "subscribe_message_total"
|
2021-05-10 17:59:40 +03:00
|
|
|
SubscribeMessageInflight = "subscribe_message_inflight"
|
2021-02-18 14:35:10 +03:00
|
|
|
|
|
|
|
labelSuccess = "success"
|
|
|
|
labelFailure = "failure"
|
|
|
|
labelStatus = "status"
|
|
|
|
labelEndpoint = "endpoint"
|
2021-03-25 00:06:38 +03:00
|
|
|
|
|
|
|
// DefaultSkipEndpoints contains list of endpoints that not evaluted by wrapper
|
|
|
|
DefaultSkipEndpoints = []string{"Meter.Metrics"}
|
2021-02-18 14:35:10 +03:00
|
|
|
)
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
type Options struct {
|
2021-03-25 23:30:38 +03:00
|
|
|
Meter meter.Meter
|
|
|
|
lopts []meter.Option
|
|
|
|
SkipEndpoints []string
|
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{
|
2021-03-25 23:30:38 +03:00
|
|
|
Meter: meter.DefaultMeter,
|
|
|
|
lopts: make([]meter.Option, 0, 5),
|
|
|
|
SkipEndpoints: DefaultSkipEndpoints,
|
2021-03-05 17:40:03 +03:00
|
|
|
}
|
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-03-15 00:49:26 +03:00
|
|
|
o.lopts = append(o.lopts, meter.Labels("name", name))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServiceVersion(version string) Option {
|
|
|
|
return func(o *Options) {
|
2021-03-15 00:49:26 +03:00
|
|
|
o.lopts = append(o.lopts, meter.Labels("version", version))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ServiceID(id string) Option {
|
|
|
|
return func(o *Options) {
|
2021-03-15 00:49:26 +03:00
|
|
|
o.lopts = append(o.lopts, meter.Labels("id", id))
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Meter(m meter.Meter) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Meter = m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:30:38 +03:00
|
|
|
func SkipEndoints(eps ...string) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.SkipEndpoints = append(o.SkipEndpoints, eps...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
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())
|
2021-03-25 23:30:38 +03:00
|
|
|
for _, ep := range w.opts.SkipEndpoints {
|
2021-03-25 00:06:38 +03:00
|
|
|
if ep == endpoint {
|
|
|
|
return w.callFunc(ctx, addr, req, rsp, opts)
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
|
2021-04-26 23:13:36 +03:00
|
|
|
labels := make([]string, 0, 4)
|
|
|
|
labels = append(labels, labelEndpoint, endpoint)
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-05-10 17:59:40 +03:00
|
|
|
w.opts.Meter.Counter(ClientRequestInflight, labels...).Inc()
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.callFunc(ctx, addr, req, rsp, opts)
|
|
|
|
te := time.Since(ts)
|
|
|
|
w.opts.Meter.Counter(ClientRequestInflight, labels...).Dec()
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(te.Seconds())
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelSuccess)
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelFailure)
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-04-26 23:13:36 +03:00
|
|
|
w.opts.Meter.Counter(ClientRequestTotal, labels...).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())
|
2021-03-25 23:30:38 +03:00
|
|
|
for _, ep := range w.opts.SkipEndpoints {
|
2021-03-25 00:06:38 +03:00
|
|
|
if ep == endpoint {
|
|
|
|
return w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
|
2021-05-10 17:59:40 +03:00
|
|
|
labels := make([]string, 0, 4)
|
|
|
|
labels = append(labels, labelEndpoint, endpoint)
|
|
|
|
|
|
|
|
w.opts.Meter.Counter(ClientRequestInflight, labels...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
ts := time.Now()
|
|
|
|
err := w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
te := time.Since(ts)
|
2021-05-10 17:59:40 +03:00
|
|
|
w.opts.Meter.Counter(ClientRequestInflight, labels...).Dec()
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(te.Seconds())
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelSuccess)
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelFailure)
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-04-26 23:13:36 +03:00
|
|
|
w.opts.Meter.Counter(ClientRequestTotal, labels...).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())
|
2021-03-25 23:30:38 +03:00
|
|
|
for _, ep := range w.opts.SkipEndpoints {
|
2021-03-25 00:06:38 +03:00
|
|
|
if ep == endpoint {
|
|
|
|
return w.Client.Stream(ctx, req, opts...)
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
|
2021-05-10 17:59:40 +03:00
|
|
|
labels := make([]string, 0, 4)
|
|
|
|
labels = append(labels, labelEndpoint, endpoint)
|
|
|
|
|
|
|
|
w.opts.Meter.Counter(ClientRequestInflight, labels...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
ts := time.Now()
|
|
|
|
stream, err := w.Client.Stream(ctx, req, opts...)
|
|
|
|
te := time.Since(ts)
|
2021-05-10 17:59:40 +03:00
|
|
|
w.opts.Meter.Counter(ClientRequestInflight, labels...).Dec()
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
w.opts.Meter.Summary(ClientRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
w.opts.Meter.Histogram(ClientRequestDurationSeconds, labels...).Update(te.Seconds())
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelSuccess)
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelFailure)
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-04-26 23:13:36 +03:00
|
|
|
w.opts.Meter.Counter(ClientRequestTotal, labels...).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()
|
|
|
|
|
2021-05-10 17:59:40 +03:00
|
|
|
labels := make([]string, 0, 4)
|
|
|
|
labels = append(labels, labelEndpoint, endpoint)
|
|
|
|
|
|
|
|
w.opts.Meter.Counter(PublishMessageInflight, labels...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
ts := time.Now()
|
|
|
|
err := w.Client.Publish(ctx, p, opts...)
|
|
|
|
te := time.Since(ts)
|
2021-05-10 17:59:40 +03:00
|
|
|
w.opts.Meter.Counter(PublishMessageInflight, labels...).Dec()
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
w.opts.Meter.Summary(PublishMessageLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
w.opts.Meter.Histogram(PublishMessageDurationSeconds, labels...).Update(te.Seconds())
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelSuccess)
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelFailure)
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-04-26 23:13:36 +03:00
|
|
|
w.opts.Meter.Counter(PublishMessageTotal, labels...).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()
|
2021-03-25 23:30:38 +03:00
|
|
|
for _, ep := range w.opts.SkipEndpoints {
|
2021-03-25 00:06:38 +03:00
|
|
|
if ep == endpoint {
|
|
|
|
return fn(ctx, req, rsp)
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 13:17:32 +03:00
|
|
|
|
2021-05-10 17:59:40 +03:00
|
|
|
labels := make([]string, 0, 4)
|
|
|
|
labels = append(labels, labelEndpoint, endpoint)
|
|
|
|
|
|
|
|
w.opts.Meter.Counter(ServerRequestInflight, labels...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
ts := time.Now()
|
|
|
|
err := fn(ctx, req, rsp)
|
|
|
|
te := time.Since(ts)
|
2021-05-10 17:59:40 +03:00
|
|
|
w.opts.Meter.Counter(ServerRequestInflight, labels...).Dec()
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
w.opts.Meter.Summary(ServerRequestLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
w.opts.Meter.Histogram(ServerRequestDurationSeconds, labels...).Update(te.Seconds())
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelSuccess)
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelFailure)
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-04-26 23:13:36 +03:00
|
|
|
w.opts.Meter.Counter(ServerRequestTotal, labels...).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()
|
|
|
|
|
2021-05-10 17:59:40 +03:00
|
|
|
labels := make([]string, 0, 4)
|
|
|
|
labels = append(labels, labelEndpoint, endpoint)
|
|
|
|
|
|
|
|
w.opts.Meter.Counter(SubscribeMessageInflight, labels...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
ts := time.Now()
|
|
|
|
err := fn(ctx, msg)
|
|
|
|
te := time.Since(ts)
|
2021-05-10 17:59:40 +03:00
|
|
|
w.opts.Meter.Counter(SubscribeMessageInflight, labels...).Dec()
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
w.opts.Meter.Summary(SubscribeMessageLatencyMicroseconds, labels...).Update(te.Seconds())
|
|
|
|
w.opts.Meter.Histogram(SubscribeMessageDurationSeconds, labels...).Update(te.Seconds())
|
2021-02-18 14:35:10 +03:00
|
|
|
|
2021-01-29 13:17:32 +03:00
|
|
|
if err == nil {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelSuccess)
|
2021-01-29 13:17:32 +03:00
|
|
|
} else {
|
2021-04-26 23:13:36 +03:00
|
|
|
labels = append(labels, labelStatus, labelFailure)
|
2021-01-29 13:17:32 +03:00
|
|
|
}
|
2021-04-26 23:13:36 +03:00
|
|
|
w.opts.Meter.Counter(SubscribeMessageTotal, labels...).Inc()
|
2021-01-29 13:17:32 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|