metric 생성자 수정 - pipe 추가
This commit is contained in:
@ -2,13 +2,12 @@ package metric
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
@ -16,8 +15,6 @@ import (
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
)
|
||||
|
||||
const metric_value_line_size = 19
|
||||
|
||||
type MetricDescription struct {
|
||||
Key string
|
||||
Type MetricType
|
||||
@ -31,6 +28,32 @@ type Exporter interface {
|
||||
UpdateMetric(string, float64)
|
||||
}
|
||||
|
||||
type MetricPipe struct {
|
||||
pipe *os.File
|
||||
}
|
||||
|
||||
func (mp MetricPipe) Close() {
|
||||
mp.pipe.Close()
|
||||
}
|
||||
|
||||
func (mp MetricPipe) writeLine(line string) {
|
||||
mp.pipe.WriteString(line + "\n")
|
||||
}
|
||||
|
||||
func NewMetricPipe(pipeName string) MetricPipe {
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
pipeName = "/tmp/" + pipeName
|
||||
case "windows":
|
||||
pipeName = `\\.\pipe\` + pipeName
|
||||
}
|
||||
|
||||
f, _ := os.Open(pipeName)
|
||||
return MetricPipe{
|
||||
pipe: f,
|
||||
}
|
||||
}
|
||||
|
||||
type MetricWriter interface {
|
||||
Add(int64)
|
||||
Set(int64)
|
||||
@ -44,13 +67,14 @@ func (mw *metric_empty) Add(int64) {}
|
||||
var MetricWriterNil = MetricWriter(&metric_empty{})
|
||||
|
||||
type metric_int64 struct {
|
||||
key string
|
||||
valptr *int64
|
||||
buff [metric_value_line_size]byte
|
||||
pipe MetricPipe
|
||||
}
|
||||
|
||||
func (mw *metric_int64) printOut() {
|
||||
binary.LittleEndian.PutUint64(mw.buff[9:], math.Float64bits(float64(atomic.LoadInt64(mw.valptr))))
|
||||
os.Stdout.Write(mw.buff[:])
|
||||
loaded := atomic.LoadInt64(mw.valptr)
|
||||
mw.pipe.writeLine(fmt.Sprintf("%s:%d", mw.key, loaded))
|
||||
}
|
||||
|
||||
func (mw *metric_int64) Set(newval int64) {
|
||||
@ -63,11 +87,16 @@ func (mw *metric_int64) Add(inc int64) {
|
||||
mw.printOut()
|
||||
}
|
||||
|
||||
func NewMetric(mt MetricType, name string, help string, constLabels map[string]string) (writer MetricWriter) {
|
||||
func NewMetric(pipe MetricPipe, mt MetricType, name string, help string, constLabels map[string]string) (writer MetricWriter) {
|
||||
if !metricEnabled {
|
||||
return MetricWriterNil
|
||||
}
|
||||
|
||||
if constLabels == nil {
|
||||
constLabels = map[string]string{}
|
||||
}
|
||||
|
||||
constLabels["pid"] = fmt.Sprintf("%d", os.Getpid())
|
||||
var disorder []struct {
|
||||
k string
|
||||
v string
|
||||
@ -100,18 +129,12 @@ func NewMetric(mt MetricType, name string, help string, constLabels map[string]s
|
||||
})
|
||||
|
||||
impl := &metric_int64{
|
||||
key: key,
|
||||
valptr: new(int64),
|
||||
pipe: pipe,
|
||||
}
|
||||
|
||||
impl.buff[0] = METRIC_HEAD_INLINE
|
||||
impl.buff[17] = METRIC_TAIL_INLINE
|
||||
impl.buff[18] = '\n'
|
||||
copy(impl.buff[1:], []byte(key))
|
||||
|
||||
output := append([]byte{METRIC_HEAD_INLINE}, temp...)
|
||||
output = append(output, METRIC_TAIL_INLINE, '\n')
|
||||
os.Stdout.Write(output)
|
||||
|
||||
pipe.writeLine(string(temp))
|
||||
// writer
|
||||
|
||||
return impl
|
||||
|
||||
Reference in New Issue
Block a user