Moved check which metric to skip to Init()

This commit is contained in:
Holger Obermaier 2022-02-04 19:22:31 +01:00
parent fdb58b0be2
commit 9ab7a6424b

View File

@ -22,16 +22,16 @@ import (
// //
const LOADAVGFILE = "/proc/loadavg" const LOADAVGFILE = "/proc/loadavg"
type LoadavgCollectorConfig struct {
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
}
type LoadavgCollector struct { type LoadavgCollector struct {
metricCollector metricCollector
tags map[string]string tags map[string]string
load_matches []string load_matches []string
load_skips []bool
proc_matches []string proc_matches []string
config LoadavgCollectorConfig proc_skips []bool
config struct {
ExcludeMetrics []string `json:"exclude_metrics,omitempty"`
}
} }
func (m *LoadavgCollector) Init(config json.RawMessage) error { func (m *LoadavgCollector) Init(config json.RawMessage) error {
@ -51,15 +51,23 @@ func (m *LoadavgCollector) Init(config json.RawMessage) error {
"load_one", "load_one",
"load_five", "load_five",
"load_fifteen"} "load_fifteen"}
m.load_skips = make([]bool, len(m.load_matches))
m.proc_matches = []string{ m.proc_matches = []string{
"proc_run", "proc_run",
"proc_total"} "proc_total"}
m.proc_skips = make([]bool, len(m.proc_matches))
for i, name := range m.load_matches {
_, m.load_skips[i] = stringArrayContains(m.config.ExcludeMetrics, name)
}
for i, name := range m.proc_matches {
_, m.proc_skips[i] = stringArrayContains(m.config.ExcludeMetrics, name)
}
m.init = true m.init = true
return nil return nil
} }
func (m *LoadavgCollector) Read(interval time.Duration, output chan lp.CCMetric) { func (m *LoadavgCollector) Read(interval time.Duration, output chan lp.CCMetric) {
var skip bool
if !m.init { if !m.init {
return return
} }
@ -84,9 +92,11 @@ func (m *LoadavgCollector) Read(interval time.Duration, output chan lp.CCMetric)
fmt.Sprintf("Read(): Failed to convert '%s' to float64: %v", ls[i], err)) fmt.Sprintf("Read(): Failed to convert '%s' to float64: %v", ls[i], err))
continue continue
} }
_, skip = stringArrayContains(m.config.ExcludeMetrics, name) if m.load_skips[i] {
continue
}
y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": x}, now) y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": x}, now)
if err == nil && !skip { if err == nil {
output <- y output <- y
} }
} }
@ -94,16 +104,18 @@ func (m *LoadavgCollector) Read(interval time.Duration, output chan lp.CCMetric)
// Process metrics // Process metrics
lv := strings.Split(ls[3], `/`) lv := strings.Split(ls[3], `/`)
for i, name := range m.proc_matches { for i, name := range m.proc_matches {
x, err := strconv.ParseFloat(lv[i], 64) x, err := strconv.ParseInt(lv[i], 10, 64)
if err != nil { if err != nil {
cclog.ComponentError( cclog.ComponentError(
m.name, m.name,
fmt.Sprintf("Read(): Failed to convert '%s' to float64: %v", lv[i], err)) fmt.Sprintf("Read(): Failed to convert '%s' to float64: %v", lv[i], err))
continue continue
} }
_, skip = stringArrayContains(m.config.ExcludeMetrics, name) if m.proc_skips[i] {
continue
}
y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": x}, now) y, err := lp.New(name, m.tags, m.meta, map[string]interface{}{"value": x}, now)
if err == nil && !skip { if err == nil {
output <- y output <- y
} }