1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

Handle empty env vars as it they were not set (#3764)

* Handle empty env vars as it they were not set

* Add changelog entry

* Add missing unit test
This commit is contained in:
Robert Pająk
2023-02-28 21:43:48 +01:00
committed by GitHub
parent 2e54fbb3fe
commit d0e4a438ef
5 changed files with 15 additions and 7 deletions
+5 -5
View File
@@ -70,8 +70,8 @@ const (
// returned.
func firstInt(defaultValue int, keys ...string) int {
for _, key := range keys {
value, ok := os.LookupEnv(key)
if !ok {
value := os.Getenv(key)
if value == "" {
continue
}
@@ -88,10 +88,10 @@ func firstInt(defaultValue int, keys ...string) int {
}
// IntEnvOr returns the int value of the environment variable with name key if
// it exists and the value is an int. Otherwise, defaultValue is returned.
// it exists, it is not empty, and the value is an int. Otherwise, defaultValue is returned.
func IntEnvOr(key string, defaultValue int) int {
value, ok := os.LookupEnv(key)
if !ok {
value := os.Getenv(key)
if value == "" {
return defaultValue
}
+4
View File
@@ -96,6 +96,7 @@ func TestEnvParse(t *testing.T) {
envVal = 2500
envValStr = "2500"
invalid = "localhost"
empty = ""
)
for _, tc := range testCases {
@@ -113,6 +114,9 @@ func TestEnvParse(t *testing.T) {
require.NoError(t, os.Setenv(key, invalid))
assert.Equal(t, defVal, tc.f(defVal), "invalid value")
require.NoError(t, os.Setenv(key, empty))
assert.Equal(t, defVal, tc.f(defVal), "empty value")
})
}
})