Compare commits

..

No commits in common. "v3" and "v3.8.16" have entirely different histories.
v3 ... v3.8.16

5 changed files with 75 additions and 106 deletions

View File

@ -11,57 +11,55 @@ import (
type prometheusCounter struct { type prometheusCounter struct {
name string name string
c *dto.Metric c *dto.Metric
n float64
} }
func (c *prometheusCounter) Add(n int) { func (c *prometheusCounter) Add(n int) {
addFloat64(&(c.n), float64(n)) addFloat64(c.c.Gauge.Value, float64(n))
} }
func (c *prometheusCounter) Dec() { func (c *prometheusCounter) Dec() {
addFloat64(&(c.n), float64(-1)) addFloat64(c.c.Gauge.Value, float64(-1))
} }
func (c *prometheusCounter) Inc() { func (c *prometheusCounter) Inc() {
addFloat64(&(c.n), float64(1)) addFloat64(c.c.Gauge.Value, float64(1))
} }
func (c *prometheusCounter) Get() uint64 { func (c *prometheusCounter) Get() uint64 {
return uint64(getFloat64(&(c.n))) return uint64(getFloat64(c.c.Gauge.Value))
} }
func (c *prometheusCounter) Set(n uint64) { func (c *prometheusCounter) Set(n uint64) {
setFloat64(&(c.n), math.Float64frombits(n)) setFloat64(c.c.Gauge.Value, math.Float64frombits(n))
} }
type prometheusFloatCounter struct { type prometheusFloatCounter struct {
name string name string
c *dto.Metric c *dto.Metric
n float64
} }
func (c *prometheusFloatCounter) Add(n float64) { func (c *prometheusFloatCounter) Add(n float64) {
addFloat64(&(c.n), n) addFloat64(c.c.Gauge.Value, n)
} }
func (c *prometheusFloatCounter) Dec() { func (c *prometheusFloatCounter) Dec() {
addFloat64(&(c.n), float64(-1)) addFloat64(c.c.Gauge.Value, float64(-1))
} }
func (c *prometheusFloatCounter) Inc() { func (c *prometheusFloatCounter) Inc() {
addFloat64(&(c.n), float64(1)) addFloat64(c.c.Gauge.Value, float64(1))
} }
func (c *prometheusFloatCounter) Get() float64 { func (c *prometheusFloatCounter) Get() float64 {
return getFloat64(&(c.n)) return getFloat64(c.c.Gauge.Value)
} }
func (c *prometheusFloatCounter) Set(n float64) { func (c *prometheusFloatCounter) Set(n float64) {
setFloat64(&(c.n), n) setFloat64(c.c.Gauge.Value, n)
} }
func (c *prometheusFloatCounter) Sub(n float64) { func (c *prometheusFloatCounter) Sub(n float64) {
addFloat64(&(c.n), -n) addFloat64(c.c.Gauge.Value, -n)
} }
func setFloat64(_addr *float64, value float64) float64 { func setFloat64(_addr *float64, value float64) float64 {

View File

@ -5,9 +5,8 @@ import dto "github.com/prometheus/client_model/go"
type prometheusGauge struct { type prometheusGauge struct {
name string name string
c *dto.Metric c *dto.Metric
n float64
} }
func (c *prometheusGauge) Get() float64 { func (c prometheusGauge) Get() float64 {
return getFloat64(&(c.n)) return getFloat64(c.c.Gauge.Value)
} }

View File

@ -5,7 +5,6 @@ import (
"io" "io"
"regexp" "regexp"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -54,83 +53,82 @@ func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
m.mu.Lock() m.mu.Lock()
c, ok := m.counter[h] mc, ok := m.counter[h]
// fmt.Printf("counter name %s hash %v labels %v\n", name, h, labels)
m.mu.Unlock() m.mu.Unlock()
if !ok { if !ok {
var n float64 var v float64
c = &prometheusCounter{ mc = &prometheusCounter{
name: name, name: name,
c: &dto.Metric{ c: &dto.Metric{
Gauge: &dto.Gauge{Value: &n}, Gauge: &dto.Gauge{Value: &v},
Label: labelMetric(clabels), Label: labelMetric(clabels),
}, },
} }
m.mu.Lock() m.mu.Lock()
m.counter[h] = c m.counter[h] = mc
m.mu.Unlock() m.mu.Unlock()
} }
return c return mc
} }
func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter { func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
m.mu.Lock() m.mu.Lock()
c, ok := m.floatCounter[h] mc, ok := m.floatCounter[h]
m.mu.Unlock() m.mu.Unlock()
if !ok { if !ok {
var n float64 var v float64
c = &prometheusFloatCounter{ mc = &prometheusFloatCounter{
name: name, name: name,
c: &dto.Metric{ c: &dto.Metric{
Gauge: &dto.Gauge{Value: &n}, Gauge: &dto.Gauge{Value: &v},
Label: labelMetric(clabels), Label: labelMetric(clabels),
}, },
} }
m.mu.Lock() m.mu.Lock()
m.floatCounter[h] = c m.floatCounter[h] = mc
m.mu.Unlock() m.mu.Unlock()
} }
return c return mc
} }
func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge { func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
m.mu.Lock() m.mu.Lock()
c, ok := m.gauge[h] mc, ok := m.gauge[h]
m.mu.Unlock() m.mu.Unlock()
if !ok { if !ok {
var n float64 var v float64
c = &prometheusGauge{ mc = &prometheusGauge{
name: name, name: name,
c: &dto.Metric{ c: &dto.Metric{
Gauge: &dto.Gauge{Value: &n}, Gauge: &dto.Gauge{Value: &v},
Label: labelMetric(clabels), Label: labelMetric(clabels),
}, },
} }
m.mu.Lock() m.mu.Lock()
m.gauge[h] = c m.gauge[h] = mc
m.mu.Unlock() m.mu.Unlock()
} }
return c return mc
} }
func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram { func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
m.mu.Lock() m.mu.Lock()
c, ok := m.histogram[h] mc, ok := m.histogram[h]
m.mu.Unlock() m.mu.Unlock()
if !ok { if !ok {
var n uint64 var c uint64
var s float64 var s float64
buckets := make([]float64, len(prometheus.DefBuckets)) buckets := make([]float64, len(prometheus.DefBuckets))
copy(buckets, prometheus.DefBuckets) copy(buckets, prometheus.DefBuckets)
mdto := &dto.Metric{ mdto := &dto.Metric{
Histogram: &dto.Histogram{ Histogram: &dto.Histogram{
SampleCount: &n, SampleCount: &c,
SampleSum: &s, SampleSum: &s,
CreatedTimestamp: timestamppb.Now(), CreatedTimestamp: timestamppb.Now(),
Bucket: make([]*dto.Bucket, len(buckets)), Bucket: make([]*dto.Bucket, len(buckets)),
@ -141,31 +139,31 @@ func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogr
var cc uint64 var cc uint64
mdto.Histogram.Bucket[idx] = &dto.Bucket{CumulativeCount: &cc, UpperBound: &b} mdto.Histogram.Bucket[idx] = &dto.Bucket{CumulativeCount: &cc, UpperBound: &b}
} }
c = &prometheusHistogram{ mc = &prometheusHistogram{
name: name, name: name,
c: mdto, c: mdto,
} }
m.mu.Lock() m.mu.Lock()
m.histogram[h] = c m.histogram[h] = mc
m.mu.Unlock() m.mu.Unlock()
} }
return c return mc
} }
func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary { func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
m.mu.Lock() m.mu.Lock()
c, ok := m.summary[h] mc, ok := m.summary[h]
m.mu.Unlock() m.mu.Unlock()
if !ok { if !ok {
var n uint64 var c uint64
var s float64 var s float64
c = &prometheusSummary{ mc = &prometheusSummary{
name: name, name: name,
c: &dto.Metric{ c: &dto.Metric{
Summary: &dto.Summary{ Summary: &dto.Summary{
SampleCount: &n, SampleCount: &c,
SampleSum: &s, SampleSum: &s,
CreatedTimestamp: timestamppb.Now(), CreatedTimestamp: timestamppb.Now(),
}, },
@ -173,36 +171,36 @@ func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
}, },
} }
m.mu.Lock() m.mu.Lock()
m.summary[h] = c m.summary[h] = mc
m.mu.Unlock() m.mu.Unlock()
} }
return c return mc
} }
func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary { func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
m.mu.Lock() m.mu.Lock()
c, ok := m.summary[h] mc, ok := m.summary[h]
m.mu.Lock() m.mu.Lock()
if !ok { if !ok {
var n uint64 var c uint64
var s float64 var s float64
c = &prometheusSummary{ mc = &prometheusSummary{
name: name, name: name,
c: &dto.Metric{ c: &dto.Metric{
Summary: &dto.Summary{ Summary: &dto.Summary{
SampleCount: &n, SampleCount: &c,
SampleSum: &s, SampleSum: &s,
}, },
Label: labelMetric(clabels), Label: labelMetric(clabels),
}, },
} }
m.mu.Lock() m.mu.Lock()
m.summary[h] = c m.summary[h] = mc
m.mu.Unlock() m.mu.Unlock()
} }
return c return mc
} }
func (m *prometheusMeter) Init(opts ...meter.Option) error { func (m *prometheusMeter) Init(opts ...meter.Option) error {
@ -240,33 +238,27 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
m.mu.Lock() m.mu.Lock()
for _, mc := range m.counter { for _, c := range m.counter {
mf := m.mfPool.Get() mf := m.mfPool.Get()
mf.Name = &mc.name mf.Name = &c.name
mf.Type = dto.MetricType_GAUGE.Enum() mf.Type = dto.MetricType_GAUGE.Enum()
n := getFloat64(&(mc.n)) mf.Metric = append(mf.Metric, c.c)
mc.c.Gauge.Value = &n
mf.Metric = append(mf.Metric, mc.c)
mfs = append(mfs, mf) mfs = append(mfs, mf)
} }
for _, mc := range m.floatCounter { for _, c := range m.floatCounter {
mf := m.mfPool.Get() mf := m.mfPool.Get()
mf.Name = &mc.name mf.Name = &c.name
mf.Type = dto.MetricType_GAUGE.Enum() mf.Type = dto.MetricType_GAUGE.Enum()
n := getFloat64(&(mc.n)) mf.Metric = append(mf.Metric, c.c)
mc.c.Gauge.Value = &n
mf.Metric = append(mf.Metric, mc.c)
mfs = append(mfs, mf) mfs = append(mfs, mf)
} }
for _, mc := range m.gauge { for _, c := range m.gauge {
mf := m.mfPool.Get() mf := m.mfPool.Get()
mf.Name = &mc.name mf.Name = &c.name
mf.Type = dto.MetricType_GAUGE.Enum() mf.Type = dto.MetricType_GAUGE.Enum()
n := getFloat64(&(mc.n)) mf.Metric = append(mf.Metric, c.c)
mc.c.Gauge.Value = &n
mf.Metric = append(mf.Metric, mc.c)
mfs = append(mfs, mf) mfs = append(mfs, mf)
} }
@ -278,15 +270,11 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
mfs = append(mfs, mf) mfs = append(mfs, mf)
} }
for _, mc := range m.summary { for _, c := range m.summary {
mf := m.mfPool.Get() mf := m.mfPool.Get()
mf.Name = &mc.name mf.Name = &c.name
mf.Type = dto.MetricType_SUMMARY.Enum() mf.Type = dto.MetricType_SUMMARY.Enum()
sc := atomic.LoadUint64(&(mc.sampleCount)) mf.Metric = append(mf.Metric, c.c)
mc.c.Summary.SampleCount = &sc
ss := getFloat64(&(mc.SampleSum))
mc.c.Summary.SampleSum = &ss
mf.Metric = append(mf.Metric, mc.c)
mfs = append(mfs, mf) mfs = append(mfs, mf)
} }

