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
|
|
|
|
2024-09-05 22:04:47 +02:00
|
|
|
static constexpr bool DEFAULT_STATSD_ENABLE{false};
|
|
|
|
static constexpr uint16_t 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{""};
|
2020-12-15 17:22:23 +01:00
|
|
|
|
|
|
|
// schedule periodic measurements, in seconds: default - 1 minute, min - 5 sec, max - 1h.
|
2024-09-05 22:04:47 +02:00
|
|
|
static constexpr int DEFAULT_STATSD_PERIOD{60};
|
|
|
|
static constexpr int MIN_STATSD_PERIOD{5};
|
2024-09-11 17:02:10 +02:00
|
|
|
static constexpr int MAX_STATSD_PERIOD{60 * 60};
|
2020-12-15 17:22:23 +01:00
|
|
|
|
|
|
|
namespace statsd {
|
|
|
|
class StatsdClient {
|
|
|
|
public:
|
2024-09-05 22:04:47 +02:00
|
|
|
explicit StatsdClient(const std::string& host, const std::string& nodename, uint16_t port,
|
|
|
|
const std::string& ns, bool enabled);
|
2020-12-15 17:22:23 +01:00
|
|
|
|
|
|
|
public:
|
2024-09-10 17:38:11 +02:00
|
|
|
bool inc(const std::string& key, float sample_rate = 1.f);
|
|
|
|
bool dec(const std::string& key, float sample_rate = 1.f);
|
|
|
|
bool count(const std::string& key, int64_t value, float sample_rate = 1.f);
|
|
|
|
bool gauge(const std::string& key, int64_t value, float sample_rate = 1.f);
|
|
|
|
bool gaugeDouble(const std::string& key, double value, float sample_rate = 1.f);
|
|
|
|
bool timing(const std::string& key, int64_t ms, float sample_rate = 1.f);
|
2020-12-15 17:22:23 +01:00
|
|
|
|
|
|
|
/* (Low Level Api) manually send a message
|
|
|
|
* type = "c", "g" or "ms"
|
|
|
|
*/
|
2024-09-10 17:38:11 +02:00
|
|
|
bool send(std::string key, int64_t value, const std::string& type, float sample_rate);
|
|
|
|
bool sendDouble(std::string key, double value, const std::string& type, float sample_rate);
|
2020-12-15 17:22:23 +01:00
|
|
|
|
2024-09-02 16:16:34 +02:00
|
|
|
private:
|
2024-09-10 17:38:11 +02:00
|
|
|
/**
|
|
|
|
* (Low Level Api) manually send a message
|
|
|
|
* which might be composed of several lines.
|
|
|
|
*/
|
|
|
|
bool send(const std::string& message);
|
|
|
|
|
|
|
|
void cleanup(std::string& key);
|
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-03 20:01:50 +02:00
|
|
|
std::unique_ptr<Sock> m_sock{nullptr};
|
|
|
|
std::pair<struct sockaddr_storage, socklen_t> m_server{{}, sizeof(struct sockaddr_storage)};
|
2024-09-05 09:51:25 +02:00
|
|
|
|
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
|