From 7ae6a1dc2bdd6948d2cb3c48562a1d630931a178 Mon Sep 17 00:00:00 2001 From: quzard <1191890118@qq.com> Date: Thu, 16 Jan 2025 20:21:36 +0800 Subject: [PATCH] Read new environment variables when calling LOAD_PARAMETER and LoadSingleValueEnvConfig (#2042) --- core/app_config/AppConfig.cpp | 16 ++++++++++ core/app_config/AppConfig.h | 1 + core/common/JsonUtil.cpp | 10 +++++- .../unittest/app_config/AppConfigUnittest.cpp | 31 +++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/core/app_config/AppConfig.cpp b/core/app_config/AppConfig.cpp index 2e939bcfab..af966d907f 100644 --- a/core/app_config/AppConfig.cpp +++ b/core/app_config/AppConfig.cpp @@ -24,6 +24,7 @@ #include "json/value.h" #include "RuntimeUtil.h" +#include "StringTools.h" #include "common/EnvUtil.h" #include "common/FileSystemUtil.h" #include "common/JsonUtil.h" @@ -847,6 +848,21 @@ bool LoadSingleValueEnvConfig(const char* envKey, T& configValue, const T minVal } catch (const exception& e) { LOG_WARNING(sLogger, (string("set ") + envKey + " from env failed, exception", e.what())); } + try { + char* value = NULL; + const auto newEnvKey = LOONGCOLLECTOR_ENV_PREFIX + ToUpperCaseString(envKey); + value = getenv(newEnvKey.c_str()); + if (value != NULL) { + T val = StringTo(value); + if (val >= minValue) { + configValue = val; + LOG_INFO(sLogger, (string("set ") + envKey + " from env, value", value)); + return true; + } + } + } catch (const exception& e) { + LOG_WARNING(sLogger, (string("set ") + envKey + " from env failed, exception", e.what())); + } return false; } diff --git a/core/app_config/AppConfig.h b/core/app_config/AppConfig.h index 35e9b991e1..c6458f644d 100644 --- a/core/app_config/AppConfig.h +++ b/core/app_config/AppConfig.h @@ -38,6 +38,7 @@ extern const uint32_t CONCURRENCY_STATISTIC_THRESHOLD; extern const uint32_t CONCURRENCY_STATISTIC_INTERVAL_THRESHOLD_SECONDS; extern const uint32_t NO_FALL_BACK_FAIL_PERCENTAGE; extern const uint32_t SLOW_FALL_BACK_FAIL_PERCENTAGE; +extern const std::string LOONGCOLLECTOR_ENV_PREFIX; void CreateAgentDir(); diff --git a/core/common/JsonUtil.cpp b/core/common/JsonUtil.cpp index 288f67283f..b182fcd61f 100644 --- a/core/common/JsonUtil.cpp +++ b/core/common/JsonUtil.cpp @@ -17,6 +17,7 @@ #include #include +#include "AppConfig.h" #include "common/ExceptionBase.h" #include "common/StringTools.h" #include "logger/Logger.h" @@ -194,10 +195,17 @@ bool LoadEnvValueIfExisting(const char* envKey, T& cfgValue) { loaded = true; \ APSARA_LOG_INFO(sLogger, ("load parameter from env", name)("value", outVal)); \ } \ - if (envName != NULL && LoadEnvValueIfExisting(envName, value)) { \ + if (envName != NULL && LoadEnvValueIfExisting(envName, outVal)) { \ loaded = true; \ APSARA_LOG_INFO(sLogger, ("load parameter from env", envName)("value", outVal)); \ } \ + if (name != NULL) { \ + const auto newEnvName = LOONGCOLLECTOR_ENV_PREFIX + ToUpperCaseString(name); \ + if (LoadEnvValueIfExisting(newEnvName.c_str(), outVal)) { \ + loaded = true; \ + APSARA_LOG_INFO(sLogger, ("load parameter from env", newEnvName)("value", outVal)); \ + } \ + } \ if (name != NULL && (confJSON.isMember(name) && confJSON[name].testJSONFunc())) { \ outVal = confJSON[name].convertJSONFunc(); \ loaded = true; \ diff --git a/core/unittest/app_config/AppConfigUnittest.cpp b/core/unittest/app_config/AppConfigUnittest.cpp index bba92f4564..7552f65782 100644 --- a/core/unittest/app_config/AppConfigUnittest.cpp +++ b/core/unittest/app_config/AppConfigUnittest.cpp @@ -37,6 +37,8 @@ class AppConfigUnittest : public ::testing::Test { public: void TestRecurseParseJsonToFlags(); void TestParseEnvToFlags(); + void TestLoadSingleValueEnvConfig(); + void TestLoadStringParameter(); private: void writeLogtailConfigJSON(const Json::Value& v) { @@ -197,8 +199,37 @@ void AppConfigUnittest::TestParseEnvToFlags() { } } +void AppConfigUnittest::TestLoadSingleValueEnvConfig() { + SetEnv("cpu_usage_limit", "0.5"); + AppConfig::GetInstance()->LoadEnvResourceLimit(); + APSARA_TEST_EQUAL(AppConfig::GetInstance()->GetCpuUsageUpLimit(), 0.5); + UnsetEnv("cpu_usage_limit"); + SetEnv("LOONG_CPU_USAGE_LIMIT", "0.6"); + AppConfig::GetInstance()->LoadEnvResourceLimit(); + APSARA_TEST_EQUAL(AppConfig::GetInstance()->GetCpuUsageUpLimit(), float(0.6)); + UnsetEnv("LOONG_CPU_USAGE_LIMIT"); +} + +void AppConfigUnittest::TestLoadStringParameter() { + Json::Value value; + std::string res; + SetEnv("cpu_usage_limit_env", "0.5"); + LoadStringParameter(res, value, "cpu_usage_limit", "cpu_usage_limit_env"); + APSARA_TEST_EQUAL(res, "0.5"); + + SetEnv("LOONG_CPU_USAGE_LIMIT", "0.6"); + LoadStringParameter(res, value, "cpu_usage_limit", "cpu_usage_limit_env"); + APSARA_TEST_EQUAL(res, "0.6"); + + value["cpu_usage_limit"] = "0.7"; + LoadStringParameter(res, value, "cpu_usage_limit", "cpu_usage_limit_env"); + APSARA_TEST_EQUAL(res, "0.7"); +} + UNIT_TEST_CASE(AppConfigUnittest, TestRecurseParseJsonToFlags); UNIT_TEST_CASE(AppConfigUnittest, TestParseEnvToFlags); +UNIT_TEST_CASE(AppConfigUnittest, TestLoadSingleValueEnvConfig); +UNIT_TEST_CASE(AppConfigUnittest, TestLoadStringParameter); } // namespace logtail