You've already forked opentelemetry-go
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:
Vendored
+5
-5
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Vendored
+4
@@ -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")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user