Skip to content

Commit

Permalink
Handle empty env vars as it they were not set (open-telemetry#3764)
Browse files Browse the repository at this point in the history
* Handle empty env vars as it they were not set

* Add changelog entry

* Add missing unit test
  • Loading branch information
pellared authored Feb 28, 2023
1 parent 2e54fbb commit d0e4a43
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down
2 changes: 1 addition & 1 deletion exporters/jaeger/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion exporters/zipkin/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions sdk/internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions sdk/internal/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestEnvParse(t *testing.T) {
envVal = 2500
envValStr = "2500"
invalid = "localhost"
empty = ""
)

for _, tc := range testCases {
Expand All @@ -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")
})
}
})
Expand Down

0 comments on commit d0e4a43

Please sign in to comment.