1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-02-04 18:21:53 +02:00

Optimize prometheus wrapper,removed mutex,initialize MetricVec at init() (#2192)

Co-authored-by: dylan.deng <dylan.deng@yijinin.com>
This commit is contained in:
lanrion 2021-07-09 19:52:08 +08:00 committed by GitHub
parent 6cdf28270f
commit 3fbf2c304f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,8 +3,6 @@ package prometheus
import (
"context"
"fmt"
"sync"
"github.com/asim/go-micro/v3/client"
"github.com/asim/go-micro/v3/logger"
"github.com/asim/go-micro/v3/registry"
@ -21,8 +19,6 @@ var (
opsCounter *prometheus.CounterVec
timeCounterSummary *prometheus.SummaryVec
timeCounterHistogram *prometheus.HistogramVec
mu sync.Mutex
)
type Options struct {
@ -51,9 +47,7 @@ func ServiceID(id string) Option {
}
}
func registerMetrics() {
mu.Lock()
defer mu.Unlock()
func init() {
if opsCounter == nil {
opsCounter = prometheus.NewCounterVec(
@ -119,7 +113,6 @@ type wrapper struct {
}
func NewClientWrapper(opts ...Option) client.Wrapper {
registerMetrics()
options := Options{}
for _, opt := range opts {
@ -137,8 +130,6 @@ func NewClientWrapper(opts ...Option) client.Wrapper {
}
func NewCallWrapper(opts ...Option) client.CallWrapper {
registerMetrics()
options := Options{}
for _, opt := range opts {
opt(&options)
@ -236,8 +227,6 @@ func (w *wrapper) Publish(ctx context.Context, p client.Message, opts ...client.
}
func NewHandlerWrapper(opts ...Option) server.HandlerWrapper {
registerMetrics()
options := Options{}
for _, opt := range opts {
opt(&options)
@ -273,8 +262,6 @@ func (w *wrapper) HandlerFunc(fn server.HandlerFunc) server.HandlerFunc {
}
func NewSubscriberWrapper(opts ...Option) server.SubscriberWrapper {
registerMetrics()
options := Options{}
for _, opt := range opts {
opt(&options)