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:
parent
2e54fbb3fe
commit
d0e4a438ef
@ -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].
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
10
sdk/internal/env/env.go
vendored
10
sdk/internal/env/env.go
vendored
@ -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
sdk/internal/env/env_test.go
vendored
4
sdk/internal/env/env_test.go
vendored
@ -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")
|
||||
})
|
||||
}
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user