stats: fetch all arguments needed when constructing g_stats_client

There's little sense in passing a ref to `ArgsManager` just to set a few
values because we'll be `const`-ing them in an upcoming commit.
Arguments supplied are expected to last the lifetime of the program's
instance and there's little reason to keep re-fetching those values.
This commit is contained in:
Kittywhiskers Van Gogh 2024-09-02 14:16:34 +00:00
parent 5133d88415
commit 30c30c1397
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
4 changed files with 27 additions and 17 deletions

View File

@ -1528,7 +1528,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
// We need to initialize g_stats_client early as currently, g_stats_client is called
// regardless of whether transmitting stats are desirable or not and if
// g_stats_client isn't present when that attempt is made, the client will crash.
::g_stats_client = std::make_unique<statsd::StatsdClient>();
::g_stats_client = std::make_unique<statsd::StatsdClient>(args.GetArg("-statshost", DEFAULT_STATSD_HOST),
args.GetArg("-statshostname", DEFAULT_STATSD_HOSTNAME),
args.GetArg("-statsport", DEFAULT_STATSD_PORT),
args.GetArg("-statsns", DEFAULT_STATSD_NAMESPACE),
args.GetBoolArg("-statsenabled", DEFAULT_STATSD_ENABLE));
{

View File

@ -74,11 +74,12 @@ struct _StatsdClientData {
char errmsg[1024];
};
StatsdClient::StatsdClient(const std::string& host, int port, const std::string& ns) :
d(std::make_unique<_StatsdClientData>())
StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, int port, const std::string& ns,
bool enabled)
: d{std::make_unique<_StatsdClientData>()}, m_enabled{enabled}
{
d->sock = INVALID_SOCKET;
config(host, port, ns);
config(host, nodename, port, ns);
}
StatsdClient::~StatsdClient()
@ -87,10 +88,11 @@ StatsdClient::~StatsdClient()
CloseSocket(d->sock);
}
void StatsdClient::config(const std::string& host, int port, const std::string& ns)
void StatsdClient::config(const std::string& host, const std::string& nodename, int port, const std::string& ns)
{
d->ns = ns;
d->host = host;
d->nodename = nodename;
d->port = port;
d->init = false;
CloseSocket(d->sock);
@ -98,13 +100,8 @@ void StatsdClient::config(const std::string& host, int port, const std::string&
int StatsdClient::init()
{
static bool fEnabled = gArgs.GetBoolArg("-statsenabled", DEFAULT_STATSD_ENABLE);
if (!fEnabled) return -3;
if ( d->init ) return 0;
config(gArgs.GetArg("-statshost", DEFAULT_STATSD_HOST), gArgs.GetArg("-statsport", DEFAULT_STATSD_PORT), gArgs.GetArg("-statsns", DEFAULT_STATSD_NAMESPACE));
d->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if ( d->sock == INVALID_SOCKET ) {
snprintf(d->errmsg, sizeof(d->errmsg), "could not create socket, err=%m");
@ -121,10 +118,6 @@ int StatsdClient::init()
return -2;
}
if (gArgs.IsArgSet("-statshostname")) {
d->nodename = gArgs.GetArg("-statshostname", DEFAULT_STATSD_HOSTNAME);
}
d->init = true;
return 0;
}
@ -226,6 +219,9 @@ int StatsdClient::sendDouble(std::string key, double value, const std::string& t
int StatsdClient::send(const std::string& message)
{
if (!m_enabled)
return -3;
int ret = init();
if ( ret )
{

View File

@ -25,12 +25,13 @@ struct _StatsdClientData;
class StatsdClient {
public:
explicit StatsdClient(const std::string& host = DEFAULT_STATSD_HOST, int port = DEFAULT_STATSD_PORT, const std::string& ns = DEFAULT_STATSD_NAMESPACE);
explicit StatsdClient(const std::string& host, const std::string& nodename, int port, const std::string& ns,
bool enabled);
~StatsdClient();
public:
// you can config at anytime; client will use new address (useful for Singleton)
void config(const std::string& host, int port, const std::string& ns = DEFAULT_STATSD_NAMESPACE);
void config(const std::string& host, const std::string& nodename, int port, const std::string& ns);
const char* errmsg();
public:
@ -62,6 +63,9 @@ class StatsdClient {
protected:
const std::unique_ptr<struct _StatsdClientData> d;
private:
const bool m_enabled{false};
};
} // namespace statsd

View File

@ -183,7 +183,13 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
SetupNetworking();
InitSignatureCache();
InitScriptExecutionCache();
::g_stats_client = std::make_unique<statsd::StatsdClient>();
::g_stats_client = std::make_unique<statsd::StatsdClient>(
m_node.args->GetArg("-statshost", DEFAULT_STATSD_HOST),
m_node.args->GetArg("-statshostname", DEFAULT_STATSD_HOSTNAME),
m_node.args->GetArg("-statsport", DEFAULT_STATSD_PORT),
m_node.args->GetArg("-statsns", DEFAULT_STATSD_NAMESPACE),
m_node.args->GetBoolArg("-statsenabled", DEFAULT_STATSD_ENABLE)
);
m_node.chain = interfaces::MakeChain(m_node);
m_node.netgroupman = std::make_unique<NetGroupManager>(/*asmap=*/std::vector<bool>());