2019-01-31 00:02:38 +00:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
2019-04-09 02:04:08 +00:00
|
|
|
"fmt"
|
2019-03-21 02:23:08 +00:00
|
|
|
"github.com/sqshq/sampler/config"
|
2019-01-31 00:02:38 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-02-03 04:11:26 +00:00
|
|
|
type Sampler struct {
|
2019-03-16 23:59:28 +00:00
|
|
|
consumer *Consumer
|
2019-04-07 15:09:24 +00:00
|
|
|
items []*Item
|
|
|
|
triggers []*Trigger
|
2019-03-16 23:59:28 +00:00
|
|
|
triggersChannel chan *Sample
|
2019-04-09 02:04:08 +00:00
|
|
|
variables []string
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-09 02:04:08 +00:00
|
|
|
func NewSampler(consumer *Consumer, items []*Item, triggers []*Trigger, options config.Options, fileVariables map[string]string, rateMs int) Sampler {
|
2019-01-31 00:02:38 +00:00
|
|
|
|
2019-03-21 02:23:08 +00:00
|
|
|
ticker := time.NewTicker(time.Duration(rateMs * int(time.Millisecond)))
|
2019-03-10 04:41:23 +00:00
|
|
|
|
|
|
|
sampler := Sampler{
|
|
|
|
consumer,
|
|
|
|
items,
|
|
|
|
triggers,
|
2019-03-16 23:59:28 +00:00
|
|
|
make(chan *Sample),
|
2019-05-24 02:58:46 +00:00
|
|
|
mergeVariables(fileVariables, options.Environment),
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
2019-01-31 00:02:38 +00:00
|
|
|
|
|
|
|
go func() {
|
2019-02-17 01:54:48 +00:00
|
|
|
for ; true; <-ticker.C {
|
2019-03-10 04:41:23 +00:00
|
|
|
for _, item := range sampler.items {
|
2019-06-03 00:53:15 +00:00
|
|
|
go sampler.sample(item, options)
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case sample := <-sampler.triggersChannel:
|
|
|
|
for _, t := range sampler.triggers {
|
2019-05-20 04:12:40 +00:00
|
|
|
t.Execute(sample)
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-02-03 04:11:26 +00:00
|
|
|
return sampler
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 15:09:24 +00:00
|
|
|
func (s *Sampler) sample(item *Item, options config.Options) {
|
2019-03-09 01:00:13 +00:00
|
|
|
|
2019-04-09 02:04:08 +00:00
|
|
|
val, err := item.nextValue(s.variables)
|
2019-03-09 01:00:13 +00:00
|
|
|
|
2019-04-07 15:09:24 +00:00
|
|
|
if len(val) > 0 {
|
2019-05-31 03:01:43 +00:00
|
|
|
sample := &Sample{Label: item.label, Value: val, Color: item.color}
|
2019-03-10 04:41:23 +00:00
|
|
|
s.consumer.SampleChannel <- sample
|
|
|
|
s.triggersChannel <- sample
|
2019-04-07 15:09:24 +00:00
|
|
|
} else if err != nil {
|
2019-03-16 23:59:28 +00:00
|
|
|
s.consumer.AlertChannel <- &Alert{
|
2019-03-10 04:41:23 +00:00
|
|
|
Title: "SAMPLING FAILURE",
|
2019-03-22 03:21:00 +00:00
|
|
|
Text: getErrorMessage(err),
|
2019-05-31 03:01:43 +00:00
|
|
|
Color: item.color,
|
2019-03-10 04:41:23 +00:00
|
|
|
}
|
2019-03-08 04:04:46 +00:00
|
|
|
}
|
2019-01-31 00:02:38 +00:00
|
|
|
}
|
2019-04-09 02:04:08 +00:00
|
|
|
|
|
|
|
// option variables takes precedence over the file variables with the same name
|
|
|
|
func mergeVariables(fileVariables map[string]string, optionsVariables []string) []string {
|
|
|
|
|
|
|
|
result := optionsVariables
|
|
|
|
|
|
|
|
for key, value := range fileVariables {
|
|
|
|
result = append([]string{fmt.Sprintf("%s=%s", key, value)}, result...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|