Conntrack update: differentiate ErrNoData and other errors (#1323, #1723)

Signed-off-by: Li Ling <liiling@google.com>
This commit is contained in:
Li Ling 2020-08-26 14:35:39 +02:00
parent a26ffaf4f8
commit 875f437fcc
1 changed files with 15 additions and 3 deletions

View File

@ -16,7 +16,12 @@
package collector
import (
"errors"
"fmt"
"os"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
@ -50,18 +55,25 @@ func NewConntrackCollector(logger log.Logger) (Collector, error) {
func (c *conntrackCollector) Update(ch chan<- prometheus.Metric) error {
value, err := readUintFromFile(procFilePath("sys/net/netfilter/nf_conntrack_count"))
if err != nil {
// Conntrack probably not loaded into the kernel.
return err
return c.handleErr(err)
}
ch <- prometheus.MustNewConstMetric(
c.current, prometheus.GaugeValue, float64(value))
value, err = readUintFromFile(procFilePath("sys/net/netfilter/nf_conntrack_max"))
if err != nil {
return err
return c.handleErr(err)
}
ch <- prometheus.MustNewConstMetric(
c.limit, prometheus.GaugeValue, float64(value))
return nil
}
func (c *conntrackCollector) handleErr(err error) error {
if errors.Is(err, os.ErrNotExist) {
level.Debug(c.logger).Log("msg", "conntrack probably not loaded")
return ErrNoData
}
return fmt.Errorf("failed to retrieve conntrack stats: %w", err)
}