micro-meter-prometheus/histogram.go
Vasiliy Tolstov da9201efff
Some checks failed
codeql / analyze (go) (push) Failing after 35s
build / test (push) Failing after 4m53s
build / lint (push) Successful in 9m30s
lower memory usage
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-10-06 13:45:33 +03:00

40 lines
784 B
Go

package prometheus
import (
"sync/atomic"
"time"
dto "github.com/prometheus/client_model/go"
)
type prometheusHistogram struct {
name string
c *dto.Metric
}
func (c prometheusHistogram) Reset() {
}
func (c prometheusHistogram) Update(n float64) {
atomic.AddUint64(c.c.Histogram.SampleCount, 1)
addFloat64(c.c.Histogram.SampleSum, n)
for _, b := range c.c.Histogram.Bucket {
if n > *b.UpperBound {
continue
}
atomic.AddUint64(b.CumulativeCount, 1)
}
}
func (c prometheusHistogram) UpdateDuration(n time.Time) {
x := time.Since(n).Seconds()
atomic.AddUint64(c.c.Histogram.SampleCount, 1)
addFloat64(c.c.Histogram.SampleSum, x)
for _, b := range c.c.Histogram.Bucket {
if x > *b.UpperBound {
continue
}
atomic.AddUint64(b.CumulativeCount, 1)
}
}