Skip to content

Commit

Permalink
feat(api/interval): supporting to set gather interval (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtulio authored Nov 8, 2018
1 parent a405447 commit 16942a9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.0
v0.2.1
3 changes: 3 additions & 0 deletions globals.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type globalConf struct {
StkUsername string
StkApikey string
StkTags string
StkInterval int
}

type globalProm struct {
Expand All @@ -27,6 +28,7 @@ type globalProm struct {
const (
exporterName = "statuscake_exporter"
exporterDescription = "StatusCake Exporter"
default_stkInterval = 300
)

var (
Expand All @@ -47,6 +49,7 @@ var (
"",
"",
"",
default_stkInterval,
}
stkAPI *stk.StkAPI
prom globalProm
Expand Down
24 changes: 17 additions & 7 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ func initFlags() error {
flagListenAddress := flag.String("web.listen-address", config.listenAddress, "Address on which to expose metrics and web interface.")
flagMetricsPath := flag.String("web.telemetry-path", config.metricsPath, "Path under which to expose metrics.")

flagStkUsername := flag.String("stk.username", "", "StatusCake API's Username.")
flagStkApikey := flag.String("stk.apikey", "", "StatusCake API's Apikey.")
flagStkTags := flag.String("stk.tags", "", "StatusCake Filter Tags separated by comma.")
flagStkUsername := flag.String("stk.username", "", "StatusCake API's Username. Default: nil (required)")
flagStkApikey := flag.String("stk.apikey", "", "StatusCake API's Apikey. Default: nil (required)")
flagStkTags := flag.String("stk.tags", "", "StatusCake Filter Tags separated by comma. Default: <empty>")
flagStkInterval := flag.Int("stk.interval", 300, "StatusCake interval time, in seconds, to gather metrics on API (avoid throtling). Default: 300.")

flagVersion := flag.Bool("v", false, "prints current version")
flag.Usage = usage
Expand Down Expand Up @@ -62,6 +63,10 @@ func initFlags() error {
config.StkTags = *flagStkTags
}

if *flagStkInterval != default_stkInterval {
config.StkInterval = *flagStkInterval
}

return nil
}

Expand Down Expand Up @@ -99,22 +104,27 @@ func initStkAPI() error {
var err error
err = nil

log.Info("Initializing Status Cake client...")

stkAPI, err = stk.NewStkAPI(config.StkUsername, config.StkApikey)
if err != nil {
log.Errorln("Init StatusCake API: ", err)
log.Errorln("Initializing StatusCake API: ", err)
return err
}
log.Infoln("Success")
log.Info("Initializing StatusCake API client...Success")

stkAPI.SetWaitInterval(uint32(config.StkInterval))

err = stkAPI.GatherAll()
if err != nil {
log.Warnln("Init Prom: Couldn't create collector: ", err)
return err
}

log.Info("StatusCake collector config:")
log.Info("- username: ", config.StkUsername)
log.Info("- interval: ", stkAPI.GetWaitInterval())
if config.StkTags != "" {
stkAPI.SetConfigTags(config.StkTags)
log.Info("- tags: ", stkAPI.GetTags())
}

return nil
Expand Down
23 changes: 18 additions & 5 deletions stk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ type StkOptions struct {
type StkAPI struct {
client *statuscake.Client
configTags string
waitIntervalSec uint8
waitIntervalSec uint32
Tests []*statuscake.Test
controlInit bool
}

type StkTest statuscake.Test
Expand All @@ -38,7 +39,8 @@ func NewStkAPI(user string, pass string) (*StkAPI, error) {

return &StkAPI{
client: c,
waitIntervalSec: 30,
waitIntervalSec: 300,
controlInit: false,
}, nil
}

Expand All @@ -47,14 +49,22 @@ func (stk *StkAPI) SetConfigTags(tags string) {
stk.configTags = tags
}

func (stk *StkAPI) GetTags() string {
return stk.configTags
}

func (stk *StkAPI) GetTests() []*statuscake.Test {
return stk.Tests
}

func (stk *StkAPI) SetWaitInterval(sec uint8) {
func (stk *StkAPI) SetWaitInterval(sec uint32) {
stk.waitIntervalSec = sec
}

func (stk *StkAPI) GetWaitInterval() uint32 {
return stk.waitIntervalSec
}

// gather functions
func (stk *StkAPI) GatherAll() error {
go stk.gatherTest()
Expand All @@ -73,14 +83,18 @@ func (stk *StkAPI) gatherTest() {
log.Println(err)
}
stk.Tests = tests
if !stk.controlInit {
log.Println(" Initial API discovery returns the Total of Tests:", len(tests))
stk.controlInit = true
}
time.Sleep(time.Second * time.Duration(stk.waitIntervalSec))
}
}

func (stk *StkAPI) gatherTestsData() {
for {
if len(stk.Tests) <= 0 {
time.Sleep(10 * time.Duration(stk.waitIntervalSec))
time.Sleep(time.Second * 10)
continue
}
filters := url.Values{}
Expand All @@ -96,7 +110,6 @@ func (stk *StkAPI) gatherTestsData() {
}
stk.Tests[t].PerformanceData = perfData
}

time.Sleep(time.Second * time.Duration(stk.waitIntervalSec))
}
}

0 comments on commit 16942a9

Please sign in to comment.