diff --git a/collector/conntrack_linux.go b/collector/conntrack_linux.go index 8b60018d..9c8f17d2 100644 --- a/collector/conntrack_linux.go +++ b/collector/conntrack_linux.go @@ -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) +}