diff --git a/metric/metric.go b/metric/metric.go index 1893a3e..f25d7a5 100644 --- a/metric/metric.go +++ b/metric/metric.go @@ -5,9 +5,11 @@ import ( "encoding/binary" "encoding/hex" "encoding/json" + "fmt" "math" "os" - "sync" + + "repositories.action2quare.com/ayo/gocommon/logger" ) 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) { - metricWriterFlag.Do(func() { - go mc.metricWriter() - }) +func init() { + NewMetric = func(MetricType, string, string, map[string]string) 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.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] temp, _ := json.Marshal(MetricDescription{ Key: key,