Skip to content

Commit

Permalink
Improved logging
Browse files Browse the repository at this point in the history
  • Loading branch information
JoukoVirtanen committed Oct 5, 2024
1 parent eca3531 commit fd7e684
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
17 changes: 14 additions & 3 deletions collector/lib/CollectorConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,28 +411,39 @@ void CollectorConfig::HandleConfigMapString(const std::string& jsonString) {
auto status = google::protobuf::util::JsonStringToMessage(jsonString, &config);

if (!status.ok()) {
CLOG(WARNING) << "Failed to parse config";
CLOG(ERROR) << "Failed to parse runtime config. Error: " << status.message();
CLOG(ERROR) << "ConfigMap file contents: " << jsonString;
} else {
SetRuntimeConfig(config);
CLOG(INFO) << "Set the config using a configmap";
CLOG(INFO) << "Set the runtime config using a configmap";
CLOG(INFO) << config.DebugString();
}
}

std::string readJsonFileToString(const std::filesystem::path& filePath) {
std::ifstream fileStream(filePath);
if (!fileStream.is_open()) {
CLOG(WARNING) << "Unable to open file: " << filePath;
CLOG(WARNING) << "Unable to open ConfigMap file: " << filePath
<< ". File may not exist or permissions may be incorrect. "
<< "If a ConfigMap was not created intentionally, which is the default, "
<< "no action is needed.";
return "";
}

std::string content((std::istreambuf_iterator<char>(fileStream)),
std::istreambuf_iterator<char>());

if (content.empty()) {
CLOG(WARNING) << "ConfigMap file is empty: " << filePath;
}
return content;
}

void CollectorConfig::HandleConfigMap(const std::filesystem::path& filePath) {
std::string jsonConfig = readJsonFileToString(filePath);
if (jsonConfig.empty()) {
CLOG(WARNING) << "No runtime configuration specified in ConfigMap.";
}
HandleConfigMapString(jsonConfig);
}

Expand Down
36 changes: 30 additions & 6 deletions collector/test/CollectorConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ TEST(CollectorConfigTest, TestEnableExternalIpsRuntimeConfig) {

TEST(CollectorConfigTest, TestConfigMapTrue) {
std::string jsonStr = R"({
"networkConnectionConfig": {
"enableExternalIps": true
}
"networkConnectionConfig": {
"enableExternalIps": true
}
})";

MockCollectorConfig config;
Expand All @@ -166,9 +166,9 @@ TEST(CollectorConfigTest, TestConfigMapTrue) {

TEST(CollectorConfigTest, TestConfigMapFalse) {
std::string jsonStr = R"({
"networkConnectionConfig": {
"enableExternalIps": false
}
"networkConnectionConfig": {
"enableExternalIps": false
}
})";

MockCollectorConfig config;
Expand All @@ -177,4 +177,28 @@ TEST(CollectorConfigTest, TestConfigMapFalse) {
EXPECT_FALSE(config.EnableExternalIPs());
}

TEST(CollectorConfigTest, TestConfigMapEmpty) {
std::string jsonStr = R"({
"networkConnectionConfig": {
}
})";

MockCollectorConfig config;
config.MockHandleConfigMapString(jsonStr);

EXPECT_FALSE(config.EnableExternalIPs());
}

TEST(CollectorConfigTest, TestConfigMapInvalid) {
std::string jsonStr = R"({
"networkConnectionConfig": {
}
)";

MockCollectorConfig config;
config.MockHandleConfigMapString(jsonStr);

EXPECT_FALSE(config.EnableExternalIPs());
}

} // namespace collector

0 comments on commit fd7e684

Please sign in to comment.