Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getters for the nodetreemodel #31043

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions comp/dogstatsd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,11 +821,7 @@ func getBuckets(cfg model.Reader, logger log.Component, option string) []float64
return nil
}

buckets, err := cfg.GetFloat64SliceE(option)
hush-hush marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logger.Errorf("%s, falling back to default values", err)
return nil
}
buckets := cfg.GetFloat64Slice(option)
if len(buckets) == 0 {
logger.Debugf("'%s' is empty, falling back to default values", option)
return nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/config/model/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ type Reader interface {
GetInt32(key string) int32
GetInt64(key string) int64
GetFloat64(key string) float64
GetTime(key string) time.Time
GetDuration(key string) time.Duration
GetStringSlice(key string) []string
GetFloat64SliceE(key string) ([]float64, error)
GetFloat64Slice(key string) []float64
GetStringMap(key string) map[string]interface{}
GetStringMapString(key string) map[string]string
GetStringMapStringSlice(key string) map[string][]string
Expand Down
22 changes: 6 additions & 16 deletions pkg/config/model/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,18 +414,6 @@ func (c *safeConfig) GetFloat64(key string) float64 {
return val
}

// GetTime wraps Viper for concurrent access
func (c *safeConfig) GetTime(key string) time.Time {
c.RLock()
defer c.RUnlock()
c.checkKnownKey(key)
val, err := c.Viper.GetTimeE(key)
if err != nil {
log.Warnf("failed to get configuration value for key %q: %s", key, err)
}
return val
}

// GetDuration wraps Viper for concurrent access
func (c *safeConfig) GetDuration(key string) time.Duration {
c.RLock()
Expand All @@ -451,26 +439,28 @@ func (c *safeConfig) GetStringSlice(key string) []string {
}

// GetFloat64SliceE loads a key as a []float64
func (c *safeConfig) GetFloat64SliceE(key string) ([]float64, error) {
func (c *safeConfig) GetFloat64Slice(key string) []float64 {
c.RLock()
defer c.RUnlock()
c.checkKnownKey(key)

// We're using GetStringSlice because viper can only parse list of string from env variables
list, err := c.Viper.GetStringSliceE(key)
if err != nil {
return nil, fmt.Errorf("'%v' is not a list", key)
log.Warnf("'%v' is not a list", key)
return nil
}

res := []float64{}
for _, item := range list {
nb, err := strconv.ParseFloat(item, 64)
if err != nil {
return nil, fmt.Errorf("value '%v' from '%v' is not a float64", item, key)
log.Warnf("value '%v' from '%v' is not a float64", item, key)
return nil
}
res = append(res, nb)
}
return res, nil
return res
}

// GetStringMap wraps Viper for concurrent access
Expand Down
52 changes: 0 additions & 52 deletions pkg/config/model/viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package model

import (
"bytes"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -111,57 +110,6 @@ func TestGetConfigEnvVarsDedupe(t *testing.T) {
assert.Equal(t, 1, count)
}

func TestGetFloat64SliceE(t *testing.T) {
config := NewConfig("test", "DD", strings.NewReplacer(".", "_")) // nolint: forbidigo

config.BindEnv("float_list")
config.SetConfigType("yaml")
yamlExample := []byte(`---
float_list:
- 1.1
- "2.2"
- 3.3
`)
config.ReadConfig(bytes.NewBuffer(yamlExample))

list, err := config.GetFloat64SliceE("float_list")
assert.NoError(t, err)
assert.Equal(t, []float64{1.1, 2.2, 3.3}, list)

yamlExample = []byte(`---
float_list:
- a
- 2.2
- 3.3
`)
config.ReadConfig(bytes.NewBuffer(yamlExample))

list, err = config.GetFloat64SliceE("float_list")
assert.NotNil(t, err)
assert.Equal(t, "value 'a' from 'float_list' is not a float64", err.Error())
assert.Nil(t, list)
}

func TestGetFloat64SliceEEnv(t *testing.T) {
config := NewConfig("test", "DD", strings.NewReplacer(".", "_")) // nolint: forbidigo

config.BindEnv("float_list")
config.SetConfigType("yaml")

yamlExample := []byte(`
float_list:
- 25
`)

config.ReadConfig(bytes.NewBuffer(yamlExample))

t.Setenv("DD_FLOAT_LIST", "1.1 2.2 3.3")

list, err := config.GetFloat64SliceE("float_list")
assert.NoError(t, err)
assert.Equal(t, []float64{1.1, 2.2, 3.3}, list)
}

func TestSet(t *testing.T) {
config := NewConfig("test", "DD", strings.NewReplacer(".", "_")) // nolint: forbidigo
config.Set("foo", "bar", SourceFile)
Expand Down
Loading
Loading