Skip to content

Commit

Permalink
ROX-26516: Introspection endpoint for runtime config (#1879)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoukoVirtanen authored Oct 10, 2024
1 parent d108359 commit d6e4d5b
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
55 changes: 55 additions & 0 deletions collector/lib/CollectorConfigInspector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "CollectorConfigInspector.h"

#include <Logging.h>
#include <string>

#include <google/protobuf/util/json_util.h>

namespace collector {

const std::string CollectorConfigInspector::kBaseRoute = "/state/config";

CollectorConfigInspector::CollectorConfigInspector(const std::shared_ptr<CollectorConfig> 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
29 changes: 29 additions & 0 deletions collector/lib/CollectorConfigInspector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef COLLECTOR_CONFIG_INSPECTOR_H
#define COLLECTOR_CONFIG_INSPECTOR_H

#include <memory>

#include <json/writer.h>

#include "CollectorConfig.h"
#include "IntrospectionEndpoint.h"

namespace collector {

class CollectorConfigInspector : public IntrospectionEndpoint {
public:
static const std::string kBaseRoute;

CollectorConfigInspector(const std::shared_ptr<CollectorConfig> config);

// implementation of CivetHandler
bool handleGet(CivetServer* server, struct mg_connection* conn) override;

private:
const std::shared_ptr<CollectorConfig> config_;
std::string configToJson(bool& isError);
};

} // namespace collector

#endif
4 changes: 4 additions & 0 deletions collector/lib/CollectorService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extern "C" {
#include <memory>

#include "CivetServer.h"
#include "CollectorConfigInspector.h"
#include "CollectorStatsExporter.h"
#include "ConnTracker.h"
#include "Containers.h"
Expand Down Expand Up @@ -63,6 +64,7 @@ void CollectorService::RunForever() {

std::unique_ptr<ContainerInfoInspector> container_info_inspector;
std::unique_ptr<NetworkStatusInspector> network_status_inspector;
std::unique_ptr<CollectorConfigInspector> collector_config_inspector;

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

Expand Down Expand Up @@ -111,6 +113,8 @@ void CollectorService::RunForever() {
server.addHandler(container_info_inspector->kBaseRoute, container_info_inspector.get());
network_status_inspector = std::make_unique<NetworkStatusInspector>(conn_tracker);
server.addHandler(network_status_inspector->kBaseRoute, network_status_inspector.get());
collector_config_inspector = std::make_unique<CollectorConfigInspector>(std::make_shared<CollectorConfig>(config_));
server.addHandler(collector_config_inspector->kBaseRoute, collector_config_inspector.get());
}

system_inspector_.Init(config_, conn_tracker);
Expand Down
5 changes: 5 additions & 0 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,8 @@ $ curl "http://<collector>:8080/state/network/connection?container=c6f030bc4b42&
]
}
```

### Config endpoint
The runtime configuration can be obtained using
$ curl "http://<collector>:8080/state/config"

0 comments on commit d6e4d5b

Please sign in to comment.