1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-18 03:22:12 +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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 7 deletions

View File

@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [Unreleased]
### Fixed
- Handle empty environment variable as it they were not set. (#3764)
## [1.14.0/0.37.0/0.0.4] 2023-02-27
This release is the last to support [Go 1.18].

View File

@ -37,7 +37,7 @@ const (
// envOr returns an env variable's value if it is exists or the default if not.
func envOr(key, defaultValue string) string {
if v, ok := os.LookupEnv(key); ok && v != "" {
if v := os.Getenv(key); v != "" {
return v
}
return defaultValue

View File

@ -24,7 +24,7 @@ const (
// envOr returns an env variable's value if it is exists or the default if not.
func envOr(key, defaultValue string) string {
if v, ok := os.LookupEnv(key); ok && v != "" {
if v := os.Getenv(key); v != "" {
return v
}
return defaultValue

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
}

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")
})
}
})