Skip to content

Commit

Permalink
Add option to disable STATS SETTINGS
Browse files Browse the repository at this point in the history
In order to support mcrouter, add a flag to disable the collection of
"STATS SETTINGS".

Fixes: #113

Signed-off-by: SuperQ <[email protected]>
  • Loading branch information
SuperQ committed Jan 10, 2022
1 parent 2458f73 commit 8b4b5a4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion cmd/memcached_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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{
Expand Down
28 changes: 17 additions & 11 deletions pkg/exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Exporter struct {
timeout time.Duration
logger log.Logger

getSettings bool

up *prometheus.Desc
uptime *prometheus.Desc
time *prometheus.Desc
Expand Down Expand Up @@ -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.",
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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")
}
Expand Down

0 comments on commit 8b4b5a4

Please sign in to comment.