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