]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
dnsdist: skip metrics with labels for carbon export and stats dump
authorEnsar Sarajčić <dev@ensarsarajcic.com>
Thu, 12 Dec 2024 18:36:41 +0000 (19:36 +0100)
committerEnsar Sarajčić <dev@ensarsarajcic.com>
Thu, 12 Dec 2024 18:36:41 +0000 (19:36 +0100)
pdns/dnsdistdist/dnsdist-carbon.cc
pdns/dnsdistdist/dnsdist-lua-inspection.cc
pdns/dnsdistdist/dnsdist-lua.cc

index 040783b68a956fd23a1d8ae26870a17d25e29277..66ec734250e4c2d416a7a977c68e3bda4955cb7c 100644 (file)
@@ -57,6 +57,11 @@ static bool doOneCarbonExport(const Carbon::Endpoint& endpoint)
     {
       auto entries = dnsdist::metrics::g_stats.entries.read_lock();
       for (const auto& entry : *entries) {
+        // Skip non-empty labels, since labels are not supported in Carbon
+        if (!entry.d_labels.empty()) {
+          continue;
+        }
+
         str << namespace_name << "." << hostname << "." << instance_name << "." << entry.d_name << ' ';
         if (const auto& val = std::get_if<pdns::stat_t*>(&entry.d_value)) {
           str << (*val)->load();
index 2ad3999f7c2e549bd7a6726fb65a8bcb26f104b0..26d37f712254f799d7b269149982131ac7459bd5 100644 (file)
@@ -19,7 +19,9 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
+#include <algorithm>
 #include <fcntl.h>
+#include <iterator>
 
 #include "dnsdist.hh"
 #include "dnsdist-console.hh"
@@ -811,12 +813,18 @@ void setupLuaInspection(LuaContext& luaCtx)
     boost::format fmt("%-35s\t%+11s");
     g_outputBuffer.clear();
     auto entries = *dnsdist::metrics::g_stats.entries.read_lock();
-    sort(entries.begin(), entries.end(),
+
+    // Filter entries to just the ones without label, for clearer output
+    std::vector<std::reference_wrapper<decltype(entries)::value_type>> unlabeledEntries;
+    std::copy_if(entries.begin(), entries.end(), std::back_inserter(unlabeledEntries), [](const decltype(entries)::value_type& triple) { return triple.d_labels.empty(); });
+
+    sort(unlabeledEntries.begin(), unlabeledEntries.end(),
          [](const decltype(entries)::value_type& lhs, const decltype(entries)::value_type& rhs) {
            return lhs.d_name < rhs.d_name;
          });
     boost::format flt("    %9.1f");
-    for (const auto& entry : entries) {
+    for (const auto& entryRef : unlabeledEntries) {
+      const auto& entry = entryRef.get();
       string second;
       if (const auto& val = std::get_if<pdns::stat_t*>(&entry.d_value)) {
         second = std::to_string((*val)->load());
@@ -828,7 +836,7 @@ void setupLuaInspection(LuaContext& luaCtx)
         second = std::to_string((*func)(entry.d_name));
       }
 
-      if (leftcolumn.size() < entries.size() / 2) {
+      if (leftcolumn.size() < unlabeledEntries.size() / 2) {
         leftcolumn.push_back((fmt % entry.d_name % second).str());
       }
       else {
index ff4e05587e5c1f8bf75e93efe08dd0e1d14426de..eec7e4dc97f41bf47fecf1990f4788cfdfefe784 100644 (file)
@@ -3413,7 +3413,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
     newThread.detach();
   });
 
-  luaCtx.writeFunction("declareMetric", [](const std::string& name, const std::string& type, const std::string& description, const boost::optional<boost::variant<std::string, declare_metric_opts_t>>& opts) {
+  luaCtx.writeFunction("declareMetric", [](const std::string& name, const std::string& type, const std::string& description, boost::optional<boost::variant<std::string, declare_metric_opts_t>> opts) {
     bool withLabels = false;
     std::optional<std::string> customName = std::nullopt;
     if (opts) {
@@ -3436,7 +3436,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
     }
     return true;
   });
-  luaCtx.writeFunction("incMetric", [](const std::string& name, const boost::optional<boost::variant<uint64_t, update_metric_opts_t>>& opts) {
+  luaCtx.writeFunction("incMetric", [](const std::string& name, boost::optional<boost::variant<uint64_t, update_metric_opts_t>> opts) {
     auto incOpts = opts.get_value_or(1);
     uint64_t step = 1;
     std::unordered_map<std::string, std::string> labels;
@@ -3457,7 +3457,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
     }
     return std::get<uint64_t>(result);
   });
-  luaCtx.writeFunction("decMetric", [](const std::string& name, const boost::optional<boost::variant<uint64_t, update_metric_opts_t>>& opts) {
+  luaCtx.writeFunction("decMetric", [](const std::string& name, boost::optional<boost::variant<uint64_t, update_metric_opts_t>> opts) {
     auto decOpts = opts.get_value_or(1);
     uint64_t step = 1;
     std::unordered_map<std::string, std::string> labels;