메트릭 생성 파라미터 변경

This commit is contained in:
2023-11-20 13:49:29 +09:00
parent 72a62b678f
commit 6c5a82cc9b

View File

@ -5,9 +5,11 @@ import (
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt"
"math" "math"
"os" "os"
"sync"
"repositories.action2quare.com/ayo/gocommon/logger"
) )
const ( const (
@ -60,30 +62,36 @@ func (mc *metricCollection) metricWriter() {
} }
} }
var metricWriterFlag sync.Once var NewMetric func(MetricType, string, string, map[string]string) MetricWriter
func NewMetric(mt MetricType, name string, help string, constLabel ...string) (writer MetricWriter) { func init() {
metricWriterFlag.Do(func() { NewMetric = func(MetricType, string, string, map[string]string) MetricWriter {
go mc.metricWriter() return func(val float64) {}
}) }
ppid := os.Getppid()
if parent, _ := os.FindProcess(ppid); parent != nil {
filename := fmt.Sprintf(`/proc/%d/stat`, os.Getppid())
if fn, err := os.ReadFile(filename); err == nil {
if string(fn) == "houston" {
logger.Println("metrics are activated for houston")
go mc.metricWriter()
NewMetric = newMetricImpl
} else {
logger.Println("metrics are NOT activated")
}
} else {
logger.Println("metrics are NOT activated")
}
} else {
logger.Println("metrics are NOT activated")
}
}
func newMetricImpl(mt MetricType, name string, help string, constLabels map[string]string) (writer MetricWriter) {
hash := md5.New() hash := md5.New()
hash.Write([]byte(name)) hash.Write([]byte(name))
var constLabels map[string]string
if len(constLabel) > 0 {
constLabels = make(map[string]string)
for i := 0; i < len(constLabel); i = i + 2 {
constLabels[constLabel[i]] = ""
hash.Write([]byte(constLabel[i]))
}
for i := 1; i < len(constLabel); i = i + 2 {
constLabels[constLabel[i-1]] = constLabel[i]
hash.Write([]byte(constLabel[i]))
}
}
key := hex.EncodeToString(hash.Sum(nil))[:metric_key_size] key := hex.EncodeToString(hash.Sum(nil))[:metric_key_size]
temp, _ := json.Marshal(MetricDescription{ temp, _ := json.Marshal(MetricDescription{
Key: key, Key: key,