8426: Refactored config to a string to make it more intuitive to the user, and to make setting a default easier
parent
0fb2de16ee
commit
3ca8b90f6c
|
@ -49,8 +49,8 @@ const (
|
|||
// DefaultAuthFile is the default location of the user/password file.
|
||||
DefaultAuthFile = "/etc/collectd/auth_file"
|
||||
|
||||
// DefaultUsePluginTuple is false, defaulting to version <1.2 where plugin values were split into separate rows
|
||||
DefaultUsePluginTuple = false
|
||||
// DefaultParseMultiValuePlugin is false, defaulting to version <1.2 where plugin values were split into separate rows
|
||||
DefaultParseMultiValuePlugin = "split"
|
||||
)
|
||||
|
||||
// Config represents a configuration for the collectd service.
|
||||
|
@ -66,7 +66,7 @@ type Config struct {
|
|||
TypesDB string `toml:"typesdb"`
|
||||
SecurityLevel string `toml:"security-level"`
|
||||
AuthFile string `toml:"auth-file"`
|
||||
UsePluginTuple bool `toml:"use-plugin-tuple"`
|
||||
ParseMultiValuePlugin string `toml:"parse-multivalue-plugin"`
|
||||
}
|
||||
|
||||
// NewConfig returns a new instance of Config with defaults.
|
||||
|
@ -82,7 +82,7 @@ func NewConfig() Config {
|
|||
TypesDB: DefaultTypesDB,
|
||||
SecurityLevel: DefaultSecurityLevel,
|
||||
AuthFile: DefaultAuthFile,
|
||||
UsePluginTuple: DefaultUsePluginTuple,
|
||||
ParseMultiValuePlugin: DefaultParseMultiValuePlugin,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,7 +120,9 @@ func (c *Config) WithDefaults() *Config {
|
|||
if d.AuthFile == "" {
|
||||
d.AuthFile = DefaultAuthFile
|
||||
}
|
||||
// UsePluginTuple will default to false if unassigned so no need to change anything here.
|
||||
if d.ParseMultiValuePlugin == "" {
|
||||
d.ParseMultiValuePlugin = DefaultParseMultiValuePlugin
|
||||
}
|
||||
|
||||
return &d
|
||||
}
|
||||
|
@ -133,6 +135,12 @@ func (c *Config) Validate() error {
|
|||
return errors.New("Invalid security level")
|
||||
}
|
||||
|
||||
switch c.ParseMultiValuePlugin {
|
||||
case "split", "join":
|
||||
default:
|
||||
return errors.New("Invalid value for parse-multivalue-plugin. Valid options are \"split\" and \"join\"")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -355,7 +355,7 @@ func (s *Service) handleMessage(buffer []byte) {
|
|||
}
|
||||
var points []models.Point
|
||||
for _, valueList := range valueLists {
|
||||
if s.Config.UsePluginTuple {
|
||||
if s.Config.ParseMultiValuePlugin == "join" {
|
||||
points = s.UnmarshalValueListPacked(valueList)
|
||||
} else {
|
||||
points = s.UnmarshalValueList(valueList)
|
||||
|
|
|
@ -19,7 +19,7 @@ import (
|
|||
)
|
||||
|
||||
func TestService_OpenClose(t *testing.T) {
|
||||
service := NewTestService(1, time.Second, false)
|
||||
service := NewTestService(1, time.Second, "split")
|
||||
|
||||
// Closing a closed service is fine.
|
||||
if err := service.Service.Close(); err != nil {
|
||||
|
@ -114,7 +114,7 @@ func TestService_Open_TypesDBDir(t *testing.T) {
|
|||
func TestService_CreatesDatabase(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
s := NewTestService(1, time.Second, false)
|
||||
s := NewTestService(1, time.Second, "split")
|
||||
|
||||
s.WritePointsFn = func(string, string, models.ConsistencyLevel, []models.Point) error {
|
||||
return nil
|
||||
|
@ -198,7 +198,7 @@ func TestService_BatchSize(t *testing.T) {
|
|||
|
||||
for _, batchSize := range batchSizes {
|
||||
func() {
|
||||
s := NewTestService(batchSize, time.Second, false)
|
||||
s := NewTestService(batchSize, time.Second, "split")
|
||||
|
||||
pointCh := make(chan models.Point)
|
||||
s.WritePointsFn = func(database, retentionPolicy string, consistencyLevel models.ConsistencyLevel, points []models.Point) error {
|
||||
|
@ -261,12 +261,12 @@ func TestService_BatchSize(t *testing.T) {
|
|||
}
|
||||
|
||||
// Test that the collectd service correctly batches points by BatchSize.
|
||||
func TestService_PluginTuple(t *testing.T) {
|
||||
func TestService_ParseMultiValuePlugin(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
totalPoints := len(expPointsTupled)
|
||||
|
||||
s := NewTestService(1, time.Second, true)
|
||||
s := NewTestService(1, time.Second, "join")
|
||||
|
||||
pointCh := make(chan models.Point, 1000)
|
||||
s.WritePointsFn = func(database, retentionPolicy string, consistencyLevel models.ConsistencyLevel, points []models.Point) error {
|
||||
|
@ -326,7 +326,7 @@ func TestService_BatchDuration(t *testing.T) {
|
|||
|
||||
totalPoints := len(expPoints)
|
||||
|
||||
s := NewTestService(5000, 250*time.Millisecond, false)
|
||||
s := NewTestService(5000, 250*time.Millisecond, "split")
|
||||
|
||||
pointCh := make(chan models.Point, 1000)
|
||||
s.WritePointsFn = func(database, retentionPolicy string, consistencyLevel models.ConsistencyLevel, points []models.Point) error {
|
||||
|
@ -389,13 +389,13 @@ type TestService struct {
|
|||
WritePointsFn func(string, string, models.ConsistencyLevel, []models.Point) error
|
||||
}
|
||||
|
||||
func NewTestService(batchSize int, batchDuration time.Duration, useTuple bool) *TestService {
|
||||
func NewTestService(batchSize int, batchDuration time.Duration, parseOpt string) *TestService {
|
||||
c := Config{
|
||||
BindAddress: "127.0.0.1:0",
|
||||
Database: "collectd_test",
|
||||
BatchSize: batchSize,
|
||||
BatchDuration: toml.Duration(batchDuration),
|
||||
UsePluginTuple: useTuple,
|
||||
ParseMultiValuePlugin: parseOpt,
|
||||
}
|
||||
|
||||
s := &TestService{
|
||||
|
|
Loading…
Reference in New Issue