Skip to content

Commit

Permalink
[configtelemetry] Update strings returned by `Level.[String|MarshalTe…
Browse files Browse the repository at this point in the history
…xt]`

  - All returned strings are capitalized.
  - "" is returned for integers that are out of Level enum range.
  - It also affects `Level.MarshalText` output, but it's not a problem because `UnmarshalText` method accepts strings in
  all cases, e.g. "normal", "Normal" and "NORMAL".
  • Loading branch information
dmitryax committed Nov 12, 2022
1 parent d55b152 commit 0c98c3e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
9 changes: 9 additions & 0 deletions .chloggen/level-update-string.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
change_type: breaking
component: configtelemetry
note: Update values returned by `Level.String` and `Level.MarshalText` method.
issues: [6490]
subtext: |
- All returned strings are capitalized.
- "" is returned for integers that are out of Level enum range.
- It also affects `Level.Marshal` output, but it's not a problem because `Unmarshal` method accepts strings in
all cases, e.g. "normal", "Normal" and "NORMAL".
18 changes: 9 additions & 9 deletions config/configtelemetry/configtelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ const (
// LevelDetailed adds dimensions and views to the previous levels.
LevelDetailed

levelNoneStr = "none"
levelBasicStr = "basic"
levelNormalStr = "normal"
levelDetailedStr = "detailed"
levelNoneStr = "None"
levelBasicStr = "Basic"
levelNormalStr = "Normal"
levelDetailedStr = "Detailed"
)

// Level is the level of internal telemetry (metrics, logs, traces about the component itself)
Expand All @@ -55,7 +55,7 @@ func (l Level) String() string {
case LevelDetailed:
return levelDetailedStr
}
return "unknown"
return ""
}

// MarshalText marshals Level to text.
Expand All @@ -71,16 +71,16 @@ func (l *Level) UnmarshalText(text []byte) error {

str := strings.ToLower(string(text))
switch str {
case levelNoneStr:
case strings.ToLower(levelNoneStr):
*l = LevelNone
return nil
case levelBasicStr:
case strings.ToLower(levelBasicStr):
*l = LevelBasic
return nil
case levelNormalStr:
case strings.ToLower(levelNormalStr):
*l = LevelNormal
return nil
case levelDetailedStr:
case strings.ToLower(levelDetailedStr):
*l = LevelDetailed
return nil
}
Expand Down
43 changes: 20 additions & 23 deletions config/configtelemetry/configtelemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,46 @@ import (

func TestUnmarshalText(t *testing.T) {
tests := []struct {
str string
str []string
level Level
err bool
}{
{
str: "",
str: []string{"", "other_string"},
level: LevelNone,
err: true,
},
{
str: "other_string",
level: LevelNone,
err: true,
},
{
str: levelNoneStr,
str: []string{"none", "None", "NONE"},
level: LevelNone,
},
{
str: levelBasicStr,
str: []string{"basic", "Basic", "BASIC"},
level: LevelBasic,
},
{
str: levelNormalStr,
str: []string{"normal", "Normal", "NORMAL"},
level: LevelNormal,
},
{
str: levelDetailedStr,
str: []string{"detailed", "Detailed", "DETAILED"},
level: LevelDetailed,
},
}

for _, test := range tests {
t.Run(test.str, func(t *testing.T) {
var lvl Level
err := lvl.UnmarshalText([]byte(test.str))
if test.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, test.level, lvl)
}
})
for _, str := range test.str {
t.Run(str, func(t *testing.T) {
var lvl Level
err := lvl.UnmarshalText([]byte(str))
if test.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, test.level, lvl)
}
})
}
}
}

Expand All @@ -73,14 +70,14 @@ func TestUnmarshalTextNilLevel(t *testing.T) {
assert.Error(t, lvl.UnmarshalText([]byte(levelNormalStr)))
}

func TestLevelString(t *testing.T) {
func TestLevelStringMarshal(t *testing.T) {
tests := []struct {
str string
level Level
err bool
}{
{
str: "unknown",
str: "",
level: Level(-10),
},
{
Expand Down
2 changes: 1 addition & 1 deletion exporter/loggingexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestValidate(t *testing.T) {
cfg: &Config{
Verbosity: configtelemetry.LevelNone,
},
expectedErr: "verbosity level \"none\" is not supported",
expectedErr: "verbosity level \"None\" is not supported",
},
{
name: "verbosity detailed",
Expand Down

0 comments on commit 0c98c3e

Please sign in to comment.