support arg: --test

This commit is contained in:
Ulric Qin 2022-04-13 23:35:43 +08:00
parent e892e2d2be
commit 142cfb5f8d
7 changed files with 46 additions and 19 deletions

View File

@ -19,8 +19,8 @@ import (
type Agent struct{}
func NewAgent(configDir, debugMode string) (*Agent, error) {
if err := config.InitConfig(configDir, debugMode); err != nil {
func NewAgent(configDir, debugMode string, testMode bool) (*Agent, error) {
if err := config.InitConfig(configDir, debugMode, testMode); err != nil {
return nil, fmt.Errorf("failed to init config: %v", err)
}

View File

@ -1,6 +1,8 @@
package agent
import (
"fmt"
"strings"
"time"
"flashcat.cloud/categraf/config"
@ -55,6 +57,30 @@ func consume(queue chan *types.Sample) {
}
func postSeries(series []*prompb.TimeSeries) {
if config.Config.TestMode {
for i := 0; i < len(series); i++ {
var sb strings.Builder
sb.WriteString(">> ")
for j := range series[i].Labels {
sb.WriteString(series[i].Labels[j].Name)
sb.WriteString("=")
sb.WriteString(series[i].Labels[j].Value)
sb.WriteString(" ")
}
for j := range series[i].Samples {
sb.WriteString(fmt.Sprint(series[i].Samples[j].Timestamp))
sb.WriteString(" ")
sb.WriteString(fmt.Sprint(series[i].Samples[j].Value))
}
fmt.Println(sb.String())
}
return
}
for _, w := range writer.Writers {
w.Write(series)
}

View File

@ -38,6 +38,7 @@ type WriterOption struct {
type ConfigType struct {
ConfigDir string
DebugMode bool
TestMode bool
Global Global
Writers []WriterOption
@ -45,7 +46,7 @@ type ConfigType struct {
var Config *ConfigType
func InitConfig(configDir, debugMode string) error {
func InitConfig(configDir, debugMode string, testMode bool) error {
configFile := path.Join(configDir, "config.toml")
if !file.IsExist(configFile) {
return fmt.Errorf("configuration file(%s) not found", configFile)
@ -59,6 +60,7 @@ func InitConfig(configDir, debugMode string) error {
Config = &ConfigType{
ConfigDir: configDir,
DebugMode: debug,
TestMode: testMode,
}
if err := cfg.LoadConfigs(configDir, Config); err != nil {

3
go.mod
View File

@ -10,13 +10,10 @@ require (
github.com/prometheus/common v0.32.1
github.com/prometheus/prometheus v2.5.0+incompatible
github.com/toolkits/pkg v1.2.9
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)
require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gogo/protobuf v1.1.1 // indirect

4
go.sum
View File

@ -36,13 +36,10 @@ github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
@ -489,7 +486,6 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View File

@ -14,6 +14,7 @@ import (
var (
DefaultInterval = time.Second * 30
DefaultTimeout = time.Second * 20
InputName = "redis"
)
type Target struct {
@ -109,7 +110,7 @@ func (t *Target) Gather(r *Redis, queue chan *types.Sample) {
}
func init() {
inputs.Add("redis", func() inputs.Input {
inputs.Add(InputName, func() inputs.Input {
return &Redis{}
})
}

21
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
@ -10,23 +11,27 @@ import (
"flashcat.cloud/categraf/agent"
"flashcat.cloud/categraf/pkg/osx"
"github.com/toolkits/pkg/runner"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
version = "0.0.1"
configDir = flag.String("configs", osx.GetEnv("CATEGRAF_CONFIGS", "conf"), "Specify configuration directory")
debugMode = flag.String("debug", osx.GetEnv("CATEGRAF_DEBUG", "false"), "Is debug mode?")
version = "0.0.1"
configDir = flag.String("configs", osx.GetEnv("CATEGRAF_CONFIGS", "conf"), "Specify configuration directory.(env:CATEGRAF_CONFIGS)")
debugMode = flag.String("debug", osx.GetEnv("CATEGRAF_DEBUG", "false"), "Is debug mode?.(env:CATEGRAF_DEBUG)")
testMode = flag.Bool("test", false, "Is test mode? print metrics to stdout")
showVersion = flag.Bool("version", false, "Show version.")
)
func main() {
kingpin.Version(version)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
flag.Parse()
if *showVersion {
fmt.Println(version)
os.Exit(0)
}
printEnv()
ag, err := agent.NewAgent(*configDir, *debugMode)
ag, err := agent.NewAgent(*configDir, *debugMode, *testMode)
if err != nil {
log.Println("F! failed to new agent:", err)
os.Exit(1)