mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2025-04-19 19:21:41 +02:00
Use central timer for collectors and router. Add expressions to router
This commit is contained in:
parent
44d8b0c979
commit
b7fbd198ff
@ -7,6 +7,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -31,26 +32,26 @@ type collectorManager struct {
|
|||||||
collectors []MetricCollector
|
collectors []MetricCollector
|
||||||
output chan lp.CCMetric
|
output chan lp.CCMetric
|
||||||
done chan bool
|
done chan bool
|
||||||
interval time.Duration
|
ticker mct.MultiChanTicker
|
||||||
duration time.Duration
|
duration time.Duration
|
||||||
wg *sync.WaitGroup
|
wg *sync.WaitGroup
|
||||||
config map[string]json.RawMessage
|
config map[string]json.RawMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
type CollectorManager interface {
|
type CollectorManager interface {
|
||||||
Init(interval time.Duration, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) error
|
Init(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) error
|
||||||
AddOutput(output chan lp.CCMetric)
|
AddOutput(output chan lp.CCMetric)
|
||||||
Start()
|
Start()
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (cm *collectorManager) Init(interval time.Duration, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) error {
|
func (cm *collectorManager) Init(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) error {
|
||||||
cm.collectors = make([]MetricCollector, 0)
|
cm.collectors = make([]MetricCollector, 0)
|
||||||
cm.output = nil
|
cm.output = nil
|
||||||
cm.done = make(chan bool)
|
cm.done = make(chan bool)
|
||||||
cm.wg = wg
|
cm.wg = wg
|
||||||
cm.interval = interval
|
cm.ticker = ticker
|
||||||
cm.duration = duration
|
cm.duration = duration
|
||||||
configFile, err := os.Open(collectConfigFile)
|
configFile, err := os.Open(collectConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -84,7 +85,8 @@ func (cm *collectorManager) Init(interval time.Duration, duration time.Duration,
|
|||||||
|
|
||||||
func (cm *collectorManager) Start() {
|
func (cm *collectorManager) Start() {
|
||||||
cm.wg.Add(1)
|
cm.wg.Add(1)
|
||||||
ticker := time.NewTicker(cm.interval)
|
tick := make(chan time.Time)
|
||||||
|
cm.ticker.AddChannel(tick)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
CollectorManagerLoop:
|
CollectorManagerLoop:
|
||||||
@ -96,7 +98,7 @@ CollectorManagerLoop:
|
|||||||
cm.wg.Done()
|
cm.wg.Done()
|
||||||
log.Print("[CollectorManager] DONE\n")
|
log.Print("[CollectorManager] DONE\n")
|
||||||
break CollectorManagerLoop
|
break CollectorManagerLoop
|
||||||
case t := <-ticker.C:
|
case t := <- tick:
|
||||||
for _, c := range cm.collectors {
|
for _, c := range cm.collectors {
|
||||||
CollectorManagerInputLoop:
|
CollectorManagerInputLoop:
|
||||||
select {
|
select {
|
||||||
@ -128,9 +130,9 @@ func (cm *collectorManager) Close() {
|
|||||||
log.Print("[CollectorManager] CLOSE")
|
log.Print("[CollectorManager] CLOSE")
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(interval time.Duration, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) (CollectorManager, error) {
|
func New(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) (CollectorManager, error) {
|
||||||
cm := &collectorManager{}
|
cm := &collectorManager{}
|
||||||
err := cm.Init(interval, duration, wg, collectConfigFile)
|
err := cm.Init(ticker, duration, wg, collectConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ type CCMetric interface {
|
|||||||
lp.MutableMetric
|
lp.MutableMetric
|
||||||
AddMeta(key, value string)
|
AddMeta(key, value string)
|
||||||
MetaList() []*lp.Tag
|
MetaList() []*lp.Tag
|
||||||
|
RemoveTag(key string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *ccMetric) Meta() map[string]string {
|
func (m *ccMetric) Meta() map[string]string {
|
||||||
|
@ -6,6 +6,9 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
"gopkg.in/Knetic/govaluate.v2"
|
||||||
|
mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker"
|
||||||
)
|
)
|
||||||
|
|
||||||
type metricRounterTagConfig struct {
|
type metricRounterTagConfig struct {
|
||||||
@ -25,29 +28,27 @@ type metricRouter struct {
|
|||||||
outputs []chan lp.CCMetric
|
outputs []chan lp.CCMetric
|
||||||
done chan bool
|
done chan bool
|
||||||
wg *sync.WaitGroup
|
wg *sync.WaitGroup
|
||||||
|
timestamp time.Time
|
||||||
|
ticker mct.MultiChanTicker
|
||||||
config metricRouterConfig
|
config metricRouterConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
type MetricRouter interface {
|
type MetricRouter interface {
|
||||||
Init(routerDone chan bool, wg *sync.WaitGroup) error
|
Init(ticker mct.MultiChanTicker, wg *sync.WaitGroup, routerConfigFile string) error
|
||||||
AddInput(input chan lp.CCMetric)
|
AddInput(input chan lp.CCMetric)
|
||||||
AddOutput(output chan lp.CCMetric)
|
AddOutput(output chan lp.CCMetric)
|
||||||
ReadConfig(filename string) error
|
|
||||||
Start()
|
Start()
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (r *metricRouter) Init(routerDone chan bool, wg *sync.WaitGroup) error {
|
func (r *metricRouter) Init(ticker mct.MultiChanTicker, wg *sync.WaitGroup, routerConfigFile string) error {
|
||||||
r.inputs = make([]chan lp.CCMetric, 0)
|
r.inputs = make([]chan lp.CCMetric, 0)
|
||||||
r.outputs = make([]chan lp.CCMetric, 0)
|
r.outputs = make([]chan lp.CCMetric, 0)
|
||||||
r.done = routerDone
|
r.done = make(chan bool)
|
||||||
r.wg = wg
|
r.wg = wg
|
||||||
return nil
|
r.ticker = ticker
|
||||||
}
|
configFile, err := os.Open(routerConfigFile)
|
||||||
|
|
||||||
func (r *metricRouter) ReadConfig(filename string) error {
|
|
||||||
configFile, err := os.Open(filename)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Print(err.Error())
|
log.Print(err.Error())
|
||||||
return err
|
return err
|
||||||
@ -62,8 +63,93 @@ func (r *metricRouter) ReadConfig(filename string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *metricRouter) StartTimer() {
|
||||||
|
m := make(chan time.Time)
|
||||||
|
r.ticker.AddChannel(m)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case t := <- m:
|
||||||
|
r.timestamp = t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *metricRouter) EvalCondition(Cond string, point lp.CCMetric) (bool, error){
|
||||||
|
expression, err := govaluate.NewEvaluableExpression(Cond)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(Cond, " = ", err.Error())
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
params := make(map[string]interface{})
|
||||||
|
params["name"] = point.Name()
|
||||||
|
for _,t := range point.TagList() {
|
||||||
|
params[t.Key] = t.Value
|
||||||
|
}
|
||||||
|
for _,m := range point.MetaList() {
|
||||||
|
params[m.Key] = m.Value
|
||||||
|
}
|
||||||
|
for _,f := range point.FieldList() {
|
||||||
|
params[f.Key] = f.Value
|
||||||
|
}
|
||||||
|
params["timestamp"] = point.Time()
|
||||||
|
|
||||||
|
result, err := expression.Evaluate(params)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(Cond, " = ", err.Error())
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return bool(result.(bool)), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *metricRouter) DoAddTags(point lp.CCMetric) {
|
||||||
|
for _, m := range r.config.AddTags {
|
||||||
|
var res bool
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if m.Condition == "*" {
|
||||||
|
res = true
|
||||||
|
err = nil
|
||||||
|
} else {
|
||||||
|
res, err = r.EvalCondition(m.Condition, point)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err.Error())
|
||||||
|
res = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if res == true {
|
||||||
|
point.AddTag(m.Key, m.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *metricRouter) DoDelTags(point lp.CCMetric) {
|
||||||
|
for _, m := range r.config.DelTags {
|
||||||
|
var res bool
|
||||||
|
var err error
|
||||||
|
if m.Condition == "*" {
|
||||||
|
res = true
|
||||||
|
err = nil
|
||||||
|
} else {
|
||||||
|
res, err = r.EvalCondition(m.Condition, point)
|
||||||
|
if err != nil {
|
||||||
|
log.Print(err.Error())
|
||||||
|
res = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if res == true {
|
||||||
|
point.RemoveTag(m.Key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *metricRouter) Start() {
|
func (r *metricRouter) Start() {
|
||||||
r.wg.Add(1)
|
r.wg.Add(1)
|
||||||
|
r.timestamp = time.Now()
|
||||||
|
if r.config.IntervalStamp == true {
|
||||||
|
r.StartTimer()
|
||||||
|
}
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
RouterLoop:
|
RouterLoop:
|
||||||
@ -82,6 +168,11 @@ RouterInputLoop:
|
|||||||
break RouterInputLoop
|
break RouterInputLoop
|
||||||
case p := <- c:
|
case p := <- c:
|
||||||
log.Print("[MetricRouter] FORWARD ",p)
|
log.Print("[MetricRouter] FORWARD ",p)
|
||||||
|
r.DoAddTags(p)
|
||||||
|
r.DoDelTags(p)
|
||||||
|
if r.config.IntervalStamp == true {
|
||||||
|
p.SetTime(r.timestamp)
|
||||||
|
}
|
||||||
for _, o := range r.outputs {
|
for _, o := range r.outputs {
|
||||||
o <- p
|
o <- p
|
||||||
}
|
}
|
||||||
@ -108,9 +199,9 @@ func (r *metricRouter) Close() {
|
|||||||
log.Print("[MetricRouter] CLOSE\n")
|
log.Print("[MetricRouter] CLOSE\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(done chan bool, wg *sync.WaitGroup) (MetricRouter, error) {
|
func New(ticker mct.MultiChanTicker, wg *sync.WaitGroup, routerConfigFile string) (MetricRouter, error) {
|
||||||
r := &metricRouter{}
|
r := &metricRouter{}
|
||||||
err := r.Init(done, wg)
|
err := r.Init(ticker, wg, routerConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
39
internal/multiChanTicker/multiChanTicker.go
Normal file
39
internal/multiChanTicker/multiChanTicker.go
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package multiChanTicker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type multiChanTicker struct {
|
||||||
|
ticker *time.Ticker
|
||||||
|
channels []chan time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type MultiChanTicker interface {
|
||||||
|
Init(duration time.Duration)
|
||||||
|
AddChannel(chan time.Time)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *multiChanTicker) Init(duration time.Duration) {
|
||||||
|
t.ticker = time.NewTicker(duration)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case ts := <-t.ticker.C:
|
||||||
|
for _, c := range t.channels {
|
||||||
|
c <- ts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *multiChanTicker) AddChannel(channel chan time.Time) {
|
||||||
|
t.channels = append(t.channels, channel)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTicker(duration time.Duration) MultiChanTicker {
|
||||||
|
t := &multiChanTicker{}
|
||||||
|
t.Init(duration)
|
||||||
|
return t
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user