Fix race condition by pre-building the cluster json data for services.json

This commit is contained in:
Tim Wojtulewicz 2024-05-31 17:02:06 -07:00 committed by Tim Wojtulewicz
parent 00b24b043a
commit f55c0a5292
2 changed files with 11 additions and 8 deletions

View file

@ -44,6 +44,8 @@ void Manager::InitPostScript() {
static auto manager_type = node_type_type->Lookup("Cluster", "MANAGER"); static auto manager_type = node_type_type->Lookup("Cluster", "MANAGER");
if ( local_node_type == manager_type ) { if ( local_node_type == manager_type ) {
BuildClusterJson();
callbacks = new CivetCallbacks(); callbacks = new CivetCallbacks();
callbacks->begin_request = [](struct mg_connection* conn) -> int { callbacks->begin_request = [](struct mg_connection* conn) -> int {
// Handle the services.json request ourselves by building up a response based on // Handle the services.json request ourselves by building up a response based on
@ -414,7 +416,7 @@ ValPtr Manager::CollectHistogramMetrics(std::string_view prefix_pattern, std::st
return ret_val; return ret_val;
} }
std::string Manager::GetClusterJson() const { void Manager::BuildClusterJson() {
rapidjson::StringBuffer buffer; rapidjson::StringBuffer buffer;
json::detail::NullDoubleWriter writer(buffer); json::detail::NullDoubleWriter writer(buffer);
@ -423,8 +425,9 @@ std::string Manager::GetClusterJson() const {
writer.Key("targets"); writer.Key("targets");
writer.StartArray(); writer.StartArray();
auto cluster_nodes = id::find_val("Cluster::nodes")->AsTableVal()->ToMap(); auto& node_val = id::find_val("Cluster::nodes");
for ( const auto& [idx, value] : cluster_nodes ) { auto node_map = node_val->AsTableVal()->ToMap();
for ( const auto& [idx, value] : node_map ) {
auto node = value->AsRecordVal(); auto node = value->AsRecordVal();
auto ip = node->GetField<AddrVal>("ip"); auto ip = node->GetField<AddrVal>("ip");
auto port = node->GetField<PortVal>("metrics_port"); auto port = node->GetField<PortVal>("metrics_port");
@ -440,7 +443,7 @@ std::string Manager::GetClusterJson() const {
writer.EndObject(); writer.EndObject();
writer.EndArray(); writer.EndArray();
return buffer.GetString(); cluster_json = buffer.GetString();
} }
CounterFamilyPtr Manager::CounterFamily(std::string_view prefix, std::string_view name, CounterFamilyPtr Manager::CounterFamily(std::string_view prefix, std::string_view name,

View file

@ -200,7 +200,7 @@ public:
* @return A JSON description of the cluster configuration for reporting * @return A JSON description of the cluster configuration for reporting
* to Prometheus for service discovery requests. * to Prometheus for service discovery requests.
*/ */
std::string GetClusterJson() const; std::string GetClusterJson() const { return cluster_json; }
/** /**
* @return The pointer to the prometheus-cpp registry used by the telemetry * @return The pointer to the prometheus-cpp registry used by the telemetry
@ -230,6 +230,7 @@ protected:
private: private:
RecordValPtr GetMetricOptsRecord(const prometheus::MetricFamily& metric_family); RecordValPtr GetMetricOptsRecord(const prometheus::MetricFamily& metric_family);
void BuildClusterJson();
std::map<std::string, std::shared_ptr<MetricFamily>> families; std::map<std::string, std::shared_ptr<MetricFamily>> families;
std::map<std::string, RecordValPtr> opts_records; std::map<std::string, RecordValPtr> opts_records;
@ -242,11 +243,10 @@ private:
GaugePtr cpu_gauge; GaugePtr cpu_gauge;
GaugePtr fds_gauge; GaugePtr fds_gauge;
std::string endpoint_name;
std::vector<std::string> export_prefixes;
std::shared_ptr<prometheus::Registry> prometheus_registry; std::shared_ptr<prometheus::Registry> prometheus_registry;
std::unique_ptr<prometheus::Exposer> prometheus_exposer; std::unique_ptr<prometheus::Exposer> prometheus_exposer;
std::string cluster_json;
}; };
} // namespace zeek::telemetry } // namespace zeek::telemetry