From d0e4a438ef9ba83a67252cab9c6606b5d7bdd95b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Tue, 28 Feb 2023 21:43:48 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 4 ++++ exporters/jaeger/env.go | 2 +- exporters/zipkin/env.go | 2 +- sdk/internal/env/env.go | 10 +++++----- sdk/internal/env/env_test.go | 4 ++++ 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d9726f60..75003be60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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]. diff --git a/exporters/jaeger/env.go b/exporters/jaeger/env.go index a7253e483..460fb5e13 100644 --- a/exporters/jaeger/env.go +++ b/exporters/jaeger/env.go @@ -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 diff --git a/exporters/zipkin/env.go b/exporters/zipkin/env.go index 7f1b60fa4..f1afd8e86 100644 --- a/exporters/zipkin/env.go +++ b/exporters/zipkin/env.go @@ -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 diff --git a/sdk/internal/env/env.go b/sdk/internal/env/env.go index 5e94b8ae5..59dcfab25 100644 --- a/sdk/internal/env/env.go +++ b/sdk/internal/env/env.go @@ -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 } diff --git a/sdk/internal/env/env_test.go b/sdk/internal/env/env_test.go index f456ae108..c2b3b7437 100644 --- a/sdk/internal/env/env_test.go +++ b/sdk/internal/env/env_test.go @@ -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") }) } })