mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
stats: move init logic into constructor
Also, add log messages to inform us if we're skipping initialization or it has successfully been initialized.
This commit is contained in:
parent
4bc727cd6c
commit
2def905044
@ -63,24 +63,18 @@ bool StatsdClient::ShouldSend(float sample_rate)
|
|||||||
|
|
||||||
StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
|
StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
|
||||||
bool enabled)
|
bool enabled)
|
||||||
: m_enabled{enabled}, m_port{port}, m_host{host}, m_nodename{nodename}, m_ns{ns}
|
: m_port{port}, m_host{host}, m_nodename{nodename}, m_ns{ns}
|
||||||
{
|
{
|
||||||
}
|
if (!enabled) {
|
||||||
|
LogPrintf("Transmitting stats are disabled, will not init StatsdClient\n");
|
||||||
StatsdClient::~StatsdClient()
|
return;
|
||||||
{
|
}
|
||||||
// close socket
|
|
||||||
CloseSocket(m_sock);
|
|
||||||
}
|
|
||||||
|
|
||||||
int StatsdClient::init()
|
|
||||||
{
|
|
||||||
if (m_init) return 0;
|
|
||||||
|
|
||||||
m_sock = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
m_sock = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
if (m_sock == INVALID_SOCKET) {
|
if (m_sock == INVALID_SOCKET) {
|
||||||
LogPrintf("ERROR: Cannot create socket (socket() returned error %s)\n", NetworkErrorString(WSAGetLastError()));
|
LogPrintf("ERROR: Cannot create socket (socket() returned error %s), cannot init StatsdClient\n",
|
||||||
return -1;
|
NetworkErrorString(WSAGetLastError()));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&m_server, 0, sizeof(m_server));
|
memset(&m_server, 0, sizeof(m_server));
|
||||||
@ -89,12 +83,18 @@ int StatsdClient::init()
|
|||||||
|
|
||||||
CNetAddr netaddr(m_server.sin_addr);
|
CNetAddr netaddr(m_server.sin_addr);
|
||||||
if (!LookupHost(m_host, netaddr, true) || !netaddr.GetInAddr(&m_server.sin_addr)) {
|
if (!LookupHost(m_host, netaddr, true) || !netaddr.GetInAddr(&m_server.sin_addr)) {
|
||||||
LogPrintf("ERROR: LookupHost or GetInAddr failed\n");
|
LogPrintf("ERROR: LookupHost or GetInAddr failed, cannot init StatsdClient\n");
|
||||||
return -2;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_init = true;
|
m_init = true;
|
||||||
return 0;
|
|
||||||
|
LogPrintf("StatsdClient initialized to transmit stats to %s:%d\n", m_host, m_port);
|
||||||
|
}
|
||||||
|
|
||||||
|
StatsdClient::~StatsdClient()
|
||||||
|
{
|
||||||
|
CloseSocket(m_sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* will change the original string */
|
/* will change the original string */
|
||||||
@ -180,15 +180,10 @@ int StatsdClient::sendDouble(std::string key, double value, const std::string& t
|
|||||||
|
|
||||||
int StatsdClient::send(const std::string& message)
|
int StatsdClient::send(const std::string& message)
|
||||||
{
|
{
|
||||||
if (!m_enabled)
|
if (!m_init)
|
||||||
return -3;
|
return -3;
|
||||||
|
|
||||||
int ret = init();
|
int ret = ::sendto(m_sock, message.data(), message.size(), 0, reinterpret_cast<const sockaddr*>(&m_server),
|
||||||
if (ret) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = ::sendto(m_sock, message.data(), message.size(), 0, reinterpret_cast<const sockaddr*>(&m_server),
|
|
||||||
sizeof(m_server));
|
sizeof(m_server));
|
||||||
if (ret == -1) {
|
if (ret == -1) {
|
||||||
LogPrintf("ERROR: Unable to send message (sendto() returned error %s), host=%s:%d\n",
|
LogPrintf("ERROR: Unable to send message (sendto() returned error %s), host=%s:%d\n",
|
||||||
|
@ -55,7 +55,6 @@ class StatsdClient {
|
|||||||
const std::string& type, float sample_rate);
|
const std::string& type, float sample_rate);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
int init();
|
|
||||||
static void cleanup(std::string& key);
|
static void cleanup(std::string& key);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -69,7 +68,6 @@ class StatsdClient {
|
|||||||
SOCKET m_sock{INVALID_SOCKET};
|
SOCKET m_sock{INVALID_SOCKET};
|
||||||
struct sockaddr_in m_server;
|
struct sockaddr_in m_server;
|
||||||
|
|
||||||
const bool m_enabled{false};
|
|
||||||
const uint16_t m_port;
|
const uint16_t m_port;
|
||||||
const std::string m_host;
|
const std::string m_host;
|
||||||
const std::string m_nodename;
|
const std::string m_nodename;
|
||||||
|
Loading…
Reference in New Issue
Block a user