mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 12:32:48 +01:00
b559a8f904
* Backport Statoshi This backports some of https://github.com/jlopp/statoshi. Missing stuff: README.md and client name changes, segwit and fee estimation stats. Fix RejectCodeToString Fix copy-paste mistake s/InvalidBlockFound/InvalidChainFound/ * Merge #16728: move-only: move coins statistics utils out of RPC 8a3b2eb17572ca2131778d52cc25ec359470a90f move-only: move coins statistics utils out of RPC (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11): Parent PR: #15606 Issue: #15605 Specification: https://github.com/jamesob/assumeutxo-docs/tree/master/proposal --- In the short-term, this move-only commit will help with fuzzing (https://github.com/bitcoin/bitcoin/pull/15606#issuecomment-524482297). Later, these procedures will be used to compute statistics (particularly a content hash) for UTXO sets coming in from snapshots. Most easily reviewed with `git ... --color-moved=dimmed_zebra`. A nice follow-up would be adding unittests, which I'll do if nobody else gets around to it. ACKs for top commit: MarcoFalke: ACK 8a3b2eb17572ca2131778d52cc25ec359470a90f, checked --color-moved=dimmed-zebra Tree-SHA512: a187d2f7590ad2450b8e8fa3d038c80a04fc3d903618c24222d7e3172250ce51badea35860c86101f2ba266eb4354e6efb8d7d508b353f29276e4665a1efdf74 * Fix 16728 * Modernize StatsdClient - Reuse some functionality from netbase - Switch from GetRand to FastRandomContext - Drop `using namespace std` and add `// namespace statsd` * Introduce PeriodicStats and make StatsdClient configurable via -stats<smth> (enabled/host/port/ns/period) * Move/rename tip stats from CheckBlock to ConnectBlock * Add new false positives to lint-format-strings.py * Add snprintf in statsd_client to the list of known violations in lint-locale-dependence.sh * Fix incorrect include guard * Use bracket syntax includes * Replace magic numbers with defaults * Move connection stats calculation into its own function And bail out early if stats are disabled * assert in PeriodicStats Co-authored-by: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com> Co-authored-by: MarcoFalke <falke.marco@gmail.com> Co-authored-by: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com>
253 lines
6.6 KiB
C++
253 lines
6.6 KiB
C++
/**
|
|
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 <statsd_client.h>
|
|
|
|
#include <compat.h>
|
|
#include <netbase.h>
|
|
#include <random.h>
|
|
#include <util.h>
|
|
|
|
#include <math.h>
|
|
#include <time.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
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<int>::max()) / std::numeric_limits<int>::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 = new _StatsdClientData;
|
|
d->sock = INVALID_SOCKET;
|
|
config(host, port, ns);
|
|
}
|
|
|
|
StatsdClient::~StatsdClient()
|
|
{
|
|
// close socket
|
|
CloseSocket(d->sock);
|
|
delete d;
|
|
d = NULL;
|
|
}
|
|
|
|
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.c_str(), 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(), value, type.c_str());
|
|
}
|
|
else
|
|
{
|
|
snprintf(buf, sizeof(buf), "%s%s:%zd|%s|@%.2f",
|
|
d->ns.c_str(), key.c_str(), 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
|