8426: Refactored config to a string to make it more intuitive to the user, and to make setting a default easier

pull/8523/head
Adam 2017-06-23 09:12:52 -04:00
parent 0fb2de16ee
commit 3ca8b90f6c
3 changed files with 47 additions and 39 deletions

View File

@ -49,40 +49,40 @@ 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.
type Config struct {
Enabled bool `toml:"enabled"`
BindAddress string `toml:"bind-address"`
Database string `toml:"database"`
RetentionPolicy string `toml:"retention-policy"`
BatchSize int `toml:"batch-size"`
BatchPending int `toml:"batch-pending"`
BatchDuration toml.Duration `toml:"batch-timeout"`
ReadBuffer int `toml:"read-buffer"`
TypesDB string `toml:"typesdb"`
SecurityLevel string `toml:"security-level"`
AuthFile string `toml:"auth-file"`
UsePluginTuple bool `toml:"use-plugin-tuple"`
Enabled bool `toml:"enabled"`
BindAddress string `toml:"bind-address"`
Database string `toml:"database"`
RetentionPolicy string `toml:"retention-policy"`
BatchSize int `toml:"batch-size"`
BatchPending int `toml:"batch-pending"`
BatchDuration toml.Duration `toml:"batch-timeout"`
ReadBuffer int `toml:"read-buffer"`
TypesDB string `toml:"typesdb"`
SecurityLevel string `toml:"security-level"`
AuthFile string `toml:"auth-file"`
ParseMultiValuePlugin string `toml:"parse-multivalue-plugin"`
}
// NewConfig returns a new instance of Config with defaults.
func NewConfig() Config {
return Config{
BindAddress: DefaultBindAddress,
Database: DefaultDatabase,
RetentionPolicy: DefaultRetentionPolicy,
ReadBuffer: DefaultReadBuffer,
BatchSize: DefaultBatchSize,
BatchPending: DefaultBatchPending,
BatchDuration: DefaultBatchDuration,
TypesDB: DefaultTypesDB,
SecurityLevel: DefaultSecurityLevel,
AuthFile: DefaultAuthFile,
UsePluginTuple: DefaultUsePluginTuple,
BindAddress: DefaultBindAddress,
Database: DefaultDatabase,
RetentionPolicy: DefaultRetentionPolicy,
ReadBuffer: DefaultReadBuffer,
BatchSize: DefaultBatchSize,
BatchPending: DefaultBatchPending,
BatchDuration: DefaultBatchDuration,
TypesDB: DefaultTypesDB,
SecurityLevel: DefaultSecurityLevel,
AuthFile: DefaultAuthFile,
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
}

View File

@ -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)

View File

@ -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,
BindAddress: "127.0.0.1:0",
Database: "collectd_test",
BatchSize: batchSize,
BatchDuration: toml.Duration(batchDuration),
ParseMultiValuePlugin: parseOpt,
}
s := &TestService{