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
|
|
|
|
2020-04-17 02:45:40 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/logger"
|
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2020-01-31 01:26:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/server"
|
2019-01-25 17:27:27 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2020-04-17 02:45:40 +03:00
|
|
|
opsCounter *prometheus.CounterVec
|
|
|
|
timeCounterSummary *prometheus.SummaryVec
|
|
|
|
timeCounterHistogram *prometheus.HistogramVec
|
2020-06-10 16:04:12 +03:00
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
ID string
|
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
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
func ServiceName(name string) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.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 {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.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 {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.ID = id
|
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
func registerMetrics() {
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
|
|
|
|
if opsCounter == nil {
|
|
|
|
opsCounter = 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
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
if timeCounterSummary == nil {
|
|
|
|
timeCounterSummary = 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
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
if timeCounterHistogram == nil {
|
|
|
|
timeCounterHistogram = 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
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
for _, collector := range []prometheus.Collector{opsCounter, timeCounterSummary, timeCounterHistogram} {
|
|
|
|
if err := prometheus.DefaultRegisterer.Register(collector); err != nil {
|
|
|
|
// if already registered, skip fatal
|
|
|
|
if _, ok := err.(prometheus.AlreadyRegisteredError); !ok {
|
|
|
|
logger.Fatal(err)
|
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 {
|
|
|
|
registerMetrics()
|
|
|
|
|
|
|
|
options := Options{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&options)
|
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
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 {
|
|
|
|
registerMetrics()
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
options := Options{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&options)
|
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) CallFunc(ctx context.Context, node *registry.Node, 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
|
2020-06-10 16:04:12 +03:00
|
|
|
timeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
timeCounterHistogram.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.callFunc(ctx, node, req, rsp, opts)
|
|
|
|
if err == nil {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.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
|
2020-06-10 16:04:12 +03:00
|
|
|
timeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
timeCounterHistogram.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 {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.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
|
2020-06-10 16:04:12 +03:00
|
|
|
timeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
timeCounterHistogram.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 {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.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
|
2020-06-10 16:04:12 +03:00
|
|
|
timeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
timeCounterHistogram.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 {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.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 {
|
|
|
|
registerMetrics()
|
2019-01-25 17:27:27 +03:00
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
options := Options{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&options)
|
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
|
2020-06-10 16:04:12 +03:00
|
|
|
timeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
timeCounterHistogram.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 {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.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 {
|
|
|
|
registerMetrics()
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2020-06-10 16:04:12 +03:00
|
|
|
options := Options{}
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(&options)
|
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
|
2020-06-10 16:04:12 +03:00
|
|
|
timeCounterSummary.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint).Observe(us)
|
|
|
|
timeCounterHistogram.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 {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.WithLabelValues(w.options.Name, w.options.Version, w.options.ID, endpoint, "success").Inc()
|
2020-04-17 02:45:40 +03:00
|
|
|
} else {
|
2020-06-10 16:04:12 +03:00
|
|
|
opsCounter.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
|
|
|
}
|
|
|
|
}
|