Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
d4472e1ab2 | |||
6f6d362c20 | |||
dd71d9ec59 |
24
counter.go
24
counter.go
@@ -11,55 +11,57 @@ 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.c.Gauge.Value, float64(n))
|
addFloat64(&(c.n), float64(n))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *prometheusCounter) Dec() {
|
func (c *prometheusCounter) Dec() {
|
||||||
addFloat64(c.c.Gauge.Value, float64(-1))
|
addFloat64(&(c.n), float64(-1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *prometheusCounter) Inc() {
|
func (c *prometheusCounter) Inc() {
|
||||||
addFloat64(c.c.Gauge.Value, float64(1))
|
addFloat64(&(c.n), float64(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *prometheusCounter) Get() uint64 {
|
func (c *prometheusCounter) Get() uint64 {
|
||||||
return uint64(getFloat64(c.c.Gauge.Value))
|
return uint64(getFloat64(&(c.n)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *prometheusCounter) Set(n uint64) {
|
func (c *prometheusCounter) Set(n uint64) {
|
||||||
setFloat64(c.c.Gauge.Value, math.Float64frombits(n))
|
setFloat64(&(c.n), 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.c.Gauge.Value, n)
|
addFloat64(&(c.n), n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *prometheusFloatCounter) Dec() {
|
func (c *prometheusFloatCounter) Dec() {
|
||||||
addFloat64(c.c.Gauge.Value, float64(-1))
|
addFloat64(&(c.n), float64(-1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *prometheusFloatCounter) Inc() {
|
func (c *prometheusFloatCounter) Inc() {
|
||||||
addFloat64(c.c.Gauge.Value, float64(1))
|
addFloat64(&(c.n), float64(1))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *prometheusFloatCounter) Get() float64 {
|
func (c *prometheusFloatCounter) Get() float64 {
|
||||||
return getFloat64(c.c.Gauge.Value)
|
return getFloat64(&(c.n))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *prometheusFloatCounter) Set(n float64) {
|
func (c *prometheusFloatCounter) Set(n float64) {
|
||||||
setFloat64(c.c.Gauge.Value, n)
|
setFloat64(&(c.n), n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *prometheusFloatCounter) Sub(n float64) {
|
func (c *prometheusFloatCounter) Sub(n float64) {
|
||||||
addFloat64(c.c.Gauge.Value, -n)
|
addFloat64(&(c.n), -n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setFloat64(_addr *float64, value float64) float64 {
|
func setFloat64(_addr *float64, value float64) float64 {
|
||||||
|
5
gauge.go
5
gauge.go
@@ -5,8 +5,9 @@ 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.c.Gauge.Value)
|
return getFloat64(&(c.n))
|
||||||
}
|
}
|
||||||
|
196
prometheus.go
196
prometheus.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
@@ -21,23 +22,24 @@ var _ meter.Meter = (*prometheusMeter)(nil)
|
|||||||
type prometheusMeter struct {
|
type prometheusMeter struct {
|
||||||
opts meter.Options
|
opts meter.Options
|
||||||
set prometheus.Registerer
|
set prometheus.Registerer
|
||||||
counter *sync.Map
|
counter map[uint64]*prometheusCounter
|
||||||
floatCounter *sync.Map
|
floatCounter map[uint64]*prometheusFloatCounter
|
||||||
gauge *sync.Map
|
gauge map[uint64]*prometheusGauge
|
||||||
histogram *sync.Map
|
histogram map[uint64]*prometheusHistogram
|
||||||
summary *sync.Map
|
summary map[uint64]*prometheusSummary
|
||||||
mfPool xpool.Pool[*dto.MetricFamily]
|
mfPool xpool.Pool[*dto.MetricFamily]
|
||||||
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMeter(opts ...meter.Option) *prometheusMeter {
|
func NewMeter(opts ...meter.Option) *prometheusMeter {
|
||||||
return &prometheusMeter{
|
return &prometheusMeter{
|
||||||
set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer,
|
set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer,
|
||||||
opts: meter.NewOptions(opts...),
|
opts: meter.NewOptions(opts...),
|
||||||
counter: &sync.Map{},
|
counter: make(map[uint64]*prometheusCounter),
|
||||||
floatCounter: &sync.Map{},
|
floatCounter: make(map[uint64]*prometheusFloatCounter),
|
||||||
gauge: &sync.Map{},
|
gauge: make(map[uint64]*prometheusGauge),
|
||||||
histogram: &sync.Map{},
|
histogram: make(map[uint64]*prometheusHistogram),
|
||||||
summary: &sync.Map{},
|
summary: make(map[uint64]*prometheusSummary),
|
||||||
mfPool: xpool.NewPool[*dto.MetricFamily](func() *dto.MetricFamily {
|
mfPool: xpool.NewPool[*dto.MetricFamily](func() *dto.MetricFamily {
|
||||||
return &dto.MetricFamily{}
|
return &dto.MetricFamily{}
|
||||||
}),
|
}),
|
||||||
@@ -51,69 +53,84 @@ func (m *prometheusMeter) Name() string {
|
|||||||
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
|
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)
|
||||||
mc, ok := m.counter.Load(h)
|
m.mu.Lock()
|
||||||
|
c, ok := m.counter[h]
|
||||||
|
// fmt.Printf("counter name %s hash %v labels %v\n", name, h, labels)
|
||||||
|
m.mu.Unlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
var v float64
|
var n float64
|
||||||
mc = &prometheusCounter{
|
c = &prometheusCounter{
|
||||||
name: name,
|
name: name,
|
||||||
c: &dto.Metric{
|
c: &dto.Metric{
|
||||||
Gauge: &dto.Gauge{Value: &v},
|
Gauge: &dto.Gauge{Value: &n},
|
||||||
Label: labelMetric(clabels),
|
Label: labelMetric(clabels),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
m.counter.Store(h, mc)
|
m.mu.Lock()
|
||||||
|
m.counter[h] = c
|
||||||
|
m.mu.Unlock()
|
||||||
}
|
}
|
||||||
return mc.(*prometheusCounter)
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
mc, ok := m.floatCounter.Load(h)
|
m.mu.Lock()
|
||||||
|
c, ok := m.floatCounter[h]
|
||||||
|
m.mu.Unlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
var v float64
|
var n float64
|
||||||
mc = &prometheusFloatCounter{
|
c = &prometheusFloatCounter{
|
||||||
name: name,
|
name: name,
|
||||||
c: &dto.Metric{
|
c: &dto.Metric{
|
||||||
Gauge: &dto.Gauge{Value: &v},
|
Gauge: &dto.Gauge{Value: &n},
|
||||||
Label: labelMetric(clabels),
|
Label: labelMetric(clabels),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
m.floatCounter.Store(h, mc)
|
m.mu.Lock()
|
||||||
|
m.floatCounter[h] = c
|
||||||
|
m.mu.Unlock()
|
||||||
}
|
}
|
||||||
return mc.(*prometheusFloatCounter)
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
mc, ok := m.gauge.Load(h)
|
m.mu.Lock()
|
||||||
|
c, ok := m.gauge[h]
|
||||||
|
m.mu.Unlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
var v float64
|
var n float64
|
||||||
mc = &prometheusGauge{
|
c = &prometheusGauge{
|
||||||
name: name,
|
name: name,
|
||||||
c: &dto.Metric{
|
c: &dto.Metric{
|
||||||
Gauge: &dto.Gauge{Value: &v},
|
Gauge: &dto.Gauge{Value: &n},
|
||||||
Label: labelMetric(clabels),
|
Label: labelMetric(clabels),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
m.gauge.Store(h, mc)
|
m.mu.Lock()
|
||||||
|
m.gauge[h] = c
|
||||||
|
m.mu.Unlock()
|
||||||
}
|
}
|
||||||
return mc.(*prometheusGauge)
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
mc, ok := m.histogram.Load(h)
|
m.mu.Lock()
|
||||||
|
c, ok := m.histogram[h]
|
||||||
|
m.mu.Unlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
var c uint64
|
var n 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: &c,
|
SampleCount: &n,
|
||||||
SampleSum: &s,
|
SampleSum: &s,
|
||||||
CreatedTimestamp: timestamppb.Now(),
|
CreatedTimestamp: timestamppb.Now(),
|
||||||
Bucket: make([]*dto.Bucket, len(buckets)),
|
Bucket: make([]*dto.Bucket, len(buckets)),
|
||||||
@@ -124,59 +141,68 @@ 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}
|
||||||
}
|
}
|
||||||
mc = &prometheusHistogram{
|
c = &prometheusHistogram{
|
||||||
name: name,
|
name: name,
|
||||||
c: mdto,
|
c: mdto,
|
||||||
}
|
}
|
||||||
|
m.mu.Lock()
|
||||||
m.histogram.Store(h, mc)
|
m.histogram[h] = c
|
||||||
|
m.mu.Unlock()
|
||||||
}
|
}
|
||||||
return mc.(*prometheusHistogram)
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
mc, ok := m.summary.Load(h)
|
m.mu.Lock()
|
||||||
|
c, ok := m.summary[h]
|
||||||
|
m.mu.Unlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
var c uint64
|
var n uint64
|
||||||
var s float64
|
var s float64
|
||||||
mc = &prometheusSummary{
|
c = &prometheusSummary{
|
||||||
name: name,
|
name: name,
|
||||||
c: &dto.Metric{
|
c: &dto.Metric{
|
||||||
Summary: &dto.Summary{
|
Summary: &dto.Summary{
|
||||||
SampleCount: &c,
|
SampleCount: &n,
|
||||||
SampleSum: &s,
|
SampleSum: &s,
|
||||||
CreatedTimestamp: timestamppb.Now(),
|
CreatedTimestamp: timestamppb.Now(),
|
||||||
},
|
},
|
||||||
Label: labelMetric(clabels),
|
Label: labelMetric(clabels),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
m.summary.Store(h, mc)
|
m.mu.Lock()
|
||||||
|
m.summary[h] = c
|
||||||
|
m.mu.Unlock()
|
||||||
}
|
}
|
||||||
return mc.(*prometheusSummary)
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
mc, ok := m.summary.Load(h)
|
m.mu.Lock()
|
||||||
|
c, ok := m.summary[h]
|
||||||
|
m.mu.Lock()
|
||||||
if !ok {
|
if !ok {
|
||||||
var c uint64
|
var n uint64
|
||||||
var s float64
|
var s float64
|
||||||
mc = &prometheusSummary{
|
c = &prometheusSummary{
|
||||||
name: name,
|
name: name,
|
||||||
c: &dto.Metric{
|
c: &dto.Metric{
|
||||||
Summary: &dto.Summary{
|
Summary: &dto.Summary{
|
||||||
SampleCount: &c,
|
SampleCount: &n,
|
||||||
SampleSum: &s,
|
SampleSum: &s,
|
||||||
},
|
},
|
||||||
Label: labelMetric(clabels),
|
Label: labelMetric(clabels),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
m.summary.Store(h, mc)
|
m.mu.Lock()
|
||||||
|
m.summary[h] = c
|
||||||
|
m.mu.Unlock()
|
||||||
}
|
}
|
||||||
return mc.(*prometheusSummary)
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *prometheusMeter) Init(opts ...meter.Option) error {
|
func (m *prometheusMeter) Init(opts ...meter.Option) error {
|
||||||
@@ -212,55 +238,59 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
|
|||||||
|
|
||||||
enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain))
|
enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain))
|
||||||
|
|
||||||
m.counter.Range(func(k, v any) bool {
|
m.mu.Lock()
|
||||||
c := v.(*prometheusCounter)
|
|
||||||
mf := m.mfPool.Get()
|
|
||||||
mf.Name = &c.name
|
|
||||||
mf.Type = dto.MetricType_GAUGE.Enum()
|
|
||||||
mf.Metric = append(mf.Metric, c.c)
|
|
||||||
mfs = append(mfs, mf)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
m.floatCounter.Range(func(k, v any) bool {
|
for _, mc := range m.counter {
|
||||||
c := v.(*prometheusFloatCounter)
|
|
||||||
mf := m.mfPool.Get()
|
mf := m.mfPool.Get()
|
||||||
mf.Name = &c.name
|
mf.Name = &mc.name
|
||||||
mf.Type = dto.MetricType_GAUGE.Enum()
|
mf.Type = dto.MetricType_GAUGE.Enum()
|
||||||
mf.Metric = append(mf.Metric, c.c)
|
n := getFloat64(&(mc.n))
|
||||||
|
mc.c.Gauge.Value = &n
|
||||||
|
mf.Metric = append(mf.Metric, mc.c)
|
||||||
mfs = append(mfs, mf)
|
mfs = append(mfs, mf)
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
|
|
||||||
m.gauge.Range(func(k, v any) bool {
|
for _, mc := range m.floatCounter {
|
||||||
c := v.(*prometheusGauge)
|
|
||||||
mf := m.mfPool.Get()
|
mf := m.mfPool.Get()
|
||||||
mf.Name = &c.name
|
mf.Name = &mc.name
|
||||||
mf.Type = dto.MetricType_GAUGE.Enum()
|
mf.Type = dto.MetricType_GAUGE.Enum()
|
||||||
mf.Metric = append(mf.Metric, c.c)
|
n := getFloat64(&(mc.n))
|
||||||
|
mc.c.Gauge.Value = &n
|
||||||
|
mf.Metric = append(mf.Metric, mc.c)
|
||||||
mfs = append(mfs, mf)
|
mfs = append(mfs, mf)
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
|
|
||||||
m.histogram.Range(func(k, v any) bool {
|
for _, mc := range m.gauge {
|
||||||
c := v.(*prometheusHistogram)
|
mf := m.mfPool.Get()
|
||||||
|
mf.Name = &mc.name
|
||||||
|
mf.Type = dto.MetricType_GAUGE.Enum()
|
||||||
|
n := getFloat64(&(mc.n))
|
||||||
|
mc.c.Gauge.Value = &n
|
||||||
|
mf.Metric = append(mf.Metric, mc.c)
|
||||||
|
mfs = append(mfs, mf)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range m.histogram {
|
||||||
mf := m.mfPool.Get()
|
mf := m.mfPool.Get()
|
||||||
mf.Name = &c.name
|
mf.Name = &c.name
|
||||||
mf.Type = dto.MetricType_HISTOGRAM.Enum()
|
mf.Type = dto.MetricType_HISTOGRAM.Enum()
|
||||||
mf.Metric = append(mf.Metric, c.c)
|
mf.Metric = append(mf.Metric, c.c)
|
||||||
mfs = append(mfs, mf)
|
mfs = append(mfs, mf)
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
|
|
||||||
m.summary.Range(func(k, v any) bool {
|
for _, mc := range m.summary {
|
||||||
c := v.(*prometheusSummary)
|
|
||||||
mf := m.mfPool.Get()
|
mf := m.mfPool.Get()
|
||||||
mf.Name = &c.name
|
mf.Name = &mc.name
|
||||||
mf.Type = dto.MetricType_SUMMARY.Enum()
|
mf.Type = dto.MetricType_SUMMARY.Enum()
|
||||||
mf.Metric = append(mf.Metric, c.c)
|
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)
|
||||||
mfs = append(mfs, mf)
|
mfs = append(mfs, mf)
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
|
m.mu.Unlock()
|
||||||
|
|
||||||
for _, mf := range mfs {
|
for _, mf := range mfs {
|
||||||
_ = enc.Encode(mf)
|
_ = enc.Encode(mf)
|
||||||
@@ -310,11 +340,13 @@ func (m *prometheusMeter) Set(opts ...meter.Option) meter.Meter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func labelMetric(labels []string) []*dto.LabelPair {
|
func labelMetric(labels []string) []*dto.LabelPair {
|
||||||
dtoLabels := make([]*dto.LabelPair, 0, len(labels)/2)
|
nl := make([]string, len(labels))
|
||||||
for idx := 0; idx < len(labels); idx += 2 {
|
copy(nl, labels)
|
||||||
|
dtoLabels := make([]*dto.LabelPair, 0, len(nl)/2)
|
||||||
|
for idx := 0; idx < len(nl); idx += 2 {
|
||||||
dtoLabels = append(dtoLabels, &dto.LabelPair{
|
dtoLabels = append(dtoLabels, &dto.LabelPair{
|
||||||
Name: &(labels[idx]),
|
Name: &(nl[idx]),
|
||||||
Value: &(labels[idx+1]),
|
Value: &(nl[idx+1]),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return dtoLabels
|
return dtoLabels
|
||||||
|
@@ -14,6 +14,28 @@ import (
|
|||||||
"go.unistack.org/micro/v3/meter"
|
"go.unistack.org/micro/v3/meter"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
|
||||||
func TestHistogram(t *testing.T) {
|
func TestHistogram(t *testing.T) {
|
||||||
m := NewMeter()
|
m := NewMeter()
|
||||||
name := "test"
|
name := "test"
|
||||||
@@ -56,6 +78,7 @@ func TestHistogram(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSummary(t *testing.T) {
|
func TestSummary(t *testing.T) {
|
||||||
|
t.Skip()
|
||||||
name := "micro_server"
|
name := "micro_server"
|
||||||
m := NewMeter()
|
m := NewMeter()
|
||||||
m.Summary("micro_server").Update(1)
|
m.Summary("micro_server").Update(1)
|
||||||
@@ -87,7 +110,7 @@ func TestSummary(t *testing.T) {
|
|||||||
p.Observe(10)
|
p.Observe(10)
|
||||||
p.Observe(30)
|
p.Observe(30)
|
||||||
mdto := &dto.Metric{}
|
mdto := &dto.Metric{}
|
||||||
p.Write(mdto)
|
_ = p.Write(mdto)
|
||||||
pbuf := bytes.NewBuffer(nil)
|
pbuf := bytes.NewBuffer(nil)
|
||||||
enc := expfmt.NewEncoder(pbuf, expfmt.NewFormat(expfmt.TypeTextPlain))
|
enc := expfmt.NewEncoder(pbuf, expfmt.NewFormat(expfmt.TypeTextPlain))
|
||||||
mf := &dto.MetricFamily{Name: &name, Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{mdto}}
|
mf := &dto.MetricFamily{Name: &name, Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{mdto}}
|
||||||
|
16
summary.go
16
summary.go
@@ -10,15 +10,17 @@ 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.c.Summary.SampleCount, 1)
|
atomic.AddUint64(&(c.sampleCount), 1)
|
||||||
addFloat64(c.c.Summary.SampleSum, n)
|
addFloat64(&(c.SampleSum), n)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c prometheusSummary) UpdateDuration(n time.Time) {
|
func (c *prometheusSummary) UpdateDuration(t time.Time) {
|
||||||
x := time.Since(n).Seconds()
|
n := time.Since(t).Seconds()
|
||||||
atomic.AddUint64(c.c.Summary.SampleCount, 1)
|
atomic.AddUint64(&(c.sampleCount), 1)
|
||||||
addFloat64(c.c.Summary.SampleSum, x)
|
addFloat64(&(c.SampleSum), n)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user