View File

@ -15,25 +15,11 @@ import (
) )
func TestHash(t *testing.T) { func TestHash(t *testing.T) {
m := NewMeter() // meter.Labels("test_key", "test_val")) t.Skip()
h1 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/clientprofile.ClientProfileService/GetClientProfile", "status", "failure"})
buf := bytes.NewBuffer(nil) h2 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/clientproduct.ClientProductService/GetDepositProducts", "status", "failure"})
h3 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/operationsinfo.OperationsInfoService/GetOperations", "status", "failure"})
for i := 0; i < 100000; i++ { t.Logf("h1: %v\nh2: %v\nh3: %v\n", h1, h2, h3)
go func() {
m.Counter("micro_server_request_total", "code", "16",
"endpoint", "/clientprofile.ClientProfileService/GetClientProfile",
"status", "failure").Inc()
m.Counter("micro_server_request_total", "code", "16",
"endpoint", "/clientproduct.ClientProductService/GetDepositProducts",
"status", "failure").Inc()
m.Counter("micro_server_request_total", "code", "16",
"endpoint", "/operationsinfo.OperationsInfoService/GetOperations",
"status", "failure").Inc()
}()
}
_ = m.Write(buf)
t.Logf("h1: %s\n", buf.Bytes())
} }
func TestHistogram(t *testing.T) { func TestHistogram(t *testing.T) {

View File

@ -10,17 +10,15 @@ import (
type prometheusSummary struct { type prometheusSummary struct {
name string name string
c *dto.Metric c *dto.Metric
sampleCount uint64
SampleSum float64
} }
func (c *prometheusSummary) Update(n float64) { func (c prometheusSummary) Update(n float64) {
atomic.AddUint64(&(c.sampleCount), 1) atomic.AddUint64(c.c.Summary.SampleCount, 1)
addFloat64(&(c.SampleSum), n) addFloat64(c.c.Summary.SampleSum, n)
} }
func (c *prometheusSummary) UpdateDuration(t time.Time) { func (c prometheusSummary) UpdateDuration(n time.Time) {
n := time.Since(t).Seconds() x := time.Since(n).Seconds()
atomic.AddUint64(&(c.sampleCount), 1) atomic.AddUint64(c.c.Summary.SampleCount, 1)
addFloat64(&(c.SampleSum), n) addFloat64(c.c.Summary.SampleSum, x)
} }