metric 기본 타입을 int64로

This commit is contained in:
2023-11-20 14:12:44 +09:00
parent 6c5a82cc9b
commit a5d9809db7

View File

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"math" "math"
"os" "os"
"sync/atomic"
"repositories.action2quare.com/ayo/gocommon/logger" "repositories.action2quare.com/ayo/gocommon/logger"
) )
@ -34,8 +35,8 @@ type MetricDescription struct {
} }
type writeRequest struct { type writeRequest struct {
key string key string
val float64 valfunc func() float64
} }
type metricCollection struct { type metricCollection struct {
@ -46,7 +47,38 @@ var mc = metricCollection{
writerChan: make(chan *writeRequest, 100), writerChan: make(chan *writeRequest, 100),
} }
type MetricWriter func(float64) type MetricWriter interface {
Add(int64)
Set(int64)
}
type metric_empty struct{}
func (mw *metric_empty) Set(newval int64) {}
func (mw *metric_empty) Add(inc int64) {}
type metric_int64 struct {
valptr *int64
key string
writerChan chan *writeRequest
}
func (mw *metric_int64) requestMetricWrite() {
mw.writerChan <- &writeRequest{
key: mw.key,
valfunc: func() float64 { return float64(atomic.LoadInt64(mw.valptr)) },
}
}
func (mw *metric_int64) Set(newval int64) {
atomic.StoreInt64(mw.valptr, newval)
mw.requestMetricWrite()
}
func (mw *metric_int64) Add(inc int64) {
atomic.AddInt64(mw.valptr, inc)
mw.requestMetricWrite()
}
func (mc *metricCollection) metricWriter() { func (mc *metricCollection) metricWriter() {
// head + metric_key_size + 8byte + tail + cr = 19 // head + metric_key_size + 8byte + tail + cr = 19
@ -57,7 +89,7 @@ func (mc *metricCollection) metricWriter() {
for req := range mc.writerChan { for req := range mc.writerChan {
copy(buff[1:], []byte(req.key)) copy(buff[1:], []byte(req.key))
binary.BigEndian.PutUint64(buff[9:], math.Float64bits(req.val)) binary.BigEndian.PutUint64(buff[9:], math.Float64bits(req.valfunc()))
os.Stdout.Write(buff[:]) os.Stdout.Write(buff[:])
} }
} }
@ -66,7 +98,7 @@ var NewMetric func(MetricType, string, string, map[string]string) MetricWriter
func init() { func init() {
NewMetric = func(MetricType, string, string, map[string]string) MetricWriter { NewMetric = func(MetricType, string, string, map[string]string) MetricWriter {
return func(val float64) {} return &metric_empty{}
} }
ppid := os.Getppid() ppid := os.Getppid()
@ -101,11 +133,9 @@ func newMetricImpl(mt MetricType, name string, help string, constLabels map[stri
ConstLabels: constLabels, ConstLabels: constLabels,
}) })
writer = func(val float64) { writer = &metric_int64{
mc.writerChan <- &writeRequest{ key: key,
key: key, writerChan: mc.writerChan,
val: val,
}
} }
output := append([]byte{METRIC_HEAD_INLINE}, temp...) output := append([]byte{METRIC_HEAD_INLINE}, temp...)