Skip to content

Commit

Permalink
Improve metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Kashkovsky committed Apr 14, 2022
1 parent e3e6ac3 commit b938903
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
25 changes: 20 additions & 5 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package cmd

import (
"log"
"time"

"sync"

"github.com/Kashkovsky/hostmonitor/core"
Expand All @@ -43,17 +45,30 @@ func runWatch(cmd *cobra.Command, args []string) {
resMap := sync.Map{}
printer := core.NewPrinter()
watcher := core.NewWatcher(&watchConfig)


go func() {
for {
printer.ToTable(&resMap)
time.Sleep(time.Second)
}
}()

watcher.Watch(func(res core.TestResult) {
resMap.Store(res.Id, res)
printer.ToTable(&resMap)
if res.InProgress {
_, ok := resMap.Load(res.Id)
if !ok {
resMap.Store(res.Id, res)
}
} else {
resMap.Store(res.Id, res)
}
})
}

func init() {
rootCmd.AddCommand(watchCmd)
watchCmd.Flags().StringVarP(&watchConfig.ConfigUrl, "configUrl", "c", core.ITArmyConfigURL, "Url of config containing url list")
watchCmd.Flags().IntVarP(&watchConfig.TestInterval, "testInterval", "i", 10, "Interval in seconds between test updates")
watchCmd.Flags().IntVarP(&watchConfig.RequestTimeout, "requestTimeout", "t", 5, "Request timeout")
watchCmd.Flags().IntVarP(&watchConfig.TestInterval, "testInterval", "i", 20, "Interval in seconds between test updates")
watchCmd.Flags().IntVarP(&watchConfig.RequestTimeout, "requestTimeout", "t", 10, "Request timeout")
watchCmd.Flags().IntVarP(&watchConfig.UpdateInterval, "updateInterval", "u", 600, "Config update interval in seconds")
}
3 changes: 1 addition & 2 deletions core/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"os"
"os/exec"
"runtime"
"strconv"
"sync"

"github.com/jedib0t/go-pretty/v6/table"
Expand Down Expand Up @@ -53,7 +52,7 @@ func (p *Printer) ToTable(results *sync.Map) {
k,
testResult.tcp,
testResult.httpStatus,
strconv.FormatInt(testResult.duration.Milliseconds(), 10) + "ms",
testResult.duration,
})
}
return true
Expand Down
14 changes: 10 additions & 4 deletions core/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"net/http"
"net/url"
"regexp"
"strconv"
"time"
)

type TestResult struct {
Id string
InProgress bool
url url.URL
tcp string
httpStatus string
duration time.Duration
duration string
}

type Tester struct {
Expand All @@ -38,18 +40,22 @@ func (t *Tester) Test(url *url.URL) {
case <-t.quit:
return
default:
t.out <- TestResult{
Id: url.Host,
InProgress: true,
httpStatus: "Testing...",
}
pass := t.tcp(url)
status, duration := t.http(url)
t.out <- TestResult{
Id: url.Host,
url: *url,
tcp: fmt.Sprintf("%d/10", pass),
httpStatus: status,
duration: duration,
duration: strconv.FormatInt(duration.Milliseconds(), 10) + "ms",
}
time.Sleep(t.testInterval)
}

time.Sleep(t.testInterval)
}
}

Expand Down
25 changes: 12 additions & 13 deletions core/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,19 @@ func NewWatcher(config *WatchConfig) Watcher {

func (w *Watcher) Watch(f func(TestResult)) {
for {
err := w.doWatch()
err := w.doWatch(f)

if err != nil {
log.Fatalf("Fatal: %v", err)
return
}

go func() {
for {
select {
case <-w.quit:
return
case rec := <-w.out:
f(rec)
}
}
}()

time.Sleep(time.Duration(w.config.UpdateInterval) * time.Second)
w.quit <- true
}
}

func (w *Watcher) doWatch() error {
func (w *Watcher) doWatch(f func(TestResult)) error {
log.Default().Println("Fetching new config...")
config, err := w.config.Update()

Expand All @@ -69,6 +58,16 @@ func (w *Watcher) doWatch() error {
}

go w.tester.Test(u)
go func() {
for {
select {
case <-w.quit:
return
case rec := <-w.out:
f(rec)
}
}
}()
}

return nil
Expand Down
Binary file modified monitor
Binary file not shown.

0 comments on commit b938903

Please sign in to comment.