2019-01-25 17:27:27 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-26 12:33:51 +03:00
|
|
|
"fmt"
|
2020-06-10 16:04:12 +03:00
|
|
|
"sync"
|
2019-01-26 12:33:51 +03:00
|
|
|
|
2019-01-25 17:27:27 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-01-19 16:51:58 +03:00
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
2019-01-25 17:27:27 +03:00
|
|
|
)
|
|
|
|
|
2019-01-26 12:33:51 +03:00
|
|
|
var (
|
2020-04-17 02:45:40 +03:00
|
|
|
// default metric prefix
|
2020-06-10 16:04:12 +03:00
|
|
|
DefaultMetricPrefix = "micro_"
|
2020-04-17 02:45:40 +03:00
|
|
|
// default label prefix
|
2020-06-10 16:04:12 +03:00
|
|
|
DefaultLabelPrefix = "micro_"
|
2019-01-26 12:33:51 +03:00
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
clientOpsCounter *prometheus.CounterVec
|
|
|
|
clientTimeCounterSummary *prometheus.SummaryVec
|
|
|
|
clientTimeCounterHistogram *prometheus.HistogramVec
|
|
|
|
|
|
|
|
serverOpsCounter *prometheus.CounterVec
|
|
|
|
serverTimeCounterSummary *prometheus.SummaryVec
|
|
|
|
serverTimeCounterHistogram *prometheus.HistogramVec
|
|
|
|
|
|
|
|
publishOpsCounter *prometheus.CounterVec
|
|
|
|
publishTimeCounterSummary *prometheus.SummaryVec
|
|
|
|
publishTimeCounterHistogram *prometheus.HistogramVec
|
|
|
|
|
|
|
|
subscribeOpsCounter *prometheus.CounterVec
|
|
|
|
subscribeTimeCounterSummary *prometheus.SummaryVec
|
|
|
|
subscribeTimeCounterHistogram *prometheus.HistogramVec
|
2020-06-10 16:04:12 +03:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
ID string
|
2021-01-19 16:51:58 +03:00
|
|
|
Context context.Context
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
type Option func(*Options)
|
2019-01-26 12:33:51 +03:00
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
func Context(ctx context.Context) Option {
|
|
|
|
return func(o *Options) {
|
|
|
|
o.Context = ctx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
func ServiceName(name string) Option {
|
2021-01-19 16:51:58 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Name = name
|
2019-01-26 12:33:51 +03:00
|
|
|
}
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2019-01-26 12:33:51 +03:00
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
func ServiceVersion(version string) Option {
|
2021-01-19 16:51:58 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.Version = version
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
func ServiceID(id string) Option {
|
2021-01-19 16:51:58 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
o.ID = id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerServerMetrics(ctx context.Context) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if serverOpsCounter == nil {
|
|
|
|
serverOpsCounter = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: fmt.Sprintf("%sserver_request_total", DefaultMetricPrefix),
|
|
|
|
Help: "Requests processed, partitioned by endpoint and status",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "status"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverTimeCounterSummary == nil {
|
|
|
|
serverTimeCounterSummary = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Name: fmt.Sprintf("%sserver_latency_microseconds", DefaultMetricPrefix),
|
|
|
|
Help: "Request latencies in microseconds, partitioned by endpoint",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if serverTimeCounterHistogram == nil {
|
|
|
|
serverTimeCounterHistogram = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Name: fmt.Sprintf("%sserver_request_duration_seconds", DefaultMetricPrefix),
|
|
|
|
Help: "Request time in seconds, partitioned by endpoint",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, collector := range []prometheus.Collector{serverOpsCounter, serverTimeCounterSummary, serverTimeCounterHistogram} {
|
|
|
|
if err := prometheus.DefaultRegisterer.Register(collector); err != nil {
|
|
|
|
// if already registered, skip fatal
|
|
|
|
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
|
|
|
|
logger.Fatal(ctx, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
func registerPublishMetrics(ctx context.Context) {
|
2020-06-10 16:04:12 +03:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
if publishOpsCounter == nil {
|
|
|
|
publishOpsCounter = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: fmt.Sprintf("%spublish_message_total", DefaultMetricPrefix),
|
|
|
|
Help: "Messages sent, partitioned by endpoint and status",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "status"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if publishTimeCounterSummary == nil {
|
|
|
|
publishTimeCounterSummary = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Name: fmt.Sprintf("%spublish_message_latency_microseconds", DefaultMetricPrefix),
|
|
|
|
Help: "Message latencies in microseconds, partitioned by endpoint",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if publishTimeCounterHistogram == nil {
|
|
|
|
publishTimeCounterHistogram = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Name: fmt.Sprintf("%spublish_message_duration_seconds", DefaultMetricPrefix),
|
|
|
|
Help: "Message publish time in seconds, partitioned by endpoint",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, collector := range []prometheus.Collector{publishOpsCounter, publishTimeCounterSummary, publishTimeCounterHistogram} {
|
|
|
|
if err := prometheus.DefaultRegisterer.Register(collector); err != nil {
|
|
|
|
// if already registered, skip fatal
|
|
|
|
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
|
|
|
|
logger.Fatal(ctx, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerSubscribeMetrics(ctx context.Context) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if subscribeOpsCounter == nil {
|
|
|
|
subscribeOpsCounter = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: fmt.Sprintf("%ssubscribe_message_total", DefaultMetricPrefix),
|
|
|
|
Help: "Messages processed, partitioned by endpoint and status",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "status"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subscribeTimeCounterSummary == nil {
|
|
|
|
subscribeTimeCounterSummary = prometheus.NewSummaryVec(
|
|
|
|
prometheus.SummaryOpts{
|
|
|
|
Name: fmt.Sprintf("%ssubscribe_message_latency_microseconds", DefaultMetricPrefix),
|
|
|
|
Help: "Message processing latencies in microseconds, partitioned by endpoint",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if subscribeTimeCounterHistogram == nil {
|
|
|
|
subscribeTimeCounterHistogram = prometheus.NewHistogramVec(
|
|
|
|
prometheus.HistogramOpts{
|
|
|
|
Name: fmt.Sprintf("%ssubscribe_message_duration_seconds", DefaultMetricPrefix),
|
|
|
|
Help: "Request time in seconds, partitioned by endpoint",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, collector := range []prometheus.Collector{subscribeOpsCounter, subscribeTimeCounterSummary, subscribeTimeCounterHistogram} {
|
|
|
|
if err := prometheus.DefaultRegisterer.Register(collector); err != nil {
|
|
|
|
// if already registered, skip fatal
|
|
|
|
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
|
|
|
|
logger.Fatal(ctx, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerClientMetrics(ctx context.Context) {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if clientOpsCounter == nil {
|
|
|
|
clientOpsCounter = prometheus.NewCounterVec(
|
2020-04-17 02:45:40 +03:00
|
|
|
prometheus.CounterOpts{
|
2020-06-10 16:04:12 +03:00
|
|
|
Name: fmt.Sprintf("%srequest_total", DefaultMetricPrefix),
|
|
|
|
Help: "Requests processed, partitioned by endpoint and status",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "status"),
|
2020-04-17 02:45:40 +03:00
|
|
|
},
|
|
|
|
)
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
if clientTimeCounterSummary == nil {
|
|
|
|
clientTimeCounterSummary = prometheus.NewSummaryVec(
|
2020-04-17 02:45:40 +03:00
|
|
|
prometheus.SummaryOpts{
|
2020-06-10 16:04:12 +03:00
|
|
|
Name: fmt.Sprintf("%slatency_microseconds", DefaultMetricPrefix),
|
|
|
|
Help: "Request latencies in microseconds, partitioned by endpoint",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
2020-04-17 02:45:40 +03:00
|
|
|
},
|
|
|
|
)
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
if clientTimeCounterHistogram == nil {
|
|
|
|
clientTimeCounterHistogram = prometheus.NewHistogramVec(
|
2020-04-17 02:45:40 +03:00
|
|
|
prometheus.HistogramOpts{
|
2020-06-10 16:04:12 +03:00
|
|
|
Name: fmt.Sprintf("%srequest_duration_seconds", DefaultMetricPrefix),
|
|
|
|
Help: "Request time in seconds, partitioned by endpoint",
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "name"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "version"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "id"),
|
|
|
|
fmt.Sprintf("%s%s", DefaultLabelPrefix, "endpoint"),
|
2020-04-17 02:45:40 +03:00
|
|
|
},
|
|
|
|
)
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
for _, collector := range []prometheus.Collector{clientOpsCounter, clientTimeCounterSummary, clientTimeCounterHistogram} {
|
2020-06-10 16:04:12 +03:00
|
|
|
if err := prometheus.DefaultRegisterer.Register(collector); err != nil {
|
|
|
|
// if already registered, skip fatal
|
|
|
|
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
|
2021-01-19 16:51:58 +03:00
|
|
|
logger.Fatal(ctx, err.Error())
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
type wrapper struct {
|
|
|
|
options Options
|
|
|
|
callFunc client.CallFunc
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWrapper(opts ...Option) client.Wrapper {
|
2021-01-19 16:51:58 +03:00
|
|
|
options := Options{Context: context.Background()}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
registerClientMetrics(options.Context)
|
|
|
|
registerPublishMetrics(options.Context)
|
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
return func(c client.Client) client.Client {
|
2020-04-17 02:45:40 +03:00
|
|
|
handler := &wrapper{
|
2020-06-10 16:04:12 +03:00
|
|
|
options: options,
|
|
|
|
Client: c,
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
func NewCallWrapper(opts ...Option) client.CallWrapper {
|
2021-01-19 16:51:58 +03:00
|
|
|
options := Options{Context: context.Background()}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
registerClientMetrics(options.Context)
|
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
return func(fn client.CallFunc) client.CallFunc {
|
2020-04-17 02:45:40 +03:00
|
|
|
handler := &wrapper{
|
2020-06-10 16:04:12 +03:00
|
|
|
options: options,
|
|
|
|
callFunc: fn,
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return handler.CallFunc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
func (w *wrapper) CallFunc(ctx context.Context, addr string, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
2020-06-10 16:04:12 +03:00
|
|
|
endpoint := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
|
2020-04-17 02:45:40 +03:00
|
|
|
|
|
|
|
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
|
|
|
|
us := v * 1000000 // make microseconds
|
2021-01-19 16:51:58 +03:00
|
|
|
clientTimeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
clientTimeCounterHistogram.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(v)
|
2020-04-17 02:45:40 +03:00
|
|
|
}))
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
err := w.callFunc(ctx, addr, req, rsp, opts)
|
2020-04-17 02:45:40 +03:00
|
|
|
if err == nil {
|
2021-01-19 16:51:58 +03:00
|
|
|
clientOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2021-01-19 16:51:58 +03:00
|
|
|
clientOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "failure").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
2020-06-10 16:04:12 +03:00
|
|
|
endpoint := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
|
2020-04-17 02:45:40 +03:00
|
|
|
|
|
|
|
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
|
|
|
|
us := v * 1000000 // make microseconds
|
2021-01-19 16:51:58 +03:00
|
|
|
clientTimeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
clientTimeCounterHistogram.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(v)
|
2020-04-17 02:45:40 +03:00
|
|
|
}))
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
err := w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
if err == nil {
|
2021-01-19 16:51:58 +03:00
|
|
|
clientOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2021-01-19 16:51:58 +03:00
|
|
|
clientOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "failure").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
|
2020-06-10 16:04:12 +03:00
|
|
|
endpoint := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
|
2020-04-17 02:45:40 +03:00
|
|
|
|
|
|
|
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
|
|
|
|
us := v * 1000000 // make microseconds
|
2021-01-19 16:51:58 +03:00
|
|
|
clientTimeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
clientTimeCounterHistogram.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(v)
|
2020-04-17 02:45:40 +03:00
|
|
|
}))
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
stream, err := w.Client.Stream(ctx, req, opts...)
|
|
|
|
if err == nil {
|
2021-01-19 16:51:58 +03:00
|
|
|
clientOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2021-01-19 16:51:58 +03:00
|
|
|
clientOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "failure").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return stream, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
2020-06-10 16:04:12 +03:00
|
|
|
endpoint := p.Topic()
|
2020-04-17 02:45:40 +03:00
|
|
|
|
|
|
|
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
|
|
|
|
us := v * 1000000 // make microseconds
|
2021-01-19 16:51:58 +03:00
|
|
|
publishTimeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
publishTimeCounterHistogram.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(v)
|
2020-04-17 02:45:40 +03:00
|
|
|
}))
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
err := w.Client.Publish(ctx, p, opts...)
|
|
|
|
if err == nil {
|
2021-01-19 16:51:58 +03:00
|
|
|
publishOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2021-01-19 16:51:58 +03:00
|
|
|
publishOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "failure").Inc()
|
2019-01-26 12:33:51 +03:00
|
|
|
}
|
|
|
|
|
2020-04-17 02:45:40 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
|
2021-01-19 16:51:58 +03:00
|
|
|
options := Options{Context: context.Background()}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
registerServerMetrics(options.Context)
|
2020-04-17 02:45:40 +03:00
|
|
|
|
|
|
|
handler := &wrapper{
|
2020-06-10 16:04:12 +03:00
|
|
|
options: options,
|
2020-04-17 02:45:40 +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 {
|
2020-06-10 16:04:12 +03:00
|
|
|
endpoint := req.Endpoint()
|
2020-04-17 02:45:40 +03:00
|
|
|
|
|
|
|
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
|
|
|
|
us := v * 1000000 // make microseconds
|
2021-01-19 16:51:58 +03:00
|
|
|
serverTimeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
serverTimeCounterHistogram.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(v)
|
2020-04-17 02:45:40 +03:00
|
|
|
}))
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
err := fn(ctx, req, rsp)
|
|
|
|
if err == nil {
|
2021-01-19 16:51:58 +03:00
|
|
|
serverOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2021-01-19 16:51:58 +03:00
|
|
|
serverOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "failure").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-02-04 01:26:31 +03:00
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
func NewSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
|
2021-01-19 16:51:58 +03:00
|
|
|
options := Options{Context: context.Background()}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-01-19 16:51:58 +03:00
|
|
|
registerSubscribeMetrics(options.Context)
|
|
|
|
|
2020-04-17 02:45:40 +03:00
|
|
|
handler := &wrapper{
|
2020-06-10 16:04:12 +03:00
|
|
|
options: options,
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
2019-01-25 17:27:27 +03:00
|
|
|
|
2020-04-17 02:45:40 +03:00
|
|
|
return handler.SubscriberFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) SubscriberFunc(fn server.SubscriberFunc) server.SubscriberFunc {
|
|
|
|
return func(ctx context.Context, msg server.Message) error {
|
2020-06-10 16:04:12 +03:00
|
|
|
endpoint := msg.Topic()
|
2020-04-17 02:45:40 +03:00
|
|
|
|
|
|
|
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
|
|
|
|
us := v * 1000000 // make microseconds
|
2021-01-19 16:51:58 +03:00
|
|
|
subscribeTimeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
subscribeTimeCounterHistogram.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(v)
|
2020-04-17 02:45:40 +03:00
|
|
|
}))
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
err := fn(ctx, msg)
|
|
|
|
if err == nil {
|
2021-01-19 16:51:58 +03:00
|
|
|
subscribeOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2021-01-19 16:51:58 +03:00
|
|
|
subscribeOpsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "failure").Inc()
|
2019-01-25 17:27:27 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
|
|
|
return err
|
2019-01-25 17:27:27 +03:00
|
|
|
}
|
|
|
|
}
|