From 9ab7a6424bb06100f5cedf92884c206e7aaff725 Mon Sep 17 00:00:00 2001 From: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Date: Fri, 4 Feb 2022 19:22:31 +0100 Subject: [PATCH] Moved check which metric to skip to Init() --- collectors/loadavgMetric.go | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/collectors/loadavgMetric.go b/collectors/loadavgMetric.go index a337d9b..3859721 100644 --- a/collectors/loadavgMetric.go +++ b/collectors/loadavgMetric.go @@ -22,16 +22,16 @@ import ( // const LOADAVGFILE = "/proc/loadavg" -type LoadavgCollectorConfig struct { - ExcludeMetrics []string `json:"exclude_metrics,omitempty"` -} - type LoadavgCollector struct { metricCollector tags map[string]string load_matches []string + load_skips []bool proc_matches []string - config LoadavgCollectorConfig + proc_skips []bool + config struct { + ExcludeMetrics []string `json:"exclude_metrics,omitempty"` + } } func (m *LoadavgCollector) Init(config json.RawMessage) error { @@ -51,15 +51,23 @@ func (m *LoadavgCollector) Init(config json.RawMessage) error { "load_one", "load_five", "load_fifteen"} + m.load_skips = make([]bool, len(m.load_matches)) m.proc_matches = []string{ "proc_run", "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 return nil } func (m *LoadavgCollector) Read(interval time.Duration, output chan lp.CCMetric) { - var skip bool if !m.init { 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)) 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) - if err == nil && !skip { + if err == nil { output <- y } } @@ -94,16 +104,18 @@ func (m *LoadavgCollector) Read(interval time.Duration, output chan lp.CCMetric) // Process metrics lv := strings.Split(ls[3], `/`) 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 { cclog.ComponentError( m.name, fmt.Sprintf("Read(): Failed to convert '%s' to float64: %v", lv[i], err)) 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) - if err == nil && !skip { + if err == nil { output <- y }