]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
Only prepend the pdns_recursor_ prefix if no 2nd arg to getMteric was 9687/head
authorOtto Moerbeek <otto.moerbeek@open-xchange.com>
Fri, 30 Oct 2020 08:12:06 +0000 (09:12 +0100)
committerOtto Moerbeek <otto@drijf.net>
Mon, 9 Nov 2020 11:51:50 +0000 (12:51 +0100)
given.

This also fixes the lookup of the HELP entries since they require the
original key. So getAllStats() now returns the key, the prometheus name
and the value.

(cherry picked from commit 308cd14cac73ba48aca3d3953e12d1cc04a96bbb)

pdns/rec-carbon.cc
pdns/rec_channel.hh
pdns/rec_channel_rec.cc
pdns/ws-recursor.cc

index c03bae308214a1f64a6734782ccc7c2d1a6eaa6e..945a424de2ac3d4010a2f0407520ef23c9a4d0d7 100644 (file)
@@ -54,14 +54,13 @@ try
     s.connect(remote);  // we do the connect so the first attempt happens while we gather stats
  
     if(msg.empty()) {
-      typedef map<string,string> all_t;
-      all_t all=getAllStatsMap(StatComponent::Carbon);
+      auto all = getAllStatsMap(StatComponent::Carbon);
       
       ostringstream str;
       time_t now=time(0);
       
-      for(const all_t::value_type& val :  all) {
-        str<<namespace_name<<'.'<<hostname<<'.'<<instance_name<<'.'<<val.first<<' '<<val.second<<' '<<now<<"\r\n";
+      for(const auto& val : all) {
+        str<<namespace_name<<'.'<<hostname<<'.'<<instance_name<<'.'<<val.first<<' '<<val.second.d_value<<' '<<now<<"\r\n";
       }
       msg = str.str();
     }
index 3862bfeed535032a3106aa3204e4de9249c4692c..14b36cc0d722f50f8ed9425afda24e8fbf995fdb 100644 (file)
@@ -72,7 +72,13 @@ public:
 
 enum class StatComponent { API, Carbon, RecControl, SNMP };
 
-std::map<std::string, std::string> getAllStatsMap(StatComponent component, bool prometheusName = false);
+struct StatsMapEntry {
+  std::string d_prometheusName;
+  std::string d_value;
+};
+typedef std::map<std::string, StatsMapEntry> StatsMap;
+StatsMap getAllStatsMap(StatComponent component);
+
 extern std::mutex g_carbon_config_lock;
 std::vector<std::pair<DNSName, uint16_t> >* pleaseGetQueryRing();
 std::vector<std::pair<DNSName, uint16_t> >* pleaseGetServfailQueryRing();
index 02c4c55b46405cc6537d29cf9fd9da2ab8979be8..a5786603e20f3d09ae4f6af37dbb22f8be2cd7ab 100644 (file)
@@ -87,6 +87,14 @@ static void addGetStat(const string& name, function<uint64_t ()> f )
   d_get64bitmembers[name]=f;
 }
 
