From 46fb5218a43deb4d7b2760f8b4466d9db2477b93 Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Wed, 10 Aug 2022 16:56:55 +0200 Subject: [PATCH] dnsdist: Better handling of the different types of metrics This commit ensures that we don't crash if we forget to update a part of code if we ever add a new type of metrics, as happened in 9f4fa5ae01efa878d2aa27e4398740d7ed6ef01f. --- pdns/dnsdist-carbon.cc | 14 +++++++++----- pdns/dnsdist-lua-inspection.cc | 6 +++--- pdns/dnsdist-web.cc | 22 +++++++++++++--------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/pdns/dnsdist-carbon.cc b/pdns/dnsdist-carbon.cc index 1776f60241..ef7a856082 100644 --- a/pdns/dnsdist-carbon.cc +++ b/pdns/dnsdist-carbon.cc @@ -73,14 +73,18 @@ void carbonDumpThread() time_t now=time(0); for(const auto& e : g_stats.entries) { str<(&e.second)) + if (const auto& val = boost::get(&e.second)) { str<<(*val)->load(); - else if(const auto& adval = boost::get*>(&e.second)) + } + else if(const auto& adval = boost::get*>(&e.second)) { str<<(*adval)->load(); - else if (const auto& dval = boost::get(&e.second)) + } + else if (const auto& dval = boost::get(&e.second)) { str<<**dval; - else - str<<(*boost::get(&e.second))(e.first); + } + else if (const auto& func = boost::get(&e.second)) { + str<<(*func)(e.first); + } str<<' '<(&e.second)) { second = std::to_string((*val)->load()); } - else if(const auto& adval = boost::get*>(&e.second)) { + else if (const auto& adval = boost::get*>(&e.second)) { second = (flt % (*adval)->load()).str(); } else if (const auto& dval = boost::get(&e.second)) { second = (flt % (**dval)).str(); } - else { - second = std::to_string((*boost::get(&e.second))(e.first)); + else if (const auto& func = boost::get(&e.second)) { + second = std::to_string((*func)(e.first)); } if (leftcolumn.size() < g_stats.entries.size()/2) { diff --git a/pdns/dnsdist-web.cc b/pdns/dnsdist-web.cc index 887d81bb19..1e50a3f7b0 100644 --- a/pdns/dnsdist-web.cc +++ b/pdns/dnsdist-web.cc @@ -483,14 +483,18 @@ static void handlePrometheus(const YaHTTP::Request& req, YaHTTP::Response& resp) output << "# TYPE " << prometheusMetricName << " " << prometheusTypeName << "\n"; output << prometheusMetricName << " "; - if (const auto& val = boost::get(&std::get<1>(e))) + if (const auto& val = boost::get(&std::get<1>(e))) { output << (*val)->load(); - else if (const auto& adval = boost::get*>(&std::get<1>(e))) + } + else if (const auto& adval = boost::get*>(&std::get<1>(e))) { output << (*adval)->load(); - else if (const auto& dval = boost::get(&std::get<1>(e))) + } + else if (const auto& dval = boost::get(&std::get<1>(e))) { output << **dval; - else - output << (*boost::get(&std::get<1>(e)))(std::get<0>(e)); + } + else if (const auto& func = boost::get(&std::get<1>(e))) { + output << (*func)(std::get<0>(e)); + } output << "\n"; } @@ -874,8 +878,8 @@ static void addStatsToJSONObject(Json::object& obj) obj.insert({e.first, (*adval)->load()}); } else if (const auto& dval = boost::get(&e.second)) { obj.insert({e.first, (**dval)}); - } else { - obj.insert({e.first, (double)(*boost::get(&e.second))(e.first)}); + } else if (const auto& func = boost::get(&e.second)) { + obj.insert({e.first, (double)(*func)(e.first)}); } } } @@ -1307,11 +1311,11 @@ static void handleStatsOnly(const YaHTTP::Request& req, YaHTTP::Response& resp) { "value", (**dval) } }); } - else { + else if (const auto& func = boost::get(&item.second)) { doc.push_back(Json::object { { "type", "StatisticItem" }, { "name", item.first }, - { "value", (double)(*boost::get(&item.second))(item.first) } + { "value", (double)(*func)(item.first) } }); } } -- 2.47.2