From 1dbf610d02b4366d64156fa91359ec3cf0eab48b Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Mon, 20 Jan 2025 12:43:18 +0300 Subject: [PATCH] reuse sync.Map Signed-off-by: Vasiliy Tolstov --- go.mod | 14 ++++++--- go.sum | 30 ++++++------------ prometheus.go | 85 +++++++++++++++++++++++++++------------------------ 3 files changed, 64 insertions(+), 65 deletions(-) diff --git a/go.mod b/go.mod index 2af65d6..5215463 100644 --- a/go.mod +++ b/go.mod @@ -7,17 +7,21 @@ require ( github.com/prometheus/client_model v0.6.1 github.com/prometheus/common v0.59.1 go.unistack.org/micro/v3 v3.10.91 - google.golang.org/protobuf v1.34.2 + google.golang.org/protobuf v1.35.2 ) require ( github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect - github.com/golang/protobuf v1.5.4 // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/klauspost/compress v1.17.11 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/procfs v0.15.1 // indirect + github.com/stretchr/testify v1.10.0 // indirect go.unistack.org/micro-proto/v3 v3.4.1 // indirect - golang.org/x/sys v0.25.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect - google.golang.org/grpc v1.58.2 // indirect + golang.org/x/net v0.32.0 // indirect + golang.org/x/sys v0.28.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect + google.golang.org/grpc v1.68.1 // indirect ) diff --git a/go.sum b/go.sum index d9856c5..44098af 100644 --- a/go.sum +++ b/go.sum @@ -2,18 +2,15 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= -github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI= github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= @@ -22,23 +19,16 @@ github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJ github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0= github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= go.unistack.org/micro-proto/v3 v3.4.1 h1:UTjLSRz2YZuaHk9iSlVqqsA50JQNAEK2ZFboGqtEa9Q= go.unistack.org/micro-proto/v3 v3.4.1/go.mod h1:okx/cnOhzuCX0ggl/vToatbCupi0O44diiiLLsZ93Zo= go.unistack.org/micro/v3 v3.10.91 h1:vuJY4tXwpqimwIkEJ3TozMYNVQQs+C5QMlQWPgSY/YM= go.unistack.org/micro/v3 v3.10.91/go.mod h1:erMgt3Bl7vQQ0e9UpQyR5NlLiZ9pKeEJ9+1tfYFaqUg= -golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= -golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= -golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= -golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= -golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= -google.golang.org/grpc v1.58.2 h1:SXUpjxeVF3FKrTYQI4f4KvbGD5u2xccdYdurwowix5I= -google.golang.org/grpc v1.58.2/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= -google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= -google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 h1:8ZmaLZE4XWrtU3MyClkYqqtl6Oegr3235h7jxsDyqCY= +google.golang.org/grpc v1.68.1 h1:oI5oTa11+ng8r8XMMN7jAOmWfPZWbYpCFaMUTACxkM0= +google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/prometheus.go b/prometheus.go index 71183dc..2bbe1cd 100644 --- a/prometheus.go +++ b/prometheus.go @@ -22,24 +22,19 @@ var _ meter.Meter = (*prometheusMeter)(nil) type prometheusMeter struct { opts meter.Options set prometheus.Registerer - counter map[uint64]*prometheusCounter - floatCounter map[uint64]*prometheusFloatCounter - gauge map[uint64]*prometheusGauge - histogram map[uint64]*prometheusHistogram - summary map[uint64]*prometheusSummary + counter sync.Map + floatCounter sync.Map + gauge sync.Map + histogram sync.Map + summary sync.Map mfPool xpool.Pool[*dto.MetricFamily] mu sync.Mutex } func NewMeter(opts ...meter.Option) *prometheusMeter { return &prometheusMeter{ - set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer, - opts: meter.NewOptions(opts...), - counter: make(map[uint64]*prometheusCounter), - floatCounter: make(map[uint64]*prometheusFloatCounter), - gauge: make(map[uint64]*prometheusGauge), - histogram: make(map[uint64]*prometheusHistogram), - summary: make(map[uint64]*prometheusSummary), + set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer, + opts: meter.NewOptions(opts...), mfPool: xpool.NewPool[*dto.MetricFamily](func() *dto.MetricFamily { return &dto.MetricFamily{} }), @@ -54,7 +49,7 @@ func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter { clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) h := newHash(name, clabels) m.mu.Lock() - c, ok := m.counter[h] + c, ok := m.counter.Load(h) // fmt.Printf("counter name %s hash %v labels %v\n", name, h, labels) m.mu.Unlock() if !ok { @@ -67,17 +62,17 @@ func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter { }, } m.mu.Lock() - m.counter[h] = c + m.counter.Store(h, c) m.mu.Unlock() } - return c + return c.(*prometheusCounter) } func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter { clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) h := newHash(name, clabels) m.mu.Lock() - c, ok := m.floatCounter[h] + c, ok := m.counter.Load(h) m.mu.Unlock() if !ok { var n float64 @@ -89,17 +84,17 @@ func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.Floa }, } m.mu.Lock() - m.floatCounter[h] = c + m.counter.Store(h, c) m.mu.Unlock() } - return c + return c.(*prometheusFloatCounter) } func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge { clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) h := newHash(name, clabels) m.mu.Lock() - c, ok := m.gauge[h] + c, ok := m.gauge.Load(h) m.mu.Unlock() if !ok { var n float64 @@ -111,17 +106,17 @@ func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string }, } m.mu.Lock() - m.gauge[h] = c + m.gauge.Store(h, c) m.mu.Unlock() } - return c + return c.(*prometheusGauge) } func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram { clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) h := newHash(name, clabels) m.mu.Lock() - c, ok := m.histogram[h] + c, ok := m.histogram.Load(h) m.mu.Unlock() if !ok { var n uint64 @@ -146,17 +141,17 @@ func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogr c: mdto, } m.mu.Lock() - m.histogram[h] = c + m.histogram.Store(h, c) m.mu.Unlock() } - return c + return c.(*prometheusHistogram) } func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary { clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) h := newHash(name, clabels) m.mu.Lock() - c, ok := m.summary[h] + c, ok := m.summary.Load(h) m.mu.Unlock() if !ok { var n uint64 @@ -173,17 +168,17 @@ func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary { }, } m.mu.Lock() - m.summary[h] = c + m.summary.Store(h, c) m.mu.Unlock() } - return c + return c.(*prometheusSummary) } func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary { clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) h := newHash(name, clabels) m.mu.Lock() - c, ok := m.summary[h] + c, ok := m.summary.Load(h) m.mu.Lock() if !ok { var n uint64 @@ -199,10 +194,10 @@ func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantile }, } m.mu.Lock() - m.summary[h] = c + m.summary.Store(h, c) m.mu.Unlock() } - return c + return c.(*prometheusSummary) } func (m *prometheusMeter) Init(opts ...meter.Option) error { @@ -240,7 +235,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { m.mu.Lock() - for _, mc := range m.counter { + m.counter.Range(func(_ any, v any) bool { + mc := v.(*prometheusCounter) mf := m.mfPool.Get() mf.Name = &mc.name mf.Type = dto.MetricType_GAUGE.Enum() @@ -248,9 +244,11 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { mc.c.Gauge.Value = &n mf.Metric = append(mf.Metric, mc.c) mfs = append(mfs, mf) - } + return true + }) - for _, mc := range m.floatCounter { + m.floatCounter.Range(func(_ any, v any) bool { + mc := v.(*prometheusFloatCounter) mf := m.mfPool.Get() mf.Name = &mc.name mf.Type = dto.MetricType_GAUGE.Enum() @@ -258,9 +256,11 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { mc.c.Gauge.Value = &n mf.Metric = append(mf.Metric, mc.c) mfs = append(mfs, mf) - } + return true + }) - for _, mc := range m.gauge { + m.gauge.Range(func(_ any, v any) bool { + mc := v.(*prometheusGauge) mf := m.mfPool.Get() mf.Name = &mc.name mf.Type = dto.MetricType_GAUGE.Enum() @@ -268,17 +268,21 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { mc.c.Gauge.Value = &n mf.Metric = append(mf.Metric, mc.c) mfs = append(mfs, mf) - } + return true + }) - for _, c := range m.histogram { + m.histogram.Range(func(_ any, v any) bool { + c := v.(*prometheusHistogram) mf := m.mfPool.Get() mf.Name = &c.name mf.Type = dto.MetricType_HISTOGRAM.Enum() mf.Metric = append(mf.Metric, c.c) mfs = append(mfs, mf) - } + return true + }) - for _, mc := range m.summary { + m.summary.Range(func(_ any, v any) bool { + mc := v.(*prometheusSummary) mf := m.mfPool.Get() mf.Name = &mc.name mf.Type = dto.MetricType_SUMMARY.Enum() @@ -288,7 +292,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { mc.c.Summary.SampleSum = &ss mf.Metric = append(mf.Metric, mc.c) mfs = append(mfs, mf) - } + return true + }) m.mu.Unlock()