2019-12-04 01:49:07 +03:00
|
|
|
package victoriametrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metrics "github.com/VictoriaMetrics/metrics"
|
2020-06-20 17:09:22 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2020-01-31 01:26:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/server"
|
2019-12-04 01:49:07 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-20 17:09:22 +03:00
|
|
|
// default metric prefix
|
|
|
|
DefaultMetricPrefix = "micro_"
|
|
|
|
// default label prefix
|
|
|
|
DefaultLabelPrefix = "micro_"
|
2019-12-04 01:49:07 +03:00
|
|
|
)
|
|
|
|
|
2020-06-20 17:09:22 +03:00
|
|
|
type Options struct {
|
|
|
|
Name string
|
|
|
|
Version string
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option func(*Options)
|
|
|
|
|
|
|
|
func ServiceName(name string) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Name = name
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 01:49:07 +03:00
|
|
|
|
2020-06-20 17:09:22 +03:00
|
|
|
func ServiceVersion(version string) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.Version = version
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func ServiceID(id string) Option {
|
|
|
|
return func(opts *Options) {
|
|
|
|
opts.ID = id
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 01:49:07 +03:00
|
|
|
|
2020-06-20 17:09:22 +03:00
|
|
|
func getName(name string, labels []string) string {
|
2019-12-04 01:49:07 +03:00
|
|
|
if len(labels) > 0 {
|
2020-06-20 17:09:22 +03:00
|
|
|
return fmt.Sprintf(`%s%s{%s}`, DefaultMetricPrefix, name, strings.Join(labels, ","))
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2020-06-20 17:09:22 +03:00
|
|
|
return fmt.Sprintf(`%s%s`, DefaultMetricPrefix, name)
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
|
|
|
|
2020-06-20 17:09:22 +03:00
|
|
|
func getLabels(opts ...Option) []string {
|
|
|
|
options := Options{}
|
2019-12-04 01:49:07 +03:00
|
|
|
|
|
|
|
for _, opt := range opts {
|
2020-06-20 17:09:22 +03:00
|
|
|
opt(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
labels := make([]string, 0, 3)
|
|
|
|
labels = append(labels, fmt.Sprintf(`%sname="%s"`, DefaultLabelPrefix, options.Name))
|
|
|
|
labels = append(labels, fmt.Sprintf(`%sversion="%s"`, DefaultLabelPrefix, options.Version))
|
|
|
|
labels = append(labels, fmt.Sprintf(`%sid="%s"`, DefaultLabelPrefix, options.ID))
|
|
|
|
|
|
|
|
return labels
|
|
|
|
}
|
|
|
|
|
|
|
|
type wrapper struct {
|
|
|
|
options Options
|
|
|
|
callFunc client.CallFunc
|
|
|
|
client.Client
|
|
|
|
labels []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWrapper(opts ...Option) client.Wrapper {
|
|
|
|
labels := getLabels(opts...)
|
|
|
|
|
|
|
|
return func(c client.Client) client.Client {
|
|
|
|
handler := &wrapper{
|
|
|
|
labels: labels,
|
|
|
|
Client: c,
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCallWrapper(opts ...Option) client.CallWrapper {
|
|
|
|
labels := getLabels(opts...)
|
|
|
|
|
|
|
|
return func(fn client.CallFunc) client.CallFunc {
|
|
|
|
handler := &wrapper{
|
|
|
|
labels: labels,
|
|
|
|
callFunc: fn,
|
|
|
|
}
|
|
|
|
|
|
|
|
return handler.CallFunc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) CallFunc(ctx context.Context, node *registry.Node, req client.Request, rsp interface{}, opts client.CallOptions) error {
|
|
|
|
endpoint := fmt.Sprintf("%s.%s", req.Service(), req.Endpoint())
|
|
|
|
wlabels := append(w.labels, fmt.Sprintf(`%sendpoint="%s"`, DefaultLabelPrefix, endpoint))
|
|
|
|
|
|
|
|
timeCounterSummary := metrics.GetOrCreateSummary(getName("upstream_latency_seconds", wlabels))
|
|
|
|
timeCounterHistogram := metrics.GetOrCreateSummary(getName("request_duration_seconds", wlabels))
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.callFunc(ctx, node, req, rsp, opts)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
|
|
|
timeCounterSummary.Update(float64(te.Seconds()))
|
|
|
|
timeCounterHistogram.Update(te.Seconds())
|
|
|
|
if err == nil {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="success"`, DefaultLabelPrefix)))).Inc()
|
|
|
|
} else {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="failure"`, DefaultLabelPrefix)))).Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
wlabels := append(w.labels, fmt.Sprintf(`%sendpoint="%s"`, DefaultLabelPrefix, endpoint))
|
|
|
|
|
|
|
|
timeCounterSummary := metrics.GetOrCreateSummary(getName("upstream_latency_seconds", wlabels))
|
|
|
|
timeCounterHistogram := metrics.GetOrCreateSummary(getName("request_duration_seconds", wlabels))
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.Client.Call(ctx, req, rsp, opts...)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
|
|
|
timeCounterSummary.Update(float64(te.Seconds()))
|
|
|
|
timeCounterHistogram.Update(te.Seconds())
|
|
|
|
if err == nil {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="success"`, DefaultLabelPrefix)))).Inc()
|
|
|
|
} else {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="failure"`, DefaultLabelPrefix)))).Inc()
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
|
|
|
|
2020-06-20 17:09:22 +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())
|
|
|
|
wlabels := append(w.labels, fmt.Sprintf(`%sendpoint="%s"`, DefaultLabelPrefix, endpoint))
|
|
|
|
|
|
|
|
timeCounterSummary := metrics.GetOrCreateSummary(getName("upstream_latency_seconds", wlabels))
|
|
|
|
timeCounterHistogram := metrics.GetOrCreateSummary(getName("request_duration_seconds", wlabels))
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
stream, err := w.Client.Stream(ctx, req, opts...)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
|
|
|
timeCounterSummary.Update(float64(te.Seconds()))
|
|
|
|
timeCounterHistogram.Update(te.Seconds())
|
|
|
|
if err == nil {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="success"`, DefaultLabelPrefix)))).Inc()
|
|
|
|
} else {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="failure"`, DefaultLabelPrefix)))).Inc()
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2020-06-20 17:09:22 +03:00
|
|
|
|
|
|
|
return stream, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
|
|
|
|
endpoint := p.Topic()
|
|
|
|
wlabels := append(w.labels, fmt.Sprintf(`%sendpoint="%s"`, DefaultLabelPrefix, endpoint))
|
|
|
|
|
|
|
|
timeCounterSummary := metrics.GetOrCreateSummary(getName("upstream_latency_seconds", wlabels))
|
|
|
|
timeCounterHistogram := metrics.GetOrCreateSummary(getName("request_duration_seconds", wlabels))
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.Client.Publish(ctx, p, opts...)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
|
|
|
timeCounterSummary.Update(float64(te.Seconds()))
|
|
|
|
timeCounterHistogram.Update(te.Seconds())
|
|
|
|
if err == nil {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="success"`, DefaultLabelPrefix)))).Inc()
|
|
|
|
} else {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="failure"`, DefaultLabelPrefix)))).Inc()
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2020-06-20 17:09:22 +03:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
|
|
|
|
labels := getLabels(opts...)
|
|
|
|
|
|
|
|
handler := &wrapper{
|
|
|
|
labels: labels,
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2020-06-20 17:09:22 +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()
|
|
|
|
wlabels := append(w.labels, fmt.Sprintf(`%sendpoint="%s"`, DefaultLabelPrefix, endpoint))
|
|
|
|
|
|
|
|
timeCounterSummary := metrics.GetOrCreateSummary(getName("upstream_latency_seconds", wlabels))
|
|
|
|
timeCounterHistogram := metrics.GetOrCreateSummary(getName("request_duration_seconds", wlabels))
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := fn(ctx, req, rsp)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
|
|
|
timeCounterSummary.Update(float64(te.Seconds()))
|
|
|
|
timeCounterHistogram.Update(te.Seconds())
|
|
|
|
if err == nil {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="success"`, DefaultLabelPrefix)))).Inc()
|
|
|
|
} else {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="failure"`, DefaultLabelPrefix)))).Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2020-06-20 17:09:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
|
|
|
|
labels := getLabels(opts...)
|
|
|
|
|
|
|
|
handler := &wrapper{
|
|
|
|
labels: labels,
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
|
|
|
|
2020-06-20 17:09:22 +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()
|
|
|
|
wlabels := append(w.labels, fmt.Sprintf(`%sendpoint="%s"`, DefaultLabelPrefix, endpoint))
|
|
|
|
|
|
|
|
timeCounterSummary := metrics.GetOrCreateSummary(getName("upstream_latency_seconds", wlabels))
|
|
|
|
timeCounterHistogram := metrics.GetOrCreateSummary(getName("request_duration_seconds", wlabels))
|
|
|
|
|
|
|
|
ts := time.Now()
|
|
|
|
err := fn(ctx, msg)
|
|
|
|
te := time.Since(ts)
|
|
|
|
|
|
|
|
timeCounterSummary.Update(float64(te.Seconds()))
|
|
|
|
timeCounterHistogram.Update(te.Seconds())
|
|
|
|
if err == nil {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="success"`, DefaultLabelPrefix)))).Inc()
|
|
|
|
} else {
|
|
|
|
metrics.GetOrCreateCounter(getName("request_total", append(wlabels, fmt.Sprintf(`%sstatus="failure"`, DefaultLabelPrefix)))).Inc()
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
2020-06-20 17:09:22 +03:00
|
|
|
|
|
|
|
return err
|
2019-12-04 01:49:07 +03:00
|
|
|
}
|
|
|
|
}
|