Documentation
¶
Overview ¶
Package monitor reports live process and system statistics (CPU, memory, goroutines, uptime) as JSON and as a small HTML dashboard.
Its entry point is New(Options), which returns a *Monitor rather than a Handler because it exposes two of them - `Stats` for the JSON payload and `View` for the dashboard page - and owns the background sampler that both read; call `Stop` when it is no longer needed.
Index ¶
Constants ¶
const DefaultRefreshInterval = 2 * time.Second
DefaultRefreshInterval is the stats refresh interval used when Options.RefreshInterval is not set.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Monitor ¶
type Monitor struct {
Holder *StatsHolder
// contains filtered or unexported fields
}
Monitor tracks and renders the server's process and operating system statistics.
Look its `Stats` and `View` methods. Initialize with the `New` package-level function.
func New ¶
New returns a new Monitor. Metrics stored through expvar standard package: - pid_cpu - pid_ram - pid_conns - os_cpu - os_ram - os_total_ram - os_load_avg - os_conns
Check https://github.com/iris-contrib/middleware/tree/master/expmetric which can be integrated with datadog or other platforms.
SECURITY WARNING: the stats endpoint MUST NOT be public.
It reports the process id, resident memory, host CPU and RAM, load average and connection counts - a precise picture of the machine, useful to anyone deciding whether and when to attack it. Guard it exactly like pprof:
auth := basicauth.Default(map[string]string{"admin": os.Getenv("MONITOR_PASSWORD")})
app.Get("/monitor", auth, m.View)
app.Get("/monitor/stats", auth, m.Stats)
Note also that importing this package registers its metrics into the default expvar map at init, so they are also visible through any mounted "/debug/vars" handler.
type Options ¶
type Options struct {
// Optional process id, defaults to the current one.
PID int32 `json:"pid" yaml:"PID"`
RefreshInterval time.Duration `json:"refresh_interval" yaml:"RefreshInterval"`
ViewRefreshInterval time.Duration `json:"view_refresh_interval" yaml:"ViewRefreshInterval"`
// If more than zero enables line animation. Defaults to zero.
ViewAnimationInterval time.Duration `json:"view_animation_interval" yaml:"ViewAnimationInterval"`
// The title of the monitor HTML document.
ViewTitle string `json:"view_title" yaml:"ViewTitle"`
}
Options holds the optional fields for the Monitor structure.
type Stats ¶
type Stats struct {
PIDCPU float64 `json:"pid_cpu" yaml:"PIDCPU"`
PIDRAM uint64 `json:"pid_ram" yaml:"PIDRAM"`
PIDConns int64 `json:"pid_conns" yaml:"PIDConns"`
OSCPU float64 `json:"os_cpu" yaml:"OSCPU"`
OSRAM uint64 `json:"os_ram" yaml:"OSRAM"`
OSTotalRAM uint64 `json:"os_total_ram" yaml:"OSTotalRAM"`
OSLoadAvg float64 `json:"os_load_avg" yaml:"OSLoadAvg"`
OSConns int64 `json:"os_conns" yaml:"OSConns"`
}
Stats holds the process and operating system statistics values.
Note that each statistic has its own expvar metric that you can use to render e.g. through statsd. Available values: * pid_cpu * pid_ram * pid_conns * os_cpu * os_ram * os_total_ram * os_load_avg * os_conns
type StatsHolder ¶
type StatsHolder struct {
// contains filtered or unexported fields
}
StatsHolder holds and refreshes the statistics.
func (*StatsHolder) Err ¶
func (sh *StatsHolder) Err() <-chan error
Err returns a read-only channel which may be filled with errors came from the refresh stats operation.
func (*StatsHolder) GetStats ¶
func (sh *StatsHolder) GetStats() Stats
GetStats returns a copy of the latest stats available.
func (*StatsHolder) Stop ¶
func (sh *StatsHolder) Stop()
Stop terminates the routine which retrieves the stats for this holder. It is a no-op if the holder was never started or it is already stopped. Other StatsHolder instances are not affected.