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
|
#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_counter_t *peers = NULL;
prom_counter_t *hashes = NULL;
prom_counter_t *hash_expired = NULL;
prom_counter_t *queries = NULL;
prom_counter_t *requests = NULL;
prom_histogram_t *offered = NULL;
prom_gauge_t *activeNodes = NULL;
prom_gauge_t *requestsInFlight = NULL;
prom_counter_t *retries = 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);
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(
"bytes_received",
"Number of bytes received",
0,
NULL
)
);
bytesSent = prom_collector_registry_must_register_metric(
prom_counter_new(
"bytes_sent",
"Number of bytes sent",
0,
NULL
)
);
meta = prom_collector_registry_must_register_metric(
prom_counter_new(
"meta",
"Meta information value is always 1",
1,
(const char *[]){ "nodeid" }
)
);
peers = prom_collector_registry_must_register_metric(
prom_counter_new(
"peers",
"Number of total peers",
0,
NULL
)
);
hashes = prom_collector_registry_must_register_metric(
prom_counter_new(
"hashes",
"Number of hashes currently stored",
0,
NULL
)
);
hash_expired = prom_collector_registry_must_register_metric(
prom_counter_new(
"hashes_expired",
"Number of hashes expired due to inactivity",
0,
NULL
)
);
queries = prom_collector_registry_must_register_metric(
prom_counter_new(
"queries",
"Amount of queries handled",
1,
(const char *[]){"outcome"}
)
);
requests = prom_collector_registry_must_register_metric(
prom_counter_new(
"requests",
"Number of requests sent by us",
0,
NULL
)
);
requestsInFlight = prom_collector_registry_must_register_metric(
prom_gauge_new(
"requests_in_flight",
"Requests currently in flight",
0,
NULL
)
);
retries = prom_collector_registry_must_register_metric(
prom_counter_new(
"retries",
"Retries sent for unanswered requests",
0,
NULL
)
);
offered = prom_collector_registry_must_register_metric(
prom_histogram_new(
"offered",
"Nodes seen and considered for the routing table",
prom_histogram_buckets_linear(0, 1, 157),
0,
NULL
)
);
activeNodes = prom_collector_registry_must_register_metric(
prom_gauge_new(
"active_nodes",
"Nodes active in the routing table",
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");
}
|