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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
#include "metrics.h"
#include "log.h"
#include "microhttpd.h"
prom_counter_t *bytesRecv = NULL;
prom_counter_t *bytesSent = NULL;
prom_counter_t *meta = NULL;
prom_gauge_t *wakeup_time = NULL;
prom_counter_t *peers = NULL;
prom_counter_t *peers_fetched = NULL;
prom_gauge_t *current_hashes = NULL;
prom_counter_t *hashes = NULL;
prom_gauge_t *hashes_size = NULL;
prom_counter_t *hash_expired = NULL;
prom_gauge_t *hash_next_expire = NULL;
prom_counter_t *queries = NULL;
prom_counter_t *requests = NULL;
prom_histogram_t *offered = NULL;
prom_gauge_t *requestsInFlight = NULL;
prom_counter_t *retries = NULL;
prom_counter_t *keepalive_count = NULL;
prom_counter_t *lookup_count = NULL;
prom_gauge_t *routing_table_occupied = NULL;
prom_counter_t *outbox_overflow = NULL;
enum MHD_Result promhttp_handler(
void *cls,
struct MHD_Connection *connection,
const char *url,
const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **con_cls
) {
if (strcmp(method, "GET") != 0) {
char *buf = "Invalid HTTP Method\n";
struct MHD_Response *response = MHD_create_response_from_buffer(strlen(buf), (void *)buf, MHD_RESPMEM_PERSISTENT);
int ret = MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, response);
MHD_destroy_response(response);
return ret;
}
if (strcmp(url, "/") == 0) {
char *buf = "OK\n";
struct MHD_Response *response = MHD_create_response_from_buffer(strlen(buf), (void *)buf, MHD_RESPMEM_PERSISTENT);
int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;
}
if (strcmp(url, "/metrics") == 0) {
const char *buf = prom_collector_registry_bridge(PROM_COLLECTOR_REGISTRY_DEFAULT);
struct MHD_Response *response = MHD_create_response_from_buffer(strlen(buf), (void *)buf, MHD_RESPMEM_MUST_FREE);
if(MHD_add_response_header(response, "Content-Type", "text/plain; version=0.0.4") == MHD_NO) abort();
int ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
MHD_destroy_response(response);
return ret;
}
char *buf = "Bad Request\n";
struct MHD_Response *response = MHD_create_response_from_buffer(strlen(buf), (void *)buf, MHD_RESPMEM_PERSISTENT);
int ret = MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, response);
MHD_destroy_response(response);
return ret;
}
#define PORT 6981
struct MHD_Daemon *mDaemon;
// @CLEAN: We should call this begin
void metric_init() {
if(prom_collector_registry_default_init() != 0) {
fatal("Failed to initialize prometheus registry");
}
mDaemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL, &promhttp_handler, NULL, MHD_OPTION_END);
if(mDaemon == NULL) {
fatal("Failed to start http server");
}
dbg("Metrics server started on port %d", PORT);
bytesRecv = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_bytes_received_total",
"Number of bytes received",
0,
NULL
)
);
bytesSent = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_bytes_sent_total",
"Number of bytes sent",
0,
NULL
)
);
meta = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_meta_info",
"Meta information value is always 1",
1,
(const char *[]){ "nodeid" }
)
);
wakeup_time = prom_collector_registry_must_register_metric(
prom_gauge_new(
"dht_wakeup_time",
"Time for next wakeup",
1,
(const char *[]){ "component" }
)
);
peers = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_peers_total",
"Number of total peers",
0,
NULL
)
);
peers_fetched = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_peers_retrieved",
"Number of peers retrieved from the table",
0,
NULL
)
);
current_hashes = prom_collector_registry_must_register_metric(
prom_gauge_new(
"dht_live_hashes_total",
"Number of hashes with active peers",
0,
NULL
)
);
hashes = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_hashes_total",
"Number of hashes currently stored",
0,
NULL
)
);
hashes_size = prom_collector_registry_must_register_metric(
prom_gauge_new(
"dht_hashes_size",
"InfoHash table capacity",
0,
NULL
)
);
hash_expired = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_hashes_expired_total",
"Number of hashes expired due to inactivity",
0,
NULL
)
);
hash_next_expire = prom_collector_registry_must_register_metric(
prom_gauge_new(
"dht_hashes_next_expire",
"Next time a hash expires",
0,
NULL
)
);
queries = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_queries_total",
"Amount of queries handled",
2,
(const char *[]){"type", "outcome"}
)
);
requests = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_requests_total",
"Number of requests sent by us",
0,
NULL
)
);
requestsInFlight = prom_collector_registry_must_register_metric(
prom_gauge_new(
"dht_requests_in_flight",
"Requests currently in flight",
0,
NULL
)
);
retries = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_retries_total",
"Retries sent for unanswered requests",
0,
NULL
)
);
offered = prom_collector_registry_must_register_metric(
prom_histogram_new(
"dht_offered",
"Nodes seen and considered for the routing table",
prom_histogram_buckets_linear(0, 1, 157),
0,
NULL
)
);
keepalive_count = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_keepalive_count",
"Number of times a node has been pinged due to inactivity",
0,
NULL
)
);
lookup_count = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_lookup_count",
"Number of lookup commands issued",
0,
NULL
)
);
routing_table_occupied = prom_collector_registry_must_register_metric(
prom_gauge_new(
"dht_routing_occupancy",
"Number of nodes in each routing bucket",
1,
(const char *[]) {"dist"}
)
);
outbox_overflow = prom_collector_registry_must_register_metric(
prom_counter_new(
"dht_outbox_overflow",
"Number of times the outbox has overflowed",
0,
NULL
)
);
}
void metric_end() {
MHD_stop_daemon(mDaemon);
if(prom_collector_registry_destroy(PROM_COLLECTOR_REGISTRY_DEFAULT) != 0) {
fatal("Failed to destroy the registry");
}
dbg("Metric server stopped");
}
|