-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathstatsd.h
More file actions
116 lines (102 loc) · 3.4 KB
/
statsd.h
File metadata and controls
116 lines (102 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (C) 2011-2026 Redis Labs Ltd.
*
* This file is part of memtier_benchmark.
*
* memtier_benchmark is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* memtier_benchmark is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with memtier_benchmark. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _STATSD_H
#define _STATSD_H
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
/**
* Simple StatsD client for sending metrics over UDP.
*
* StatsD protocol format:
* <metric_name>:<value>|<type>[|@<sample_rate>]
*
* Types:
* g = gauge (instantaneous value)
* c = counter (increment/decrement)
* ms = timing (milliseconds)
* h = histogram
* s = set
*/
class statsd_client
{
private:
int m_socket;
struct sockaddr_in m_server_addr;
char m_prefix[256];
char m_run_label[128];
char m_graphite_host[256];
unsigned short m_graphite_port;
bool m_enabled;
bool m_initialized;
// Internal method to send a formatted metric
void send_metric(const char *name, const char *value, const char *type);
public:
statsd_client();
~statsd_client();
/**
* Initialize the StatsD client.
* @param host StatsD server hostname or IP
* @param port StatsD server port (default 8125)
* @param prefix Prefix for all metric names (e.g., "memtier")
* @param run_label Label for this benchmark run (e.g., "asm_scaling")
* @return true if initialization successful, false otherwise
*/
bool init(const char *host, unsigned short port, const char *prefix, const char *run_label);
/**
* Set the Graphite HTTP port for event annotations.
* Default is 8080, matching the host-side port in docker-compose.statsd.yml.
* Use 80 if running memtier_benchmark inside the same Docker network.
* @param port Graphite web UI port
*/
void set_graphite_port(unsigned short port) { m_graphite_port = port; }
/**
* Close the UDP socket and cleanup.
*/
void close();
/**
* Check if the client is enabled and ready to send metrics.
*/
bool is_enabled() const { return m_enabled && m_initialized; }
/**
* Send a gauge metric (instantaneous value).
* @param name Metric name (will be prefixed)
* @param value Current value
*/
void gauge(const char *name, double value);
/**
* Send a gauge metric with long value.
*/
void gauge(const char *name, long value);
/**
* Send a timing metric (in milliseconds).
* @param name Metric name (will be prefixed)
* @param value_ms Time in milliseconds
*/
void timing(const char *name, double value_ms);
/**
* Send an event/annotation to Graphite.
* This sends an HTTP POST to Graphite's events API.
* @param what Short description of the event
* @param data Additional event data (optional)
* @param tags Comma-separated tags (optional)
*/
void event(const char *what, const char *data = NULL, const char *tags = NULL);
};
#endif // _STATSD_H