Skip to content

Commit d6e4d5b

Browse files
ROX-26516: Introspection endpoint for runtime config (#1879)
1 parent d108359 commit d6e4d5b

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "CollectorConfigInspector.h"
2+
3+
#include <Logging.h>
4+
#include <string>
5+
6+
#include <google/protobuf/util/json_util.h>
7+
8+
namespace collector {
9+
10+
const std::string CollectorConfigInspector::kBaseRoute = "/state/config";
11+
12+
CollectorConfigInspector::CollectorConfigInspector(const std::shared_ptr<CollectorConfig> config) : config_(config) {
13+
}
14+
15+
std::string CollectorConfigInspector::configToJson(bool& isError) {
16+
const auto& runtime_config = config_->GetRuntimeConfig();
17+
18+
if (!runtime_config.has_value()) {
19+
return "{}";
20+
}
21+
22+
std::string jsonString;
23+
const auto& config = runtime_config.value();
24+
google::protobuf::util::Status status = google::protobuf::util::MessageToJsonString(config, &jsonString);
25+
26+
if (!status.ok()) {
27+
isError = true;
28+
CLOG(WARNING) << "Failed to convert protobuf object to JSON: " << status.ToString();
29+
return R"({"error": "Failed to convert protobuf object to JSON")";
30+
}
31+
32+
return jsonString;
33+
}
34+
35+
bool CollectorConfigInspector::handleGet(CivetServer* server, struct mg_connection* conn) {
36+
const mg_request_info* req_info = mg_get_request_info(conn);
37+
38+
if (req_info == nullptr) {
39+
return ServerError(conn, "unable to read request");
40+
}
41+
42+
bool isError = false;
43+
std::string jsonString = configToJson(isError);
44+
45+
if (isError) {
46+
mg_printf(conn, "HTTP/1.1 500 Internal Server Error\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n");
47+
} else {
48+
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n");
49+
}
50+
mg_printf(conn, "%s\r\n", jsonString.c_str());
51+
52+
return true;
53+
}
54+
55+
} // namespace collector
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef COLLECTOR_CONFIG_INSPECTOR_H
2+
#define COLLECTOR_CONFIG_INSPECTOR_H
3+
4+
#include <memory>
5+
6+
#include <json/writer.h>
7+
8+
#include "CollectorConfig.h"
9+
#include "IntrospectionEndpoint.h"
10+
11+
namespace collector {
12+
13+
class CollectorConfigInspector : public IntrospectionEndpoint {
14+
public:
15+
static const std::string kBaseRoute;
16+
17+
CollectorConfigInspector(const std::shared_ptr<CollectorConfig> config);
18+
19+
// implementation of CivetHandler
20+
bool handleGet(CivetServer* server, struct mg_connection* conn) override;
21+
22+
private:
23+
const std::shared_ptr<CollectorConfig> config_;
24+
std::string configToJson(bool& isError);
25+
};
26+
27+
} // namespace collector
28+
29+
#endif

collector/lib/CollectorService.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010
#include <memory>
1111

1212
#include "CivetServer.h"
13+
#include "CollectorConfigInspector.h"
1314
#include "CollectorStatsExporter.h"
1415
#include "ConnTracker.h"
1516
#include "Containers.h"
@@ -63,6 +64,7 @@ void CollectorService::RunForever() {
6364

6465
std::unique_ptr<ContainerInfoInspector> container_info_inspector;
6566
std::unique_ptr<NetworkStatusInspector> network_status_inspector;
67+
std::unique_ptr<CollectorConfigInspector> collector_config_inspector;
6668

6769
CLOG(INFO) << "Network scrape interval set to " << config_.ScrapeInterval() << " seconds";
6870

@@ -111,6 +113,8 @@ void CollectorService::RunForever() {
111113
server.addHandler(container_info_inspector->kBaseRoute, container_info_inspector.get());
112114
network_status_inspector = std::make_unique<NetworkStatusInspector>(conn_tracker);
113115
server.addHandler(network_status_inspector->kBaseRoute, network_status_inspector.get());
116+
collector_config_inspector = std::make_unique<CollectorConfigInspector>(std::make_shared<CollectorConfig>(config_));
117+
server.addHandler(collector_config_inspector->kBaseRoute, collector_config_inspector.get());
114118
}
115119

116120
system_inspector_.Init(config_, conn_tracker);

docs/troubleshooting.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,8 @@ $ curl "http://<collector>:8080/state/network/connection?container=c6f030bc4b42&
602602
]
603603
}
604604
```
605+
606+
### Config endpoint
607+
The runtime configuration can be obtained using
608+
$ curl "http://<collector>:8080/state/config"
609+

0 commit comments

Comments
 (0)