From: Pieter Lexis Date: Tue, 13 Jan 2026 10:40:02 +0000 (+0100) Subject: feat(dnsdist/carbon): use server_id when ourname is empty X-Git-Tag: rec-5.4.0-beta1~41^2~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6bd823cc41a865a3ec890cbc01b970cb7c2204ad;p=thirdparty%2Fpdns.git feat(dnsdist/carbon): use server_id when ourname is empty --- diff --git a/pdns/dnsdistdist/dnsdist-carbon.cc b/pdns/dnsdistdist/dnsdist-carbon.cc index 82ae493504..43020c585a 100644 --- a/pdns/dnsdistdist/dnsdist-carbon.cc +++ b/pdns/dnsdistdist/dnsdist-carbon.cc @@ -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::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& 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::max() ? static_cast(interval) : 30}; } @@ -368,6 +360,16 @@ void Carbon::run(const std::vector& 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 */ diff --git a/pdns/dnsdistdist/dnsdist-carbon.hh b/pdns/dnsdistdist/dnsdist-carbon.hh index 96c9f96dac..1be811c3f6 100644 --- a/pdns/dnsdistdist/dnsdist-carbon.hh +++ b/pdns/dnsdistdist/dnsdist-carbon.hh @@ -25,6 +25,7 @@ #ifndef DISABLE_CARBON #include +#include #include "iputils.hh" namespace dnsdist @@ -36,12 +37,14 @@ public: { ComboAddress server; std::string namespace_name; - std::string ourname; + std::optional 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& ourName, uint64_t interval, const std::string& namespace_name, const std::string& instance_name); static void run(const std::vector& endpoints); }; diff --git a/pdns/dnsdistdist/dnsdist-configuration-yaml.cc b/pdns/dnsdistdist/dnsdist-configuration-yaml.cc index 32c14ee551..3b9836c594 100644 --- a/pdns/dnsdistdist/dnsdist-configuration-yaml.cc +++ b/pdns/dnsdistdist/dnsdist-configuration-yaml.cc @@ -20,6 +20,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include +#include #include #include @@ -1092,7 +1093,7 @@ static void handleCarbonConfiguration([[maybe_unused]] const ::rust::Vec(carbonConfig.name), carbonConfig.interval, carbonConfig.name_space.empty() ? "dnsdist" : std::string(carbonConfig.name_space), carbonConfig.instance.empty() ? "main" : std::string(carbonConfig.instance)); diff --git a/pdns/dnsdistdist/dnsdist-lua.cc b/pdns/dnsdistdist/dnsdist-lua.cc index 4f70538f1b..5fc7a147a3 100644 --- a/pdns/dnsdistdist/dnsdist-lua.cc +++ b/pdns/dnsdistdist/dnsdist-lua.cc @@ -1041,7 +1041,7 @@ static void setupLuaConfig(LuaContext& luaCtx, bool client, bool configCheck) luaCtx.writeFunction("carbonServer", [](const std::string& address, std::optional ourName, std::optional interval, std::optional namespace_name, std::optional 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")); diff --git a/pdns/dnsdistdist/dnsdist-settings-definitions.yml b/pdns/dnsdistdist/dnsdist-settings-definitions.yml index de9b48b372..bfcb87ca68 100644 --- a/pdns/dnsdistdist/dnsdist-settings-definitions.yml +++ b/pdns/dnsdistdist/dnsdist-settings-definitions.yml @@ -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