Skip to content

Commit

Permalink
Read new environment variables when calling LOAD_PARAMETER and LoadSi…
Browse files Browse the repository at this point in the history
…ngleValueEnvConfig (#2042)
  • Loading branch information
quzard authored Jan 16, 2025
1 parent 92aefd4 commit 7ae6a1d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
16 changes: 16 additions & 0 deletions core/app_config/AppConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<T>(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;
}

Expand Down
1 change: 1 addition & 0 deletions core/app_config/AppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
10 changes: 9 additions & 1 deletion core/common/JsonUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <memory>
#include <sstream>

#include "AppConfig.h"
#include "common/ExceptionBase.h"
#include "common/StringTools.h"
#include "logger/Logger.h"
Expand Down Expand Up @@ -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; \
Expand Down
31 changes: 31 additions & 0 deletions core/unittest/app_config/AppConfigUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 7ae6a1d

Please sign in to comment.