]> git.ipfire.org Git - thirdparty/pdns.git/commitdiff
feat(dnsdist/carbon): use server_id when ourname is empty
authorPieter Lexis <pieter.lexis@powerdns.com>
Tue, 13 Jan 2026 10:40:02 +0000 (11:40 +0100)
committerPieter Lexis <pieter.lexis@powerdns.com>
Thu, 15 Jan 2026 16:13:20 +0000 (17:13 +0100)
pdns/dnsdistdist/dnsdist-carbon.cc
pdns/dnsdistdist/dnsdist-carbon.hh
pdns/dnsdistdist/dnsdist-configuration-yaml.cc
pdns/dnsdistdist/dnsdist-lua.cc
pdns/dnsdistdist/dnsdist-settings-definitions.yml

index 82ae493504c4e49c308a0adce702054a9571b1bf..43020c585a09545ae7e585c77631b1acd87beadc 100644 (file)
@@ -43,7 +43,7 @@ static bool doOneCarbonExport(const Carbon::Endpoint& endpoint)
 {
   const auto& server = endpoint.server;
   const std::string& namespace_name = endpoint.namespace_name;
-  const std::string& hostname = endpoint.ourname;
+  const std::string hostname = endpoint.getOurName();
   const std::string& instance_name = endpoint.instance_name;
 
   try {
@@ -330,7 +330,7 @@ static void carbonHandler(const Carbon::Endpoint& endpoint)
         if (consecutiveFailures < std::numeric_limits<decltype(consecutiveFailures)>::max()) {
           consecutiveFailures++;
         }
-        vinfolog("Run for %s - %s failed, next attempt in %d", endpoint.server.toStringWithPort(), endpoint.ourname, backOff);
+        vinfolog("Run for %s - %s failed, next attempt in %d", endpoint.server.toStringWithPort(), endpoint.getOurName(), backOff);
         std::this_thread::sleep_for(std::chrono::seconds(backOff));
       }
     } while (true);
@@ -343,19 +343,11 @@ static void carbonHandler(const Carbon::Endpoint& endpoint)
   }
 }
 
-Carbon::Endpoint Carbon::newEndpoint(const std::string& address, std::string ourName, uint64_t interval, const std::string& namespace_name, const std::string& instance_name)
+Carbon::Endpoint Carbon::newEndpoint(const std::string& address, const std::optional<std::string>& ourName, uint64_t interval, const std::string& namespace_name, const std::string& instance_name)
 {
-  if (ourName.empty()) {
-    try {
-      ourName = getCarbonHostName();
-    }
-    catch (const std::exception& exp) {
-      throw std::runtime_error(std::string("The 'ourname' setting in 'carbonServer()' has not been set and we are unable to determine the system's hostname: ") + exp.what());
-    }
-  }
   return Carbon::Endpoint{ComboAddress(address, 2003),
                           !namespace_name.empty() ? namespace_name : "dnsdist",
-                          std::move(ourName),
+                          ourName,
                           !instance_name.empty() ? instance_name : "main",
                           interval < std::numeric_limits<unsigned int>::max() ? static_cast<unsigned int>(interval) : 30};
 }
@@ -368,6 +360,16 @@ void Carbon::run(const std::vector<Carbon::Endpoint>& endpoints)
   }
 }
 
+const std::string Carbon::Endpoint::getOurName() const
+{
+  std::string ret = ourname.value_or("");
+  if (!ourname) {
+    ret = dnsdist::configuration::getCurrentRuntimeConfiguration().d_server_id;
+  }
+  std::replace(ret.begin(), ret.end(), '.', '_');
+  return ret;
+}
+
 }
 #endif /* DISABLE_CARBON */
 
index 96c9f96daceeddf0c6d4f1353a28e4ecd5daa97b..1be811c3f60c4e912285cd0a204256e74e86e011 100644 (file)
@@ -25,6 +25,7 @@
 
 #ifndef DISABLE_CARBON
 #include <string>
+#include <optional>
 #include "iputils.hh"
 
 namespace dnsdist
@@ -36,12 +37,14 @@ public:
   {
     ComboAddress server;
     std::string namespace_name;
-    std::string ourname;
+    std::optional<std::string> ourname; // When unset, we use the Runtime Config server_id
     std::string instance_name;
     unsigned int interval;
+
+    const std::string getOurName() const;
   };
 
-  static Endpoint newEndpoint(const std::string& address, std::string ourName, uint64_t interval, const std::string& namespace_name, const std::string& instance_name);
+  static Endpoint newEndpoint(const std::string& address, const std::optional<std::string>& ourName, uint64_t interval, const std::string& namespace_name, const std::string& instance_name);
   static void run(const std::vector<Endpoint>& endpoints);
 };
 
index 32c14ee551b31a95529b65e077245b434667e7cd..3b9836c59491e9df7bb53286c4246bbd7d329958 100644 (file)
@@ -20,6 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 #include <memory>
+#include <optional>
 #include <stdexcept>
 #include <vector>
 
@@ -1092,7 +1093,7 @@ static void handleCarbonConfiguration([[maybe_unused]] const ::rust::Vec<dnsdist
     dnsdist::configuration::updateRuntimeConfiguration([&carbonConfigs](dnsdist::configuration::RuntimeConfiguration& config) {
       for (const auto& carbonConfig : carbonConfigs) {
         auto newEndpoint = dnsdist::Carbon::newEndpoint(std::string(carbonConfig.address),
-                                                        std::string(carbonConfig.name),
+                                                        carbonConfig.name.empty() ? std::nullopt : std::optional<std::string>(carbonConfig.name),
                                                         carbonConfig.interval,
                                                         carbonConfig.name_space.empty() ? "dnsdist" : std::string(carbonConfig.name_space),
                                                         carbonConfig.instance.empty() ? "main" : std::string(carbonConfig.instance));
index 4f70538f1bc389da8a85ed0f8b3aaed0947335d4..5fc7a147a363abc574c6a0738a07dcab4fab5875 100644 (file)
@@ -1041,7 +1041,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck)
   luaCtx.writeFunction("carbonServer", [](const std::string& address, std::optional<string> ourName, std::optional<uint64_t> interval, std::optional<string> namespace_name, std::optional<string> instance_name) {
     setLuaSideEffect();
     auto newEndpoint = dnsdist::Carbon::newEndpoint(address,
-                                                    (ourName ? *ourName : ""),
+                                                    ourName,
                                                     (interval ? *interval : 30),
                                                     (namespace_name ? *namespace_name : "dnsdist"),
                                                     (instance_name ? *instance_name : "main"));
index de9b48b372816d5db2cc84dd43fba16faa093e39..bfcb87ca68c583f08edae51467b0adc9da319bbf 100644 (file)
@@ -173,7 +173,7 @@ carbon:
     - name: "name"
       type: "String"
       default: ""
-      description: "An optional string specifying the hostname that should be used. If left empty, the system hostname is used"
+      description: "An optional string specifying the hostname that should be used. If left empty, the general.server_id is used"
     - name: "interval"
       type: u32
       default: 30