add interval configuration in global section

This commit is contained in:
Ulric Qin 2022-04-14 23:00:39 +08:00
parent fb4f23c6a6
commit 88a454b27a
7 changed files with 46 additions and 47 deletions

View File

@ -2,6 +2,7 @@ package agent
import (
"fmt"
"log"
"strings"
"sync"
"time"
@ -82,7 +83,7 @@ func postSeries(series []*prompb.TimeSeries) {
sb.WriteString(fmt.Sprint(series[i].Samples[j].Value))
}
fmt.Println(sb.String())
log.Println(sb.String())
}
return
}

View File

@ -7,6 +7,8 @@ Hostname = ""
OmitHostname = false
# s | ms
Precision = "ms"
# global collect interval
IntervalSeconds = 15
[Global.Labels]
region = "shanghai"

View File

@ -1,18 +1,18 @@
# print configurations
PrintConfigs = false
# collect interval
IntervalSeconds = 30
# collect timeout per target
TimeoutSeconds = 20
# append labels for series
Labels = { aaaa = "bbbb", cccc = "ddddd" }
# # print configurations
# PrintConfigs = false
[[Targets]]
# redis server address
Address = "127.0.0.1:6379"
# redis server password
Password = "1234"
# collect interval this target
IntervalSeconds = 5
# collect timeout this target
TimeoutSeconds = 5
# # collect interval
# IntervalSeconds = 15
# # append labels for series
# Labels = { key1 = "value1", key2 = "value2" }
# [[Targets]]
# # redis server address
# Address = "127.0.0.1:6379"
# # redis server password
# Password = ""
# # collect interval this target
# IntervalSeconds = 15

View File

@ -1,5 +1,5 @@
# whether collect metric: system_n_users
CollectUserNumber = false
# # whether collect metric: system_n_users
# CollectUserNumber = false
# # collect interval, default 15s
# IntervalSeconds = 15

View File

@ -6,17 +6,19 @@ import (
"os"
"path"
"strconv"
"time"
"flashcat.cloud/categraf/pkg/cfg"
"github.com/toolkits/pkg/file"
)
type Global struct {
PrintConfig bool
Hostname string
OmitHostname bool
Labels map[string]string
Precision string
PrintConfig bool
Hostname string
OmitHostname bool
Labels map[string]string
Precision string
IntervalSeconds int64
}
type WriterOpt struct {
@ -81,6 +83,10 @@ func InitConfig(configDir, debugMode string, testMode bool) error {
Config.Global.Hostname = name
}
if Config.Global.IntervalSeconds <= 0 {
Config.Global.IntervalSeconds = 15
}
if Config.Global.PrintConfig {
bs, _ := json.MarshalIndent(Config, "", " ")
fmt.Println(string(bs))
@ -88,3 +94,7 @@ func InitConfig(configDir, debugMode string, testMode bool) error {
return nil
}
func GetInterval() time.Duration {
return time.Duration(Config.Global.IntervalSeconds) * time.Second
}

View File

@ -7,32 +7,26 @@ import (
"strings"
"time"
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/types"
)
const InputName = "redis"
var (
DefaultInterval = time.Second * 30
DefaultTimeout = time.Second * 20
)
type Target struct {
IntervalSeconds int64
TimeoutSeconds int64
Labels map[string]string
quit chan struct{}
Address string
Password string
quit chan struct{}
}
type Redis struct {
PrintConfigs bool
IntervalSeconds int64
TimeoutSeconds int64
Labels map[string]string
Targets []*Target
@ -44,11 +38,6 @@ func (r *Redis) TidyConfig() error {
bs, _ := json.MarshalIndent(r, "", " ")
fmt.Println(string(bs))
}
if len(r.Targets) == 0 {
log.Println("I! [redis] Targets is empty")
}
return nil
}
@ -76,17 +65,18 @@ func (t *Target) getInterval(r *Redis) time.Duration {
return time.Duration(r.IntervalSeconds) * time.Second
}
return DefaultInterval
return config.GetInterval()
}
func (t *Target) LoopGather(r *Redis, queue chan *types.Sample) {
interval := t.getInterval(r)
for {
select {
case <-t.quit:
close(t.quit)
return
default:
time.Sleep(t.getInterval(r))
time.Sleep(interval)
defer func() {
if r := recover(); r != nil {
if strings.Contains(fmt.Sprint(r), "closed channel") {

View File

@ -17,10 +17,6 @@ import (
const InputName = "system"
var (
DefaultInterval = time.Second * 15
)
type SystemStats struct {
PrintConfigs bool
IntervalSeconds int64
@ -35,7 +31,7 @@ func (s *SystemStats) getInterval() time.Duration {
if s.IntervalSeconds != 0 {
return time.Duration(s.IntervalSeconds) * time.Second
}
return DefaultInterval
return config.GetInterval()
}
// overwrite func
@ -66,7 +62,7 @@ func (s *SystemStats) LoopGather(queue chan *types.Sample) {
if strings.Contains(fmt.Sprint(r), "closed channel") {
return
} else {
log.Println("E! gather system metrics panic:", r)
log.Println("E! gather metrics panic:", r)
}
}
}()