metric 기본 타입을 int64로
This commit is contained in:
@ -8,6 +8,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
)
|
||||
@ -35,7 +36,7 @@ type MetricDescription struct {
|
||||
|
||||
type writeRequest struct {
|
||||
key string
|
||||
val float64
|
||||
valfunc func() float64
|
||||
}
|
||||
|
||||
type metricCollection struct {
|
||||
@ -46,7 +47,38 @@ var mc = metricCollection{
|
||||
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() {
|
||||
// head + metric_key_size + 8byte + tail + cr = 19
|
||||
@ -57,7 +89,7 @@ func (mc *metricCollection) metricWriter() {
|
||||
|
||||
for req := range mc.writerChan {
|
||||
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[:])
|
||||
}
|
||||
}
|
||||
@ -66,7 +98,7 @@ var NewMetric func(MetricType, string, string, map[string]string) MetricWriter
|
||||
|
||||
func init() {
|
||||
NewMetric = func(MetricType, string, string, map[string]string) MetricWriter {
|
||||
return func(val float64) {}
|
||||
return &metric_empty{}
|
||||
}
|
||||
|
||||
ppid := os.Getppid()
|
||||
@ -101,11 +133,9 @@ func newMetricImpl(mt MetricType, name string, help string, constLabels map[stri
|
||||
ConstLabels: constLabels,
|
||||
})
|
||||
|
||||
writer = func(val float64) {
|
||||
mc.writerChan <- &writeRequest{
|
||||
writer = &metric_int64{
|
||||
key: key,
|
||||
val: val,
|
||||
}
|
||||
writerChan: mc.writerChan,
|
||||
}
|
||||
|
||||
output := append([]byte{METRIC_HEAD_INLINE}, temp...)
|
||||
|
||||
Reference in New Issue
Block a user