From 5527932342c560bc12c0682b0230eb470e763cfa Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Sat, 18 Feb 2023 18:10:26 +0000 Subject: [PATCH] analytics: cleanup now unused statsd client implementation --- contrib/devtools/copyright_header.py | 1 - src/Makefile.am | 2 - src/statsd_client.cpp | 247 --------------------------- src/statsd_client.h | 69 -------- test/lint/lint-cppcheck-dash.sh | 1 - test/lint/lint-format-strings.py | 2 - test/lint/lint-locale-dependence.sh | 1 - 7 files changed, 323 deletions(-) delete mode 100644 src/statsd_client.cpp delete mode 100644 src/statsd_client.h diff --git a/contrib/devtools/copyright_header.py b/contrib/devtools/copyright_header.py index bebbc18a013d03..4a10c9493bc028 100755 --- a/contrib/devtools/copyright_header.py +++ b/contrib/devtools/copyright_header.py @@ -30,7 +30,6 @@ 'src/crypto/*', 'src/ctpl_stl.h', 'src/reverse_iterator.h', - 'src/statsd_client.cpp', 'src/test/fuzz/FuzzedDataProvider.h', 'src/tinyformat.h', 'src/bench/nanobench.h', diff --git a/src/Makefile.am b/src/Makefile.am index 89ffd8d694eb42..1ed225c6e50f80 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -279,7 +279,6 @@ BITCOIN_CORE_H = \ spork.h \ stacktraces.h \ streams.h \ - statsd_client.h \ support/allocators/mt_pooled_secure.h \ support/allocators/pooled_secure.h \ support/allocators/secure.h \ @@ -459,7 +458,6 @@ libbitcoin_server_a_SOURCES = \ script/sigcache.cpp \ shutdown.cpp \ spork.cpp \ - statsd_client.cpp \ timedata.cpp \ torcontrol.cpp \ txdb.cpp \ diff --git a/src/statsd_client.cpp b/src/statsd_client.cpp deleted file mode 100644 index e1c745d593c4e4..00000000000000 --- a/src/statsd_client.cpp +++ /dev/null @@ -1,247 +0,0 @@ -/** -Copyright (c) 2014, Rex -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -* Neither the name of the {organization} nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -**/ - -#include - -#include -#include -#include -#include - -#include -#include - -statsd::StatsdClient statsClient; - -namespace statsd { - -inline bool fequal(float a, float b) -{ - const float epsilon = 0.0001; - return ( fabs(a - b) < epsilon ); -} - -thread_local FastRandomContext insecure_rand; - -inline bool should_send(float sample_rate) -{ - if ( fequal(sample_rate, 1.0) ) - { - return true; - } - - float p = float(insecure_rand(std::numeric_limits::max())) / float(std::numeric_limits::max()); - return sample_rate > p; -} - -struct _StatsdClientData { - SOCKET sock; - struct sockaddr_in server; - - std::string ns; - std::string host; - std::string nodename; - short port; - bool init; - - char errmsg[1024]; -}; - -StatsdClient::StatsdClient(const std::string& host, int port, const std::string& ns) : - d(std::make_unique<_StatsdClientData>()) -{ - d->sock = INVALID_SOCKET; - config(host, port, ns); -} - -StatsdClient::~StatsdClient() -{ - // close socket - CloseSocket(d->sock); -} - -void StatsdClient::config(const std::string& host, int port, const std::string& ns) -{ - d->ns = ns; - d->host = host; - d->port = port; - d->init = false; - CloseSocket(d->sock); -} - -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"); - return -1; - } - - memset(&d->server, 0, sizeof(d->server)); - d->server.sin_family = AF_INET; - d->server.sin_port = htons(d->port); - - CNetAddr netaddr(d->server.sin_addr); - if (!LookupHost(d->host, netaddr, true) || !netaddr.GetInAddr(&d->server.sin_addr)) { - snprintf(d->errmsg, sizeof(d->errmsg), "LookupHost or GetInAddr failed"); - return -2; - } - - if (gArgs.IsArgSet("-statshostname")) { - d->nodename = gArgs.GetArg("-statshostname", DEFAULT_STATSD_HOSTNAME); - } - - d->init = true; - return 0; -} - -/* will change the original string */ -void StatsdClient::cleanup(std::string& key) -{ - size_t pos = key.find_first_of(":|@"); - while ( pos != std::string::npos ) - { - key[pos] = '_'; - pos = key.find_first_of(":|@"); - } -} - -int StatsdClient::dec(const std::string& key, float sample_rate) -{ - return count(key, -1, sample_rate); -} - -int StatsdClient::inc(const std::string& key, float sample_rate) -{ - return count(key, 1, sample_rate); -} - -int StatsdClient::count(const std::string& key, size_t value, float sample_rate) -{ - return send(key, value, "c", sample_rate); -} - -int StatsdClient::gauge(const std::string& key, size_t value, float sample_rate) -{ - return send(key, value, "g", sample_rate); -} - -int StatsdClient::gaugeDouble(const std::string& key, double value, float sample_rate) -{ - return sendDouble(key, value, "g", sample_rate); -} - -int StatsdClient::timing(const std::string& key, size_t ms, float sample_rate) -{ - return send(key, ms, "ms", sample_rate); -} - -int StatsdClient::send(std::string key, size_t value, const std::string& type, float sample_rate) -{ - if (!should_send(sample_rate)) { - return 0; - } - - // partition stats by node name if set - if (!d->nodename.empty()) - key = key + "." + d->nodename; - - cleanup(key); - - char buf[256]; - if ( fequal( sample_rate, 1.0 ) ) - { - snprintf(buf, sizeof(buf), "%s%s:%zd|%s", - d->ns.c_str(), key.c_str(), (ssize_t) value, type.c_str()); - } - else - { - snprintf(buf, sizeof(buf), "%s%s:%zd|%s|@%.2f", - d->ns.c_str(), key.c_str(), (ssize_t) value, type.c_str(), sample_rate); - } - - return send(buf); -} - -int StatsdClient::sendDouble(std::string key, double value, const std::string& type, float sample_rate) -{ - if (!should_send(sample_rate)) { - return 0; - } - - // partition stats by node name if set - if (!d->nodename.empty()) - key = key + "." + d->nodename; - - cleanup(key); - - char buf[256]; - if ( fequal( sample_rate, 1.0 ) ) - { - snprintf(buf, sizeof(buf), "%s%s:%f|%s", - d->ns.c_str(), key.c_str(), value, type.c_str()); - } - else - { - snprintf(buf, sizeof(buf), "%s%s:%f|%s|@%.2f", - d->ns.c_str(), key.c_str(), value, type.c_str(), sample_rate); - } - - return send(buf); -} - -int StatsdClient::send(const std::string& message) -{ - int ret = init(); - if ( ret ) - { - return ret; - } - ret = sendto(d->sock, message.data(), message.size(), 0, (struct sockaddr *) &d->server, sizeof(d->server)); - if ( ret == -1) { - snprintf(d->errmsg, sizeof(d->errmsg), - "sendto server fail, host=%s:%d, err=%m", d->host.c_str(), d->port); - return -1; - } - return 0; -} - -const char* StatsdClient::errmsg() -{ - return d->errmsg; -} -} // namespace statsd diff --git a/src/statsd_client.h b/src/statsd_client.h deleted file mode 100644 index 88c386ce49bb96..00000000000000 --- a/src/statsd_client.h +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright (c) 2020-2022 The Dash Core developers -// Distributed under the MIT software license, see the accompanying -// file COPYING or http://www.opensource.org/licenses/mit-license.php. - -#ifndef BITCOIN_STATSD_CLIENT_H -#define BITCOIN_STATSD_CLIENT_H - -#include -#include - -static const bool DEFAULT_STATSD_ENABLE = false; -static const int DEFAULT_STATSD_PORT = 8125; -static const std::string DEFAULT_STATSD_HOST = "127.0.0.1"; -static const std::string DEFAULT_STATSD_HOSTNAME = ""; -static const std::string DEFAULT_STATSD_NAMESPACE = ""; - -// schedule periodic measurements, in seconds: default - 1 minute, min - 5 sec, max - 1h. -static const int64_t DEFAULT_STATSD_PERIOD = 60; -static const int64_t MIN_STATSD_PERIOD = 5; -static const int64_t MAX_STATSD_PERIOD = 60 * 60; - -namespace statsd { - -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); - ~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); - const char* errmsg(); - - public: - int inc(const std::string& key, float sample_rate = 1.0); - int dec(const std::string& key, float sample_rate = 1.0); - int count(const std::string& key, size_t value, float sample_rate = 1.0); - int gauge(const std::string& key, size_t value, float sample_rate = 1.0); - int gaugeDouble(const std::string& key, double value, float sample_rate = 1.0); - int timing(const std::string& key, size_t ms, float sample_rate = 1.0); - - public: - /** - * (Low Level Api) manually send a message - * which might be composed of several lines. - */ - int send(const std::string& message); - - /* (Low Level Api) manually send a message - * type = "c", "g" or "ms" - */ - int send(std::string key, size_t value, - const std::string& type, float sample_rate); - int sendDouble(std::string key, double value, - const std::string& type, float sample_rate); - - protected: - int init(); - static void cleanup(std::string& key); - - protected: - std::unique_ptr d; -}; - -} // namespace statsd - -#endif // BITCOIN_STATSD_CLIENT_H diff --git a/test/lint/lint-cppcheck-dash.sh b/test/lint/lint-cppcheck-dash.sh index 4d8fa400e9b628..8f01b41fe5dc99 100755 --- a/test/lint/lint-cppcheck-dash.sh +++ b/test/lint/lint-cppcheck-dash.sh @@ -90,7 +90,6 @@ FILES=$(git ls-files -- "src/analytics/*.cpp" \ "src/spork.*" \ "src/saltedhasher.*" \ "src/stacktraces.*" \ - "src/statsd_client.*" \ "src/test/block_reward_reallocation_tests.cpp" \ "src/test/bls_tests.cpp" \ "src/test/dip0020opcodes_tests.cpp" \ diff --git a/test/lint/lint-format-strings.py b/test/lint/lint-format-strings.py index e0d262cb684b7b..e81841341376db 100755 --- a/test/lint/lint-format-strings.py +++ b/test/lint/lint-format-strings.py @@ -24,8 +24,6 @@ ("src/qt/networkstyle.cpp", "strprintf(titleAddText, gArgs.GetDevNetName())"), ("src/rpc/evo.cpp", "strprintf(it->second, nParamNum)"), ("src/stacktraces.cpp", "strprintf(fmtStr, i, si.pc, lstr, fstr)"), - ("src/statsd_client.cpp", "snprintf(d->errmsg, sizeof(d->errmsg), \"could not create socket, err=%m\")"), - ("src/statsd_client.cpp", "snprintf(d->errmsg, sizeof(d->errmsg), \"sendto server fail, host=%s:%d, err=%m\", d->host.c_str(), d->port)"), ("src/util/system.cpp", "strprintf(_(COPYRIGHT_HOLDERS).translated, COPYRIGHT_HOLDERS_SUBSTITUTION)"), ("src/wallet/wallet.h", "WalletLogPrintf(std::string fmt, Params... parameters)"), ("src/wallet/wallet.h", "LogPrintf((\"%s \" + fmt).c_str(), GetDisplayName(), parameters...)"), diff --git a/test/lint/lint-locale-dependence.sh b/test/lint/lint-locale-dependence.sh index 7020c43917d75b..36377aec7d961b 100755 --- a/test/lint/lint-locale-dependence.sh +++ b/test/lint/lint-locale-dependence.sh @@ -13,7 +13,6 @@ KNOWN_VIOLATIONS=( "src/dbwrapper.cpp.*stoul" "src/dbwrapper.cpp:.*vsnprintf" "src/rest.cpp:.*strtol" - "src/statsd_client.cpp:.*snprintf" "src/test/dbwrapper_tests.cpp:.*snprintf" "src/test/fuzz/locale.cpp" "src/test/fuzz/string.cpp"