2024-09-05 09:52:37 +02:00
|
|
|
// Copyright (c) 2014-2017 Statoshi Developers
|
2023-12-31 01:00:00 +01:00
|
|
|
// Copyright (c) 2020-2023 The Dash Core developers
|
2023-01-12 22:32:07 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-12-15 17:22:23 +01:00
|
|
|
#ifndef BITCOIN_STATSD_CLIENT_H
|
|
|
|
#define BITCOIN_STATSD_CLIENT_H
|
|
|
|
|
2024-09-05 22:42:14 +02:00
|
|
|
#include <random.h>
|
|
|
|
#include <threadsafety.h>
|
2024-09-05 09:51:25 +02:00
|
|
|
#include <util/sock.h>
|
|
|
|
|
2020-12-15 17:22:23 +01:00
|
|
|
#include <string>
|
2021-12-28 22:54:50 +01:00
|
|
|
#include <memory>
|
2020-12-15 17:22:23 +01:00
|
|
|
|
|
|
|
static const bool DEFAULT_STATSD_ENABLE = false;
|
2024-09-11 16:33:55 +02:00
|
|
|
static const uint16_t DEFAULT_STATSD_PORT = 8125;
|
2020-12-15 17:22:23 +01:00
|
|
|
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 int DEFAULT_STATSD_PERIOD = 60;
|
|
|
|
static const int MIN_STATSD_PERIOD = 5;
|
|
|
|
static const int MAX_STATSD_PERIOD = 60 * 60;
|
|
|
|
|
|
|
|
namespace statsd {
|
|
|
|
class StatsdClient {
|
|
|
|
public:
|
2024-09-11 16:33:55 +02:00
|
|
|
explicit StatsdClient(const std::string& host, const std::string& nodename, uint16_t port, const std::string& ns,
|
2024-09-02 16:16:34 +02:00
|
|
|
bool enabled);
|
2020-12-15 17:22:23 +01:00
|
|
|
~StatsdClient();
|
|
|
|
|
|
|
|
public:
|
2024-09-05 22:42:14 +02:00
|
|
|
int inc(const std::string& key, float sample_rate = 1.f);
|
|
|
|
int dec(const std::string& key, float sample_rate = 1.f);
|
|
|
|
int count(const std::string& key, int64_t value, float sample_rate = 1.f);
|
|
|
|
int gauge(const std::string& key, int64_t value, float sample_rate = 1.f);
|
|
|
|
int gaugeDouble(const std::string& key, double value, float sample_rate = 1.f);
|
|
|
|
int timing(const std::string& key, int64_t ms, float sample_rate = 1.f);
|
2020-12-15 17:22:23 +01:00
|
|
|
|
|
|
|
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"
|
|
|
|
*/
|
2024-09-11 16:33:55 +02:00
|
|
|
int send(std::string key, int64_t value,
|
2020-12-15 17:22:23 +01:00
|
|
|
const std::string& type, float sample_rate);
|
|
|
|
int sendDouble(std::string key, double value,
|
|
|
|
const std::string& type, float sample_rate);
|
|
|
|
|
|
|
|
protected:
|
2021-04-15 19:58:04 +02:00
|
|
|
static void cleanup(std::string& key);
|
2020-12-15 17:22:23 +01:00
|
|
|
|
2024-09-02 16:16:34 +02:00
|
|
|
private:
|
2024-09-05 22:42:14 +02:00
|
|
|
bool ShouldSend(float sample_rate);
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable Mutex cs;
|
|
|
|
mutable FastRandomContext insecure_rand GUARDED_BY(cs);
|
|
|
|
|
2024-09-05 09:51:25 +02:00
|
|
|
bool m_init{false};
|
|
|
|
SOCKET m_sock{INVALID_SOCKET};
|
|
|
|
struct sockaddr_in m_server;
|
|
|
|
|
2024-09-11 16:33:55 +02:00
|
|
|
const uint16_t m_port;
|
2024-09-05 09:51:25 +02:00
|
|
|
const std::string m_host;
|
|
|
|
const std::string m_nodename;
|
|
|
|
const std::string m_ns;
|
2020-12-15 17:22:23 +01:00
|
|
|
};
|
2021-04-15 19:58:04 +02:00
|
|
|
} // namespace statsd
|
2020-12-15 17:22:23 +01:00
|
|
|
|
2024-09-02 14:00:02 +02:00
|
|
|
extern std::unique_ptr<statsd::StatsdClient> g_stats_client;
|
2020-12-15 17:22:23 +01:00
|
|
|
|
|
|
|
#endif // BITCOIN_STATSD_CLIENT_H
|