Skip to content

Commit b6a54f3

Browse files
authored
Merge pull request #120 from nettorta/monitoring_config
monitoring configuration, examples
2 parents fb6bf51 + 5b54c5e commit b6a54f3

File tree

7 files changed

+138
-25
lines changed

7 files changed

+138
-25
lines changed

acceptance_tests/acceptance_suite_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,37 @@ func NewTestConfig() *TestConfig {
6565
// PandoraConfig will be encoded to file via github.com/ghodss/yaml that supports json tags.
6666
// Or it can be encoded as JSON too, to test pandora JSON support.
6767
type PandoraConfig struct {
68-
Pool []*InstancePoolConfig `json:"pools"`
69-
LogConfig `json:"log,omitempty"`
68+
Pool []*InstancePoolConfig `json:"pools"`
69+
LogConfig `json:"log,omitempty"`
70+
MonitoringConfig `json:"monitoring,omitempty"`
7071
}
7172

7273
type LogConfig struct {
7374
Level string `json:"level,omitempty"`
7475
File string `json:"file,omitempty"`
7576
}
7677

78+
type MonitoringConfig struct {
79+
Expvar *expvarConfig
80+
CPUProfile *cpuprofileConfig
81+
MemProfile *memprofileConfig
82+
}
83+
84+
type expvarConfig struct {
85+
Enabled bool `json:"enabled"`
86+
Port int `json:"port"`
87+
}
88+
89+
type cpuprofileConfig struct {
90+
Enabled bool `json:"enabled"`
91+
File string `json:"file"`
92+
}
93+
94+
type memprofileConfig struct {
95+
Enabled bool `json:"enabled"`
96+
File string `json:"file"`
97+
}
98+
7799
func (pc *PandoraConfig) Append(ipc *InstancePoolConfig) {
78100
pc.Pool = append(pc.Pool, ipc)
79101
}

cli/cli.go

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3233
var configSearchDirs = []string{"./", "./config", "/etc/pandora"}
3334

3435
type 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

4041
type 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

216227
type 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

222248
func 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
}

docs/tutorial.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ Pandora supports config files in `YAML`_ format. Create a new file named ``load.
3333
type: once # start 10 instances
3434
times: 10
3535
36+
37+
You can enable debug information about gun (e.g. monitoring and additional logging).
38+
39+
.. code-block:: yaml
40+
41+
log: # gun logging configuration
42+
level: error # log only `error` messages (`debug` for verbose logging)
43+
44+
monitoring:
45+
expvar: # gun statistics HTTP server
46+
enabled: true
47+
port: 1234
48+
cpuprofile: # cpu profiling
49+
enabled: true
50+
file: "cpuprofile.log"
51+
memprofile: # mem profiling
52+
enabled: true
53+
memprofile: "memprofile.log"
54+
55+
3656
`ammo.uri`:
3757

3858
::

examples/connect.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ pools:
1919
startup:
2020
type: once
2121
times: 5
22+
log:
23+
level: debug

examples/debug_and_profiling.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
pools:
2+
- id: HTTP pool
3+
gun:
4+
type: http
5+
target: example.com:80
6+
dial:
7+
timeout: 1s
8+
ammo:
9+
type: http/json
10+
file: ./examples/http.jsonline
11+
result:
12+
type: phout
13+
destination: ./http_phout.log
14+
rps:
15+
type: line
16+
from: 1
17+
to: 5
18+
duration: 2s
19+
startup:
20+
type: once
21+
times: 5
22+
log:
23+
level: debug
24+
monitoring:
25+
expvar:
26+
enabled: true
27+
port: 1234
28+
cpuprofile:
29+
enabled: true
30+
memprofile:
31+
enabled: true

examples/example.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ pools:
1515
startup:
1616
type: once
1717
times: 5
18-
18+
log:
19+
level: debug

examples/http.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,13 @@ pools:
1919
startup:
2020
type: once
2121
times: 5
22-
22+
log:
23+
level: debug
24+
monitoring:
25+
expvar:
26+
enabled: true
27+
port: 1234
28+
cpuprofile:
29+
enabled: false
30+
memprofile:
31+
enabled: false

0 commit comments

Comments
 (0)