From 6a65e1b2627bb8fd296bfe881a3d7290431fa3eb Mon Sep 17 00:00:00 2001 From: Kyle Brandt Date: Mon, 7 Mar 2016 12:46:49 -0500 Subject: [PATCH] cmd/scollector: Make MaxMem kill switch configurable --- cmd/scollector/conf/conf.go | 5 +++++ cmd/scollector/main.go | 13 ++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/scollector/conf/conf.go b/cmd/scollector/conf/conf.go index 74a6c4b6f7..5aa8c3fe9e 100644 --- a/cmd/scollector/conf/conf.go +++ b/cmd/scollector/conf/conf.go @@ -25,6 +25,11 @@ type Conf struct { BatchSize int // MaxQueueLen is the number of metrics keept internally. MaxQueueLen int + // MaxMem is the maximum number of megabytes that can be allocated + // before scollector panics (shuts down). Default of 500 MB. This + // is a saftey mechanism to protect the host from the monitoring + // agent + MaxMem uint64 // Filter filters collectors matching these terms. Filter []string // PProf is an IP:Port binding to be used for debugging with pprof package. diff --git a/cmd/scollector/main.go b/cmd/scollector/main.go index aa0fb38cef..9ee3510cb2 100644 --- a/cmd/scollector/main.go +++ b/cmd/scollector/main.go @@ -240,14 +240,17 @@ func main() { } collect.MaxQueueLen = conf.MaxQueueLen } - + maxMemMegaBytes := uint64(500) + if conf.MaxMem != 0 { + maxMemMegaBytes = conf.MaxMem + } go func() { - const maxMem = 500 * 1024 * 1024 // 500MB + maxMemBytes := maxMemMegaBytes * 1024 * 1024 var m runtime.MemStats - for range time.Tick(time.Minute) { + for range time.Tick(time.Second * 30) { runtime.ReadMemStats(&m) - if m.Alloc > maxMem { - panic("memory max reached") + if m.Alloc > maxMemBytes { + panic(fmt.Sprintf("memory max reached: (current: %v bytes, max: %v bytes)", m.Alloc, maxMemBytes)) } } }()