namespace dnsdist
{
-LockGuarded<Carbon::Config> Carbon::s_config;
-
static bool doOneCarbonExport(const Carbon::Endpoint& endpoint)
{
const auto& server = endpoint.server;
return true;
}
-static void carbonHandler(Carbon::Endpoint&& endpoint)
+static void carbonHandler(Carbon::Endpoint endpoint)
{
setThreadName("dnsdist/carbon");
const auto intervalUSec = endpoint.interval * 1000 * 1000;
}
}
-bool Carbon::addEndpoint(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)
{
- if (endpoint.ourname.empty()) {
+ if (ourName.empty()) {
try {
- endpoint.ourname = getCarbonHostName();
+ ourName = getCarbonHostName();
}
- catch (const std::exception& e) {
- 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: ") + e.what());
+ 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());
}
}
-
- auto config = s_config.lock();
- if (config->d_running) {
- // we already started the threads, let's just spawn a new one
- std::thread newHandler(carbonHandler, std::move(endpoint));
- newHandler.detach();
- }
- else {
- config->d_endpoints.push_back(std::move(endpoint));
- }
- return true;
+ return Carbon::Endpoint{ComboAddress(address, 2003),
+ !namespace_name.empty() ? namespace_name : "dnsdist",
+ ourName,
+ !instance_name.empty() ? instance_name : "main",
+ interval < std::numeric_limits<unsigned int>::max() ? static_cast<unsigned int>(interval) : 30};
}
-void Carbon::run()
+void Carbon::run(const std::vector<Carbon::Endpoint>& endpoints)
{
- auto config = s_config.lock();
- if (config->d_running) {
- throw std::runtime_error("The carbon threads are already running");
- }
- for (auto& endpoint : config->d_endpoints) {
- std::thread newHandler(carbonHandler, std::move(endpoint));
+ for (auto& endpoint : endpoints) {
+ std::thread newHandler(carbonHandler, endpoint);
newHandler.detach();
}
- config->d_endpoints.clear();
- config->d_running = true;
}
}
#include "config.h"
#ifndef DISABLE_CARBON
-
-#include <thread>
+#include <string>
#include "iputils.hh"
-#include "lock.hh"
namespace dnsdist
{
unsigned int interval;
};
- static bool addEndpoint(Endpoint&& endpoint);
- static void run();
-
-private:
- struct Config
- {
- std::vector<Endpoint> d_endpoints;
- bool d_running{false};
- };
-
- static LockGuarded<Config> s_config;
+ static Endpoint newEndpoint(const std::string& address, std::string ourName, uint64_t interval, const std::string& namespace_name, const std::string& instance_name);
+ static void run(const std::vector<Endpoint>& endpoints);
+ static void addEndpointAtRuntime(const Endpoint& endpoint);
};
}
#include <string>
#include "credentials.hh"
+#include "dnsdist-carbon.hh"
#include "dnsdist-query-count.hh"
#include "dnsdist-rule-chains.hh"
#include "iputils.hh"
{
rules::RuleChains d_ruleChains;
servers_t d_backends;
+ std::vector<dnsdist::Carbon::Endpoint> d_carbonEndpoints;
std::map<std::string, std::shared_ptr<ServerPool>> d_pools;
std::shared_ptr<const CredentialsHolder> d_webPassword;
std::shared_ptr<const CredentialsHolder> d_webAPIKey;
#ifndef DISABLE_CARBON
luaCtx.writeFunction("carbonServer", [](const std::string& address, boost::optional<string> ourName, boost::optional<uint64_t> interval, boost::optional<string> namespace_name, boost::optional<string> instance_name) {
setLuaSideEffect();
- dnsdist::Carbon::Endpoint endpoint{ComboAddress(address, 2003),
- (namespace_name && !namespace_name->empty()) ? *namespace_name : "dnsdist",
- ourName ? *ourName : "",
- (instance_name && !instance_name->empty()) ? *instance_name : "main",
- (interval && *interval < std::numeric_limits<unsigned int>::max()) ? static_cast<unsigned int>(*interval) : 30};
- dnsdist::Carbon::addEndpoint(std::move(endpoint));
+ auto newEndpoint = dnsdist::Carbon::newEndpoint(address,
+ (ourName ? *ourName : ""),
+ (interval ? *interval : 30),
+ (namespace_name ? *namespace_name : "dnsdist"),
+ (instance_name ? *instance_name : "main"));
+ if (dnsdist::configuration::isConfigurationDone()) {
+ dnsdist::Carbon::run({newEndpoint});
+ }
+ dnsdist::configuration::updateRuntimeConfiguration([&newEndpoint](dnsdist::configuration::RuntimeConfiguration& config) {
+ config.d_carbonEndpoints.push_back(std::move(newEndpoint));
+ });
});
#endif /* DISABLE_CARBON */
dnsdist::ServiceDiscovery::run();
#ifndef DISABLE_CARBON
- dnsdist::Carbon::run();
+ dnsdist::Carbon::run(dnsdist::configuration::getCurrentRuntimeConfiguration().d_carbonEndpoints);
#endif /* DISABLE_CARBON */
thread stattid(maintThread);