From d6e4d5b379a84b845211dce48c32879ed209b6af Mon Sep 17 00:00:00 2001 From: Jouko Virtanen Date: Wed, 9 Oct 2024 17:08:31 -0700 Subject: [PATCH] ROX-26516: Introspection endpoint for runtime config (#1879) --- collector/lib/CollectorConfigInspector.cpp | 55 ++++++++++++++++++++++ collector/lib/CollectorConfigInspector.h | 29 ++++++++++++ collector/lib/CollectorService.cpp | 4 ++ docs/troubleshooting.md | 5 ++ 4 files changed, 93 insertions(+) create mode 100644 collector/lib/CollectorConfigInspector.cpp create mode 100644 collector/lib/CollectorConfigInspector.h diff --git a/collector/lib/CollectorConfigInspector.cpp b/collector/lib/CollectorConfigInspector.cpp new file mode 100644 index 0000000000..85ab65b411 --- /dev/null +++ b/collector/lib/CollectorConfigInspector.cpp @@ -0,0 +1,55 @@ +#include "CollectorConfigInspector.h" + +#include +#include + +#include + +namespace collector { + +const std::string CollectorConfigInspector::kBaseRoute = "/state/config"; + +CollectorConfigInspector::CollectorConfigInspector(const std::shared_ptr config) : config_(config) { +} + +std::string CollectorConfigInspector::configToJson(bool& isError) { + const auto& runtime_config = config_->GetRuntimeConfig(); + + if (!runtime_config.has_value()) { + return "{}"; + } + + std::string jsonString; + const auto& config = runtime_config.value(); + google::protobuf::util::Status status = google::protobuf::util::MessageToJsonString(config, &jsonString); + + if (!status.ok()) { + isError = true; + CLOG(WARNING) << "Failed to convert protobuf object to JSON: " << status.ToString(); + return R"({"error": "Failed to convert protobuf object to JSON")"; + } + + return jsonString; +} + +bool CollectorConfigInspector::handleGet(CivetServer* server, struct mg_connection* conn) { + const mg_request_info* req_info = mg_get_request_info(conn); + + if (req_info == nullptr) { + return ServerError(conn, "unable to read request"); + } + + bool isError = false; + std::string jsonString = configToJson(isError); + + if (isError) { + mg_printf(conn, "HTTP/1.1 500 Internal Server Error\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"); + } else { + mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nConnection: close\r\n\r\n"); + } + mg_printf(conn, "%s\r\n", jsonString.c_str()); + + return true; +} + +} // namespace collector diff --git a/collector/lib/CollectorConfigInspector.h b/collector/lib/CollectorConfigInspector.h new file mode 100644 index 0000000000..cda577f4ae --- /dev/null +++ b/collector/lib/CollectorConfigInspector.h @@ -0,0 +1,29 @@ +#ifndef COLLECTOR_CONFIG_INSPECTOR_H +#define COLLECTOR_CONFIG_INSPECTOR_H + +#include + +#include + +#include "CollectorConfig.h" +#include "IntrospectionEndpoint.h" + +namespace collector { + +class CollectorConfigInspector : public IntrospectionEndpoint { + public: + static const std::string kBaseRoute; + + CollectorConfigInspector(const std::shared_ptr config); + + // implementation of CivetHandler + bool handleGet(CivetServer* server, struct mg_connection* conn) override; + + private: + const std::shared_ptr config_; + std::string configToJson(bool& isError); +}; + +} // namespace collector + +#endif diff --git a/collector/lib/CollectorService.cpp b/collector/lib/CollectorService.cpp index 019798deac..c1d39e7317 100644 --- a/collector/lib/CollectorService.cpp +++ b/collector/lib/CollectorService.cpp @@ -10,6 +10,7 @@ extern "C" { #include #include "CivetServer.h" +#include "CollectorConfigInspector.h" #include "CollectorStatsExporter.h" #include "ConnTracker.h" #include "Containers.h" @@ -63,6 +64,7 @@ void CollectorService::RunForever() { std::unique_ptr container_info_inspector; std::unique_ptr network_status_inspector; + std::unique_ptr collector_config_inspector; CLOG(INFO) << "Network scrape interval set to " << config_.ScrapeInterval() << " seconds"; @@ -111,6 +113,8 @@ void CollectorService::RunForever() { server.addHandler(container_info_inspector->kBaseRoute, container_info_inspector.get()); network_status_inspector = std::make_unique(conn_tracker); server.addHandler(network_status_inspector->kBaseRoute, network_status_inspector.get()); + collector_config_inspector = std::make_unique(std::make_shared(config_)); + server.addHandler(collector_config_inspector->kBaseRoute, collector_config_inspector.get()); } system_inspector_.Init(config_, conn_tracker); diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index d1faaa2600..23a4e6dff9 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -602,3 +602,8 @@ $ curl "http://:8080/state/network/connection?container=c6f030bc4b42& ] } ``` + +### Config endpoint +The runtime configuration can be obtained using +$ curl "http://:8080/state/config" +