Skip to content

Commit

Permalink
Make scrape interval changeable
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Oct 31, 2020
1 parent 83bece4 commit 7421c21
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
20 changes: 11 additions & 9 deletions diagnoser/diagnoser.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package diagnoser mainly provides two components, scraper and GUI
// for the process diagnosis.
package diagnoser

import (
Expand All @@ -13,21 +15,21 @@ import (
"github.com/nakabonne/gosivy/stats"
)

const defaultRequestInterval = time.Second

func Run(addr *net.TCPAddr) error {
// Run performs the scraper which periodically scrapes from the agent,
// and then draws charts to show the stats.
func Run(addr *net.TCPAddr, scrapeInterval time.Duration) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

statsCh := make(chan *stats.Stats)
meta, err := poll(ctx, addr, statsCh)
meta, err := startScraping(ctx, addr, scrapeInterval, statsCh)
if err != nil {
return err
}
return gui.Run(meta, statsCh)
}

func poll(ctx context.Context, addr *net.TCPAddr, statsCh chan<- *stats.Stats) (*stats.Meta, error) {
func startScraping(ctx context.Context, addr *net.TCPAddr, interval time.Duration, statsCh chan<- *stats.Stats) (*stats.Meta, error) {
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
return nil, err
Expand All @@ -48,7 +50,7 @@ func poll(ctx context.Context, addr *net.TCPAddr, statsCh chan<- *stats.Stats) (
}

go func(ctx context.Context, ch chan<- *stats.Stats) {
tick := time.NewTicker(defaultRequestInterval)
tick := time.NewTicker(interval)
defer tick.Stop()
for {
select {
Expand All @@ -64,19 +66,19 @@ func poll(ctx context.Context, addr *net.TCPAddr, statsCh chan<- *stats.Stats) (

buf := []byte{stats.SignalStats}
if _, err := conn.Write(buf); err != nil {
logrus.Errorf("failed to write: %v", err)
logrus.Errorf("failed to write into connection: %v", err)
continue
}
res, err := ioutil.ReadAll(conn)
if err != nil {
logrus.Errorf("failed to read: %v", err)
logrus.Errorf("failed to read the response: %v", err)
continue
}
conn.Close()

var stats stats.Stats
if err := json.Unmarshal(res, &stats); err != nil {
logrus.Errorf("failed to unmarshal stats: %v", err)
logrus.Errorf("failed to decode stats: %v", err)
continue
}
ch <- &stats
Expand Down
4 changes: 2 additions & 2 deletions diagnoser/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (

const (
// How often termdash redraws the screen.
redrawInterval = 250 * time.Millisecond
rootID = "root"
defaultRedrawInterval = time.Second
rootID = "root"
)

type runner func(ctx context.Context, t terminalapi.Terminal, c *container.Container, opts ...termdash.Option) error
Expand Down
29 changes: 23 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"runtime"
"strconv"
"strings"
"time"

"github.com/k0kubun/pp"
"github.com/sirupsen/logrus"
Expand All @@ -20,6 +21,8 @@ import (
"github.com/nakabonne/gosivy/pidfile"
)

const defaultScrapeInterval = time.Second

var (
flagSet = flag.NewFlagSet("gosivy", flag.ContinueOnError)

Expand All @@ -30,11 +33,12 @@ var (
)

type cli struct {
debug bool
version bool
list bool
stdout io.Writer
stderr io.Writer
debug bool
version bool
list bool
scrapeInterval time.Duration
stdout io.Writer
stderr io.Writer
}

func (c *cli) usage() {
Expand Down Expand Up @@ -69,21 +73,34 @@ func parseFlags(stdout, stderr io.Writer) (*cli, error) {
flagSet.BoolVarP(&c.version, "version", "v", false, "Print the current version.")
flagSet.BoolVar(&c.debug, "debug", false, "Run in debug mode.")
flagSet.BoolVarP(&c.list, "list-processes", "l", false, "Show processes where gosivy agent runs on.")
flagSet.DurationVar(&c.scrapeInterval, "scrape-interval", defaultScrapeInterval, "Interval to scrape from the agent. It must be >= 1s")
flagSet.Usage = c.usage
if err := flagSet.Parse(os.Args[1:]); err != nil {
if !errors.Is(err, flag.ErrHelp) {
fmt.Fprintln(c.stderr, err)
}
return nil, err
}

return c, nil
}

func (c *cli) validate() error {
if c.scrapeInterval < time.Second {
return fmt.Errorf(`"--scrape-interval" must be >= 1s`)
}
return nil
}

func (c *cli) run(args []string) int {
if c.version {
fmt.Fprintf(c.stderr, "version=%s, commit=%s, buildDate=%s, os=%s, arch=%s\n", version, commit, date, runtime.GOOS, runtime.GOARCH)
return 0
}
if err := c.validate(); err != nil {
fmt.Fprintln(c.stderr, err)
return 1
}
if err := setLogger(nil, c.debug); err != nil {
fmt.Fprintf(c.stderr, "failed to prepare for debugging: %v\n", err)
return 1
Expand All @@ -105,7 +122,7 @@ func (c *cli) run(args []string) int {
return 1
}
}
if err := diagnoser.Run(addr); err != nil {
if err := diagnoser.Run(addr, c.scrapeInterval); err != nil {
fmt.Fprintf(c.stderr, "failed to start diagnoser: %s\n", err.Error())
c.usage()
return 1
Expand Down

0 comments on commit 7421c21

Please sign in to comment.