Skip to content

Commit fd7e684

Browse files
committed
Improved logging
1 parent eca3531 commit fd7e684

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

collector/lib/CollectorConfig.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,28 +411,39 @@ void CollectorConfig::HandleConfigMapString(const std::string& jsonString) {
411411
auto status = google::protobuf::util::JsonStringToMessage(jsonString, &config);
412412

413413
if (!status.ok()) {
414-
CLOG(WARNING) << "Failed to parse config";
414+
CLOG(ERROR) << "Failed to parse runtime config. Error: " << status.message();
415+
CLOG(ERROR) << "ConfigMap file contents: " << jsonString;
415416
} else {
416417
SetRuntimeConfig(config);
417-
CLOG(INFO) << "Set the config using a configmap";
418+
CLOG(INFO) << "Set the runtime config using a configmap";
418419
CLOG(INFO) << config.DebugString();
419420
}
420421
}
421422

422423
std::string readJsonFileToString(const std::filesystem::path& filePath) {
423424
std::ifstream fileStream(filePath);
424425
if (!fileStream.is_open()) {
425-
CLOG(WARNING) << "Unable to open file: " << filePath;
426+
CLOG(WARNING) << "Unable to open ConfigMap file: " << filePath
427+
<< ". File may not exist or permissions may be incorrect. "
428+
<< "If a ConfigMap was not created intentionally, which is the default, "
429+
<< "no action is needed.";
426430
return "";
427431
}
428432

429433
std::string content((std::istreambuf_iterator<char>(fileStream)),
430434
std::istreambuf_iterator<char>());
435+
436+
if (content.empty()) {
437+
CLOG(WARNING) << "ConfigMap file is empty: " << filePath;
438+
}
431439
return content;
432440
}
433441

434442
void CollectorConfig::HandleConfigMap(const std::filesystem::path& filePath) {
435443
std::string jsonConfig = readJsonFileToString(filePath);
444+
if (jsonConfig.empty()) {
445+
CLOG(WARNING) << "No runtime configuration specified in ConfigMap.";
446+
}
436447
HandleConfigMapString(jsonConfig);
437448
}
438449

collector/test/CollectorConfigTest.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ TEST(CollectorConfigTest, TestEnableExternalIpsRuntimeConfig) {
153153

154154
TEST(CollectorConfigTest, TestConfigMapTrue) {
155155
std::string jsonStr = R"({
156-
"networkConnectionConfig": {
157-
"enableExternalIps": true
158-
}
156+
"networkConnectionConfig": {
157+
"enableExternalIps": true
158+
}
159159
})";
160160

161161
MockCollectorConfig config;
@@ -166,9 +166,9 @@ TEST(CollectorConfigTest, TestConfigMapTrue) {
166166

167167
TEST(CollectorConfigTest, TestConfigMapFalse) {
168168
std::string jsonStr = R"({
169-
"networkConnectionConfig": {
170-
"enableExternalIps": false
171-
}
169+
"networkConnectionConfig": {
170+
"enableExternalIps": false
171+
}
172172
})";
173173

174174
MockCollectorConfig config;
@@ -177,4 +177,28 @@ TEST(CollectorConfigTest, TestConfigMapFalse) {
177177
EXPECT_FALSE(config.EnableExternalIPs());
178178
}
179179

180+
TEST(CollectorConfigTest, TestConfigMapEmpty) {
181+
std::string jsonStr = R"({
182+
"networkConnectionConfig": {
183+
}
184+
})";
185+
186+
MockCollectorConfig config;
187+
config.MockHandleConfigMapString(jsonStr);
188+
189+
EXPECT_FALSE(config.EnableExternalIPs());
190+
}
191+
192+
TEST(CollectorConfigTest, TestConfigMapInvalid) {
193+
std::string jsonStr = R"({
194+
"networkConnectionConfig": {
195+
}
196+
)";
197+
198+
MockCollectorConfig config;
199+
config.MockHandleConfigMapString(jsonStr);
200+
201+
EXPECT_FALSE(config.EnableExternalIPs());
202+
}
203+
180204
} // namespace collector

0 commit comments

Comments
 (0)