]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: Better handling of the different types of metrics 11845/head
authorRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 10 Aug 2022 14:56:55 +0000 (16:56 +0200)
committerRemi Gacogne <remi.gacogne@powerdns.com>
Wed, 10 Aug 2022 14:56:55 +0000 (16:56 +0200)
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
pdns/dnsdist-lua-inspection.cc
pdns/dnsdist-web.cc

index 1776f60241b557c5e15688ba428f6eb0280f291a..ef7a8560823864364425a4c1d58f095c1a2d8231 100644 (file)
@@ -73,14 +73,18 @@ void carbonDumpThread()
           time_t now=time(0);
           for(const auto& e : g_stats.entries) {
             str<<namespace_name<<"."<<hostname<<"."<<instance_name<<"."<<e.first<<' ';
-            if(const auto& val = boost::get<pdns::stat_t*>(&e.second))
+            if (const auto& val = boost::get<pdns::stat_t*>(&e.second)) {
               str<<(*val)->load();
-            else if(const auto& adval = boost::get<pdns::stat_t_trait<double>*>(&e.second))
+            }
+            else if(const auto& adval = boost::get<pdns::stat_t_trait<double>*>(&e.second)) {
               str<<(*adval)->load();
-            else if (const auto& dval = boost::get<double*>(&e.second))
+            }
+            else if (const auto& dval = boost::get<double*>(&e.second)) {
               str<<**dval;
-            else
-              str<<(*boost::get<DNSDistStats::statfunction_t>(&e.second))(e.first);
+            }
+            else if (const auto& func = boost::get<DNSDistStats::statfunction_t>(&e.second)) {
+              str<<(*func)(e.first);
+            }
             str<<' '<<now<<"\r\n";
           }
           auto states = g_dstates.getLocal();
index c6314809823eff3409d0c9eeb976dd1cd2562ecd..fa652e23e974cbb87f7e4ea739259a965bb13d9e 100644 (file)
@@ -701,14 +701,14 @@ void setupLuaInspection(LuaContext& luaCtx)
         if (const auto& val = boost::get<pdns::stat_t*>(&e.second)) {
           second = std::to_string((*val)->load());
         }
-        else if(const auto& adval = boost::get<pdns::stat_t_trait<double>*>(&e.second)) {
+        else if (const auto& adval = boost::get<pdns::stat_t_trait<double>*>(&e.second)) {
           second = (flt % (*adval)->load()).str();
         }
         else if (const auto& dval = boost::get<double*>(&e.second)) {
           second = (flt % (**dval)).str();
         }
-        else {
-          second = std::to_string((*boost::get<DNSDistStats::statfunction_t>(&e.second))(e.first));
+        else if (const auto& func = boost::get<DNSDistStats::statfunction_t>(&e.second)) {
+          second = std::to_string((*func)(e.first));
         }
 
         if (leftcolumn.size() < g_stats.entries.size()/2) {
index 887d81bb19f8e710ea446612f19ce154668b7292..1e50a3f7b0dd14c61dafbc758d599a5c6dbc0cbf 100644 (file)
@@ -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<pdns::stat_t*>(&std::get<1>(e)))
+    if (const auto& val = boost::get<pdns::stat_t*>(&std::get<1>(e))) {
       output << (*val)->load();
-    else if (const auto& adval = boost::get<pdns::stat_t_trait<double>*>(&std::get<1>(e)))
+    }
+    else if (const auto& adval = boost::get<pdns::stat_t_trait<double>*>(&std::get<1>(e))) {
       output << (*adval)->load();
-    else if (const auto& dval = boost::get<double*>(&std::get<1>(e)))
+    }
+    else if (const auto& dval = boost::get<double*>(&std::get<1>(e))) {
       output << **dval;
-    else
-      output << (*boost::get<DNSDistStats::statfunction_t>(&std::get<1>(e)))(std::get<0>(e));
+    }
+    else if (const auto& func = boost::get<DNSDistStats::statfunction_t>(&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<double*>(&e.second)) {
       obj.insert({e.first, (**dval)});
-    } else {
-      obj.insert({e.first, (double)(*boost::get<DNSDistStats::statfunction_t>(&e.second))(e.first)});
+    } else if (const auto& func = boost::get<DNSDistStats::statfunction_t>(&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<DNSDistStats::statfunction_t>(&item.second)) {
       doc.push_back(Json::object {
           { "type", "StatisticItem" },
           { "name", item.first },
-          { "value", (double)(*boost::get<DNSDistStats::statfunction_t>(&item.second))(item.first) }
+          { "value", (double)(*func)(item.first) }
         });
     }
   }