Conntrack update: differentiate ErrNoData and other errors
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)
+}
