From 8b4b5a4b98946cf067c992fde0e96e05ffb0fb67 Mon Sep 17 00:00:00 2001 From: SuperQ Date: Mon, 10 Jan 2022 11:26:17 +0100 Subject: [PATCH] Add option to disable STATS SETTINGS In order to support mcrouter, add a flag to disable the collection of "STATS SETTINGS". Fixes: https://github.com/prometheus/memcached_exporter/issues/113 Signed-off-by: SuperQ --- README.md | 8 ++++++++ cmd/memcached_exporter/main.go | 3 ++- pkg/exporter/exporter.go | 28 +++++++++++++++++----------- pkg/exporter/exporter_test.go | 4 ++-- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e4e5be1..31aa84e 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,14 @@ Alternatively a Dockerfile is supplied: docker run -p 9150:9150 quay.io/prometheus/memcached-exporter:latest ``` +## mcrouter + +In order to use the memcached_exporter with [mcrouter](https://github.com/facebook/mcrouter) the "STATS SETTINGS" collection needs to be disabled. + +```sh +./memcached_exporter --no.memcached.stats.settings +``` + ## Collectors The exporter collects a number of statistics from the server: diff --git a/cmd/memcached_exporter/main.go b/cmd/memcached_exporter/main.go index 798b18a..05549c3 100644 --- a/cmd/memcached_exporter/main.go +++ b/cmd/memcached_exporter/main.go @@ -36,6 +36,7 @@ func main() { address = kingpin.Flag("memcached.address", "Memcached server address.").Default("localhost:11211").String() timeout = kingpin.Flag("memcached.timeout", "memcached connect timeout.").Default("1s").Duration() pidFile = kingpin.Flag("memcached.pid-file", "Optional path to a file containing the memcached PID for additional metrics.").Default("").String() + getSettings = kingpin.Flag("memcached.stats.settings", "Enable collection of STATS SETTINGS.").Default("true").Bool() webConfig = webflag.AddFlags(kingpin.CommandLine) listenAddress = kingpin.Flag("web.listen-address", "Address to listen on for web interface and telemetry.").Default(":9150").String() metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String() @@ -51,7 +52,7 @@ func main() { level.Info(logger).Log("msg", "Build context", "context", version.BuildContext()) prometheus.MustRegister(version.NewCollector("memcached_exporter")) - prometheus.MustRegister(exporter.New(*address, *timeout, logger)) + prometheus.MustRegister(exporter.New(*address, *timeout, *getSettings, logger)) if *pidFile != "" { procExporter := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{ diff --git a/pkg/exporter/exporter.go b/pkg/exporter/exporter.go index 3421dcc..7fb5c70 100644 --- a/pkg/exporter/exporter.go +++ b/pkg/exporter/exporter.go @@ -40,6 +40,8 @@ type Exporter struct { timeout time.Duration logger log.Logger + getSettings bool + up *prometheus.Desc uptime *prometheus.Desc time *prometheus.Desc @@ -109,11 +111,12 @@ type Exporter struct { } // New returns an initialized exporter. -func New(server string, timeout time.Duration, logger log.Logger) *Exporter { +func New(server string, timeout time.Duration, getSettings bool, logger log.Logger) *Exporter { return &Exporter{ - address: server, - timeout: timeout, - logger: logger, + address: server, + timeout: timeout, + logger: logger, + getSettings: getSettings, up: prometheus.NewDesc( prometheus.BuildFQName(Namespace, "", "up"), "Could the memcached server be reached.", @@ -602,17 +605,20 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) { level.Error(e.logger).Log("msg", "Failed to collect stats from memcached", "err", err) up = 0 } - statsSettings, err := c.StatsSettings() - if err != nil { - level.Error(e.logger).Log("msg", "Could not query stats settings", "err", err) - up = 0 - } if err := e.parseStats(ch, stats); err != nil { up = 0 } - if err := e.parseStatsSettings(ch, statsSettings); err != nil { - up = 0 + + if e.getSettings { + statsSettings, err := c.StatsSettings() + if err != nil { + level.Error(e.logger).Log("msg", "Could not query stats settings", "err", err) + up = 0 + } + if err := e.parseStatsSettings(ch, statsSettings); err != nil { + up = 0 + } } ch <- prometheus.MustNewConstMetric(e.up, prometheus.GaugeValue, up) diff --git a/pkg/exporter/exporter_test.go b/pkg/exporter/exporter_test.go index c9ef682..60b8f73 100644 --- a/pkg/exporter/exporter_test.go +++ b/pkg/exporter/exporter_test.go @@ -45,7 +45,7 @@ func TestParseStatsSettings(t *testing.T) { }, } ch := make(chan prometheus.Metric, 100) - e := New("", 100*time.Millisecond, log.NewNopLogger()) + e := New("", 100*time.Millisecond, true, log.NewNopLogger()) if err := e.parseStatsSettings(ch, statsSettings); err != nil { t.Errorf("expect return error, error: %v", err) } @@ -67,7 +67,7 @@ func TestParseStatsSettings(t *testing.T) { }, } ch := make(chan prometheus.Metric, 100) - e := New("", 100*time.Millisecond, log.NewNopLogger()) + e := New("", 100*time.Millisecond, true, log.NewNopLogger()) if err := e.parseStatsSettings(ch, statsSettings); err == nil { t.Error("expect return error but not") }