@@ -11,6 +11,7 @@ import (
1111 "os/signal"
1212 "runtime"
1313 "runtime/pprof"
14+ "strconv"
1415 "strings"
1516 "syscall"
1617 "time"
@@ -32,9 +33,9 @@ var useStdinConfig = false
3233var configSearchDirs = []string {"./" , "./config" , "/etc/pandora" }
3334
3435type cliConfig struct {
35- Engine engine.Config `config:",squash"`
36- Log logConfig `config:"log"`
37- // TODO(skipor): monitoring
36+ Engine engine.Config `config:",squash"`
37+ Log logConfig `config:"log"`
38+ Monitoring monitoringConfig `config:" monitoring"`
3839}
3940
4041type logConfig struct {
@@ -62,6 +63,20 @@ func defaultConfig() *cliConfig {
6263 Level : zap .InfoLevel ,
6364 File : "stdout" ,
6465 },
66+ Monitoring : monitoringConfig {
67+ Expvar : & expvarConfig {
68+ Enabled : false ,
69+ Port : 1234 ,
70+ },
71+ CPUProfile : & cpuprofileConfig {
72+ Enabled : false ,
73+ File : "cpuprofile.log" ,
74+ },
75+ MemProfile : & memprofileConfig {
76+ Enabled : false ,
77+ File : "memprofile.log" ,
78+ },
79+ },
6580 }
6681}
6782
@@ -74,13 +89,9 @@ func Run() {
7489 flag .PrintDefaults ()
7590 }
7691 var (
77- example bool
78- monitoring monitoringConfig
92+ example bool
7993 )
8094 flag .BoolVar (& example , "example" , false , "print example config to STDOUT and exit" )
81- flag .StringVar (& monitoring .CPUProfile , "cpuprofile" , "" , "write cpu profile to file" )
82- flag .StringVar (& monitoring .MemProfile , "memprofile" , "" , "write memory profile to this file" )
83- flag .BoolVar (& monitoring .Expvar , "expvar" , false , "start HTTP server with monitoring variables" )
8495 flag .Parse ()
8596
8697 if example {
@@ -93,7 +104,7 @@ func Run() {
93104 zap .ReplaceGlobals (log )
94105 zap .RedirectStdLog (log )
95106
96- closeMonitoring := startMonitoring (monitoring )
107+ closeMonitoring := startMonitoring (conf . Monitoring )
97108 defer closeMonitoring ()
98109 m := newEngineMetrics ()
99110 startReport (m )
@@ -214,22 +225,39 @@ func newViper() *viper.Viper {
214225}
215226
216227type monitoringConfig struct {
217- Expvar bool // TODO: struct { Enabled bool; Port string }
218- CPUProfile string // TODO: struct { Enabled bool; File string }
219- MemProfile string // TODO: struct { Enabled bool; File string }
228+ Expvar * expvarConfig
229+ CPUProfile * cpuprofileConfig
230+ MemProfile * memprofileConfig
231+ }
232+
233+ type expvarConfig struct {
234+ Enabled bool `config:"enabled"`
235+ Port int `config:"port" validate:"required"`
236+ }
237+
238+ type cpuprofileConfig struct {
239+ Enabled bool `config:"enabled"`
240+ File string `config:"file"`
241+ }
242+
243+ type memprofileConfig struct {
244+ Enabled bool `config:"enabled"`
245+ File string `config:"file"`
220246}
221247
222248func startMonitoring (conf monitoringConfig ) (stop func ()) {
223249 zap .L ().Debug ("Start monitoring" , zap .Reflect ("conf" , conf ))
224- if conf .Expvar {
225- go func () {
226- err := http .ListenAndServe (":1234" , nil )
227- zap .L ().Fatal ("Monitoring server failed" , zap .Error (err ))
228- }()
250+ if conf .Expvar != nil {
251+ if conf .Expvar .Enabled {
252+ go func () {
253+ err := http .ListenAndServe (":" + strconv .Itoa (conf .Expvar .Port ), nil )
254+ zap .L ().Fatal ("Monitoring server failed" , zap .Error (err ))
255+ }()
256+ }
229257 }
230258 var stops []func ()
231- if conf .CPUProfile != "" {
232- f , err := os .Create (conf .CPUProfile )
259+ if conf .CPUProfile . Enabled {
260+ f , err := os .Create (conf .CPUProfile . File )
233261 if err != nil {
234262 zap .L ().Fatal ("CPU profile file create fail" , zap .Error (err ))
235263 }
@@ -240,8 +268,8 @@ func startMonitoring(conf monitoringConfig) (stop func()) {
240268 f .Close ()
241269 })
242270 }
243- if conf .MemProfile != "" {
244- f , err := os .Create (conf .MemProfile )
271+ if conf .MemProfile . Enabled {
272+ f , err := os .Create (conf .MemProfile . File )
245273 if err != nil {
246274 zap .L ().Fatal ("Memory profile file create fail" , zap .Error (err ))
247275 }
0 commit comments