Compare commits

..

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

5 changed files with 75 additions and 106 deletions

View File

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

View File

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

View File

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

View File

@ -15,25 +15,11 @@ import (
)
func TestHash(t *testing.T) {
m := NewMeter() // meter.Labels("test_key", "test_val"))
buf := bytes.NewBuffer(nil)
for i := 0; i < 100000; i++ {
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())
t.Skip()
h1 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/clientprofile.ClientProfileService/GetClientProfile", "status", "failure"})
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"})
t.Logf("h1: %v\nh2: %v\nh3: %v\n", h1, h2, h3)
}
func TestHistogram(t *testing.T) {

View File

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