From 30c30c1397d23fbe6b6e7da57c2b52e97d1899c9 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Mon, 2 Sep 2024 14:16:34 +0000 Subject: [PATCH] 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. --- src/init.cpp | 6 +++++- src/statsd_client.cpp | 22 +++++++++------------- src/statsd_client.h | 8 ++++++-- src/test/util/setup_common.cpp | 8 +++++++- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 6f3ba2c958..8630d10a32 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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(); + ::g_stats_client = std::make_unique(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)); { diff --git a/src/statsd_client.cpp b/src/statsd_client.cpp index cd8a940125..429ad72e35 100644 --- a/src/statsd_client.cpp +++ b/src/statsd_client.cpp @@ -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 ) { diff --git a/src/statsd_client.h b/src/statsd_client.h index 736b55e167..f93469d20b 100644 --- a/src/statsd_client.h +++ b/src/statsd_client.h @@ -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 d; + + private: + const bool m_enabled{false}; }; } // namespace statsd diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 2f7538ddf5..4e98f2cc3a 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -183,7 +183,13 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve SetupNetworking(); InitSignatureCache(); InitScriptExecutionCache(); - ::g_stats_client = std::make_unique(); + ::g_stats_client = std::make_unique( + 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(/*asmap=*/std::vector());