2024-10-06 13:45:33 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
"sync/atomic"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
)
|
|
|
|
|
|
|
|
type prometheusCounter struct {
|
|
|
|
name string
|
|
|
|
c *dto.Metric
|
2024-11-08 15:27:05 +03:00
|
|
|
n float64
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusCounter) Add(n int) {
|
2024-11-08 15:27:05 +03:00
|
|
|
addFloat64(&(c.n), float64(n))
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusCounter) Dec() {
|
2024-11-08 15:27:05 +03:00
|
|
|
addFloat64(&(c.n), float64(-1))
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusCounter) Inc() {
|
2024-11-08 15:27:05 +03:00
|
|
|
addFloat64(&(c.n), float64(1))
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusCounter) Get() uint64 {
|
2024-11-08 15:27:05 +03:00
|
|
|
return uint64(getFloat64(&(c.n)))
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusCounter) Set(n uint64) {
|
2024-11-08 15:27:05 +03:00
|
|
|
setFloat64(&(c.n), math.Float64frombits(n))
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type prometheusFloatCounter struct {
|
|
|
|
name string
|
|
|
|
c *dto.Metric
|
2024-11-08 15:27:05 +03:00
|
|
|
n float64
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusFloatCounter) Add(n float64) {
|
2024-11-08 15:27:05 +03:00
|
|
|
addFloat64(&(c.n), n)
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusFloatCounter) Dec() {
|
2024-11-08 15:27:05 +03:00
|
|
|
addFloat64(&(c.n), float64(-1))
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusFloatCounter) Inc() {
|
2024-11-08 15:27:05 +03:00
|
|
|
addFloat64(&(c.n), float64(1))
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusFloatCounter) Get() float64 {
|
2024-11-08 15:27:05 +03:00
|
|
|
return getFloat64(&(c.n))
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusFloatCounter) Set(n float64) {
|
2024-11-08 15:27:05 +03:00
|
|
|
setFloat64(&(c.n), n)
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *prometheusFloatCounter) Sub(n float64) {
|
2024-11-08 15:27:05 +03:00
|
|
|
addFloat64(&(c.n), -n)
|
2024-10-06 13:45:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func setFloat64(_addr *float64, value float64) float64 {
|
|
|
|
addr := (*uint64)(unsafe.Pointer(_addr))
|
|
|
|
for {
|
|
|
|
x := atomic.LoadUint64(addr)
|
|
|
|
if atomic.CompareAndSwapUint64(addr, x, math.Float64bits(value)) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFloat64(_addr *float64, delta float64) float64 {
|
|
|
|
addr := (*uint64)(unsafe.Pointer(_addr))
|
|
|
|
for {
|
|
|
|
x := atomic.LoadUint64(addr)
|
|
|
|
y := math.Float64frombits(x) + delta
|
|
|
|
if atomic.CompareAndSwapUint64(addr, x, math.Float64bits(y)) {
|
|
|
|
return y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFloat64(_addr *float64) float64 {
|
|
|
|
addr := (*uint64)(unsafe.Pointer(_addr))
|
|
|
|
x := atomic.LoadUint64(addr)
|
|
|
|
y := math.Float64frombits(x)
|
|
|
|
return y
|
|
|
|
}
|