mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
stats: move _StatsdClientData
variables into StatsdClient
There doesn't seem to be a benefit to keeping these values in a separate struct.
This commit is contained in:
parent
30c30c1397
commit
a9d1b1494d
@ -41,7 +41,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
std::unique_ptr<statsd::StatsdClient> g_stats_client;
|
std::unique_ptr<statsd::StatsdClient> g_stats_client;
|
||||||
|
|
||||||
namespace statsd {
|
namespace statsd {
|
||||||
|
|
||||||
inline bool fequal(float a, float b)
|
inline bool fequal(float a, float b)
|
||||||
{
|
{
|
||||||
const float epsilon = 0.0001;
|
const float epsilon = 0.0001;
|
||||||
@ -61,64 +60,39 @@ inline bool should_send(float sample_rate)
|
|||||||
return sample_rate > p;
|
return sample_rate > p;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct _StatsdClientData {
|
StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, short port, const std::string& ns,
|
||||||
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, const std::string& nodename, int port, const std::string& ns,
|
|
||||||
bool enabled)
|
bool enabled)
|
||||||
: d{std::make_unique<_StatsdClientData>()}, m_enabled{enabled}
|
: m_enabled{enabled}, m_port{port}, m_host{host}, m_nodename{nodename}, m_ns{ns}
|
||||||
{
|
{
|
||||||
d->sock = INVALID_SOCKET;
|
|
||||||
config(host, nodename, port, ns);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StatsdClient::~StatsdClient()
|
StatsdClient::~StatsdClient()
|
||||||
{
|
{
|
||||||
// close socket
|
// close socket
|
||||||
CloseSocket(d->sock);
|
CloseSocket(m_sock);
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int StatsdClient::init()
|
int StatsdClient::init()
|
||||||
{
|
{
|
||||||
if ( d->init ) return 0;
|
if ( m_init ) return 0;
|
||||||
|
|
||||||
d->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
m_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
if ( d->sock == INVALID_SOCKET ) {
|
if ( m_sock == INVALID_SOCKET ) {
|
||||||
snprintf(d->errmsg, sizeof(d->errmsg), "could not create socket, err=%m");
|
snprintf(m_errmsg, sizeof(m_errmsg), "could not create socket, err=%m");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&d->server, 0, sizeof(d->server));
|
memset(&m_server, 0, sizeof(m_server));
|
||||||
d->server.sin_family = AF_INET;
|
m_server.sin_family = AF_INET;
|
||||||
d->server.sin_port = htons(d->port);
|
m_server.sin_port = htons(m_port);
|
||||||
|
|
||||||
CNetAddr netaddr(d->server.sin_addr);
|
CNetAddr netaddr(m_server.sin_addr);
|
||||||
if (!LookupHost(d->host, netaddr, true) || !netaddr.GetInAddr(&d->server.sin_addr)) {
|
if (!LookupHost(m_host, netaddr, true) || !netaddr.GetInAddr(&m_server.sin_addr)) {
|
||||||
snprintf(d->errmsg, sizeof(d->errmsg), "LookupHost or GetInAddr failed");
|
snprintf(m_errmsg, sizeof(m_errmsg), "LookupHost or GetInAddr failed");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
d->init = true;
|
m_init = true;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,8 +144,8 @@ int StatsdClient::send(std::string key, size_t value, const std::string& type, f
|
|||||||
}
|
}
|
||||||
|
|
||||||
// partition stats by node name if set
|
// partition stats by node name if set
|
||||||
if (!d->nodename.empty())
|
if (!m_nodename.empty())
|
||||||
key = key + "." + d->nodename;
|
key = key + "." + m_nodename;
|
||||||
|
|
||||||
cleanup(key);
|
cleanup(key);
|
||||||
|
|
||||||
@ -179,12 +153,12 @@ int StatsdClient::send(std::string key, size_t value, const std::string& type, f
|
|||||||
if ( fequal( sample_rate, 1.0 ) )
|
if ( fequal( sample_rate, 1.0 ) )
|
||||||
{
|
{
|
||||||
snprintf(buf, sizeof(buf), "%s%s:%zd|%s",
|
snprintf(buf, sizeof(buf), "%s%s:%zd|%s",
|
||||||
d->ns.c_str(), key.c_str(), (ssize_t) value, type.c_str());
|
m_ns.c_str(), key.c_str(), (ssize_t) value, type.c_str());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(buf, sizeof(buf), "%s%s:%zd|%s|@%.2f",
|
snprintf(buf, sizeof(buf), "%s%s:%zd|%s|@%.2f",
|
||||||
d->ns.c_str(), key.c_str(), (ssize_t) value, type.c_str(), sample_rate);
|
m_ns.c_str(), key.c_str(), (ssize_t) value, type.c_str(), sample_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return send(buf);
|
return send(buf);
|
||||||
@ -197,8 +171,8 @@ int StatsdClient::sendDouble(std::string key, double value, const std::string& t
|
|||||||
}
|
}
|
||||||
|
|
||||||
// partition stats by node name if set
|
// partition stats by node name if set
|
||||||
if (!d->nodename.empty())
|
if (!m_nodename.empty())
|
||||||
key = key + "." + d->nodename;
|
key = key + "." + m_nodename;
|
||||||
|
|
||||||
cleanup(key);
|
cleanup(key);
|
||||||
|
|
||||||
@ -206,12 +180,12 @@ int StatsdClient::sendDouble(std::string key, double value, const std::string& t
|
|||||||
if ( fequal( sample_rate, 1.0 ) )
|
if ( fequal( sample_rate, 1.0 ) )
|
||||||
{
|
{
|
||||||
snprintf(buf, sizeof(buf), "%s%s:%f|%s",
|
snprintf(buf, sizeof(buf), "%s%s:%f|%s",
|
||||||
d->ns.c_str(), key.c_str(), value, type.c_str());
|
m_ns.c_str(), key.c_str(), value, type.c_str());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(buf, sizeof(buf), "%s%s:%f|%s|@%.2f",
|
snprintf(buf, sizeof(buf), "%s%s:%f|%s|@%.2f",
|
||||||
d->ns.c_str(), key.c_str(), value, type.c_str(), sample_rate);
|
m_ns.c_str(), key.c_str(), value, type.c_str(), sample_rate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return send(buf);
|
return send(buf);
|
||||||
@ -227,10 +201,10 @@ int StatsdClient::send(const std::string& message)
|
|||||||
{
|
{
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
ret = sendto(d->sock, message.data(), message.size(), 0, reinterpret_cast<const sockaddr*>(&d->server), sizeof(d->server));
|
ret = sendto(m_sock, message.data(), message.size(), 0, reinterpret_cast<const sockaddr*>(&m_server), sizeof(m_server));
|
||||||
if ( ret == -1) {
|
if ( ret == -1) {
|
||||||
snprintf(d->errmsg, sizeof(d->errmsg),
|
snprintf(m_errmsg, sizeof(m_errmsg),
|
||||||
"sendto server fail, host=%s:%d, err=%m", d->host.c_str(), d->port);
|
"sendto server fail, host=%s:%d, err=%m", m_host.c_str(), m_port);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -238,7 +212,6 @@ int StatsdClient::send(const std::string& message)
|
|||||||
|
|
||||||
const char* StatsdClient::errmsg()
|
const char* StatsdClient::errmsg()
|
||||||
{
|
{
|
||||||
return d->errmsg;
|
return m_errmsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace statsd
|
} // namespace statsd
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
#ifndef BITCOIN_STATSD_CLIENT_H
|
#ifndef BITCOIN_STATSD_CLIENT_H
|
||||||
#define BITCOIN_STATSD_CLIENT_H
|
#define BITCOIN_STATSD_CLIENT_H
|
||||||
|
|
||||||
|
#include <util/sock.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@ -20,18 +22,13 @@ static const int MIN_STATSD_PERIOD = 5;
|
|||||||
static const int MAX_STATSD_PERIOD = 60 * 60;
|
static const int MAX_STATSD_PERIOD = 60 * 60;
|
||||||
|
|
||||||
namespace statsd {
|
namespace statsd {
|
||||||
|
|
||||||
struct _StatsdClientData;
|
|
||||||
|
|
||||||
class StatsdClient {
|
class StatsdClient {
|
||||||
public:
|
public:
|
||||||
explicit StatsdClient(const std::string& host, const std::string& nodename, int port, const std::string& ns,
|
explicit StatsdClient(const std::string& host, const std::string& nodename, short port, const std::string& ns,
|
||||||
bool enabled);
|
bool enabled);
|
||||||
~StatsdClient();
|
~StatsdClient();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// you can config at anytime; client will use new address (useful for Singleton)
|
|
||||||
void config(const std::string& host, const std::string& nodename, int port, const std::string& ns);
|
|
||||||
const char* errmsg();
|
const char* errmsg();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -61,13 +58,18 @@ class StatsdClient {
|
|||||||
int init();
|
int init();
|
||||||
static void cleanup(std::string& key);
|
static void cleanup(std::string& key);
|
||||||
|
|
||||||
protected:
|
|
||||||
const std::unique_ptr<struct _StatsdClientData> d;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const bool m_enabled{false};
|
bool m_init{false};
|
||||||
};
|
char m_errmsg[1024];
|
||||||
|
SOCKET m_sock{INVALID_SOCKET};
|
||||||
|
struct sockaddr_in m_server;
|
||||||
|
|
||||||
|
const bool m_enabled{false};
|
||||||
|
const short m_port;
|
||||||
|
const std::string m_host;
|
||||||
|
const std::string m_nodename;
|
||||||
|
const std::string m_ns;
|
||||||
|
};
|
||||||
} // namespace statsd
|
} // namespace statsd
|
||||||
|
|
||||||
extern std::unique_ptr<statsd::StatsdClient> g_stats_client;
|
extern std::unique_ptr<statsd::StatsdClient> g_stats_client;
|
||||||
|
Loading…
Reference in New Issue
Block a user