+static std::string getPrometheusName(const std::string& arg)
+{
+  std::string name = arg;
+  std::replace_if(name.begin(), name.end(), [](char c){
+    return !isalnum(static_cast<unsigned char>(c));}, '_');
+  return "pdns_recursor_" + name;
+}
+
 std::atomic<unsigned long>* getDynMetric(const std::string& str, const std::string& prometheusName)
 {
   std::lock_guard<std::mutex> l(d_dynmetricslock);
@@ -98,8 +106,7 @@ std::atomic<unsigned long>* getDynMetric(const std::string& str, const std::stri
   if (!prometheusName.empty()) {
     name = prometheusName;
   } else {
-    std::replace_if(name.begin(), name.end(), [](char c){
-      return !isalnum(static_cast<unsigned char>(c));}, '_');
+    name = getPrometheusName(name);
   }
 
   auto ret = dynmetrics{new std::atomic<unsigned long>(), name};
@@ -131,25 +138,25 @@ optional<uint64_t> getStatByName(const std::string& name)
   return get(name);
 }
 
-map<string,string> getAllStatsMap(StatComponent component, bool prometheusNames)
+StatsMap getAllStatsMap(StatComponent component)
 {
-  map<string,string> ret;
+  StatsMap ret;
   const auto& blacklistMap = s_blacklistedStats.at(component);
 
   for(const auto& the32bits :  d_get32bitpointers) {
     if (blacklistMap.count(the32bits.first) == 0) {
-      ret.insert(make_pair(the32bits.first, std::to_string(*the32bits.second)));
+      ret.insert(make_pair(the32bits.first, StatsMapEntry{getPrometheusName(the32bits.first), std::to_string(*the32bits.second)}));
     }
   }
   for(const auto& atomic :  d_getatomics) {
     if (blacklistMap.count(atomic.first) == 0) {
-      ret.insert(make_pair(atomic.first, std::to_string(atomic.second->load())));
+      ret.insert(make_pair(atomic.first, StatsMapEntry{getPrometheusName(atomic.first), std::to_string(atomic.second->load())}));
     }
   }
 
   for(const auto& the64bitmembers :  d_get64bitmembers) {
     if (blacklistMap.count(the64bitmembers.first) == 0) {
-      ret.insert(make_pair(the64bitmembers.first, std::to_string(the64bitmembers.second())));
+      ret.insert(make_pair(the64bitmembers.first, StatsMapEntry{getPrometheusName(the64bitmembers.first), std::to_string(the64bitmembers.second())}));
     }
   }
 
@@ -157,8 +164,7 @@ map<string,string> getAllStatsMap(StatComponent component, bool prometheusNames)
     std::lock_guard<std::mutex> l(d_dynmetricslock);
     for(const auto& a : d_dynmetrics) {
       if (blacklistMap.count(a.first) == 0) {
-        ret.insert({prometheusNames ? a.second.d_prometheusName : a.first,
-            std::to_string(*a.second.d_ptr)});
+        ret.insert(make_pair(a.first, StatsMapEntry{a.second.d_prometheusName, std::to_string(*a.second.d_ptr)}));
       }
     }
   }
@@ -168,11 +174,10 @@ map<string,string> getAllStatsMap(StatComponent component, bool prometheusNames)
 
 static string getAllStats()
 {
-  typedef map<string, string> varmap_t;
-  varmap_t varmap = getAllStatsMap(StatComponent::RecControl);
+  auto varmap = getAllStatsMap(StatComponent::RecControl);
   string ret;
-  for(varmap_t::value_type& tup :  varmap) {
-    ret += tup.first + "\t" + tup.second +"\n";
+  for (const auto& tup : varmap) {
+    ret += tup.first + "\t" + tup.second.d_value + "\n";
   }
   return ret;
 }
index 1fa55063e7d3e143ec2b95c47fef30719631b7a7..223ed4a3699f3b0b620351dfe9a5f166984f848d 100644 (file)
@@ -50,8 +50,12 @@ using json11::Json;
 
 void productServerStatisticsFetch(map<string,string>& out)
 {
-  map<string,string> stats = getAllStatsMap(StatComponent::API);
-  out.swap(stats);
+  auto stats = getAllStatsMap(StatComponent::API);
+  map<string,string> ret;
+  for (const auto& entry: stats) {
+    ret.insert(make_pair(entry.first, entry.second.d_value));
+  }
+  out.swap(ret);
 }
 
 boost::optional<uint64_t> productServerStatisticsFetch(const std::string& name)
@@ -434,17 +438,14 @@ static void prometheusMetrics(HttpRequest *req, HttpResponse *resp) {
 
     registerAllStats();
 
-
     std::ostringstream output;
-    typedef map <string, string> varmap_t;
-    
+
     // Argument controls blacklisting of any stats. So
     // stats-api-blacklist will be used to block returned stats.
-    // Second arg tells to use the prometheus names.
-    varmap_t varmap = getAllStatsMap(StatComponent::API, true);
-    for (const auto &tup :  varmap) {
+    auto varmap = getAllStatsMap(StatComponent::API);
+    for (const auto& tup : varmap) {
         std::string metricName = tup.first;
-        std::string prometheusMetricName = "pdns_recursor_" + metricName;
+        std::string prometheusMetricName = tup.second.d_prometheusName;
 
         MetricDefinition metricDetails;
 
@@ -460,7 +461,7 @@ static void prometheusMetrics(HttpRequest *req, HttpResponse *resp) {
           output << "# HELP " << prometheusMetricName << " " << metricDetails.description << "\n";
           output << "# TYPE " << prometheusMetricName << " " << prometheusTypeName << "\n";
         }
-        output << prometheusMetricName << " " << tup.second << "\n";
+        output << prometheusMetricName << " " << tup.second.d_value << "\n";
     }
 
     output << "# HELP pdns_recursor_info " << "Info from pdns_recursor, value is always 1" << "\n";
@@ -470,7 +471,6 @@ static void prometheusMetrics(HttpRequest *req, HttpResponse *resp) {
     resp->body = output.str();
     resp->headers["Content-Type"] = "text/plain";
     resp->status = 200;
-
 }