Skip to content

Commit

Permalink
Merge pull request #1652 from bosun-monitor/maxMemConf
Browse files Browse the repository at this point in the history
cmd/scollector: Make MaxMem kill switch configurable
  • Loading branch information
gbrayut committed Mar 17, 2016
2 parents e8d299e + 6a65e1b commit fd9e271
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 5 additions & 0 deletions cmd/scollector/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 8 additions & 5 deletions cmd/scollector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}()
Expand Down

0 comments on commit fd9e271

Please sign in to comment.