-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackhole.c
638 lines (529 loc) · 16.2 KB
/
blackhole.c
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* blackhole-ext
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <netinet/in.h>
#include <netdb.h>
#include "php.h"
#include "ext/standard/info.h"
#include "php_blackhole.h"
ZEND_DECLARE_MODULE_GLOBALS(blackhole)
#ifdef COMPILE_DL_BLACKHOLE
ZEND_GET_MODULE(blackhole)
#endif
#define MICRO_IN_SEC 1000000.00
/* {{{ internal funcs */
static inline double php_blackhole_timeval_to_double(struct timeval tp)
{
return tp.tv_sec + tp.tv_usec / MICRO_IN_SEC;
}
static blackhole_statsd *php_blackhole_statsd_open(const char *host, int port) /* {{{ */
{
if (!host || !port) {
return NULL;
}
blackhole_statsd *statsd = malloc(sizeof(blackhole_statsd));
if (statsd == NULL) {
php_error_docref(NULL, E_WARNING, "unable to allocate memory for statsd");
return NULL;
}
if ((statsd->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == FAILURE) {
php_error_docref(NULL, E_WARNING, "failed to initialize StatsD UDP socket");
free(statsd);
return NULL;
}
memset(&statsd->server, 0, sizeof(statsd->server));
statsd->server.sin_family = AF_INET;
statsd->server.sin_port = htons(port);
struct addrinfo *result = NULL, hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
int error;
if ((error = getaddrinfo(host, NULL, &hints, &result))) {
php_error_docref(NULL, E_WARNING, "failed to resolve StatsD server hostname '%s': %s", host, gai_strerror(error));
free(statsd);
return NULL;
}
memcpy(&(statsd->server.sin_addr), &((struct sockaddr_in*)result->ai_addr)->sin_addr, sizeof(struct in_addr));
freeaddrinfo(result);
return statsd;
}
/* }}} */
static int php_blackhole_statsd_send(blackhole_statsd *statsd, char *data)
{
ssize_t sent;
sent = sendto(statsd->sock, data, strlen(data), 0, (struct sockaddr *)&statsd->server, sizeof(statsd->server));
return sent != FAILURE ? SUCCESS : FAILURE;
}
static void php_blackhole_statsd_close(blackhole_statsd *statsd) /* {{{ */
{
if (statsd == NULL) {
return;
}
if (statsd->sock != FAILURE) {
close(statsd->sock);
statsd->sock = FAILURE;
}
free(statsd);
}
/* }}} */
static double php_blackhole_request_duration() /* {{{ */
{
struct timeval tp;
if (gettimeofday(&tp, NULL) == FAILURE) {
return 0;
}
timersub(&tp, &BLACKHOLE_G(request_started_at), &tp);
return php_blackhole_timeval_to_double(tp);
}
/* }}} */
static int php_blackhole_str_append(char **str, const char *buf) /* {{{ */
{
if (*str == NULL) {
size_t size = strlen(*str);
str = malloc(size + 1);
memcpy(str, buf, size);
str[size] = NULL;
return SUCCESS;
}
char *str_new;
if (asprintf(&str_new, "%s%s", *str, buf) == FAILURE) {
return FAILURE;
}
free(*str);
*str = str_new;
return SUCCESS;
}
/* }}} */
/* {{{ test_metric:456|g|#test_tag1:test_value1,test_tag2:test_value2
*/
static char *php_blackhole_create_request_data(const char *metric_name, HashTable *tags, int request_duration_ms)
{
if (metric_name == NULL) {
return NULL;
}
char *data;
int tags_cnt = tags == NULL ? 0 : zend_hash_num_elements(tags);
if (tags_cnt == 0) {
if (asprintf(&data, "%s:%d|ms", metric_name, request_duration_ms) < SUCCESS) {
php_error_docref(NULL, E_WARNING, "failed to allocate request data");
return NULL;
}
return data;
}
if (asprintf(&data, "%s:%d|ms|#", metric_name, request_duration_ms) < SUCCESS) {
php_error_docref(NULL, E_WARNING, "failed to allocate request data");
return NULL;
}
HashPosition position;
zval *zv;
int i = 0;
for (zend_hash_internal_pointer_reset_ex(tags, &position);
(zv = zend_hash_get_current_data_ex(tags, &position)) != NULL;
zend_hash_move_forward_ex(tags, &position)) {
zend_string *key;
zend_ulong index;
char *tag_value = Z_PTR_P(zv);
i++;
if (zend_hash_get_current_key_ex(tags, &key, &index, &position) == HASH_KEY_IS_STRING) {
char *tag_buf;
if (i < tags_cnt) {
if (asprintf(&tag_buf, "%s:%s,", key->val, tag_value) < SUCCESS) {
php_error_docref(NULL, E_WARNING, "failed to allocate tag string for request data");
return NULL;
}
} else {
if (asprintf(&tag_buf, "%s:%s", key->val, tag_value) < SUCCESS) {
php_error_docref(NULL, E_WARNING, "failed to allocate tag string for request data");
return NULL;
}
}
php_blackhole_str_append(&data, tag_buf);
free(tag_buf);
} else {
continue;
}
}
return data;
}
/* }}} */
static blackhole_metric *php_blackhole_find_metric(const char *name) /* {{{ */
{
for (int i = 0; i < BLACKHOLE_G(metrics_initialized); i++) {
blackhole_metric *metric = &BLACKHOLE_G(metrics)[i];
if (strcmp(metric->name, name) == 0) {
return metric;
}
}
return NULL;
}
/* }}} */
static int php_blackhole_send_metric(blackhole_metric *metric, int request_duration_ms) /* {{{ */
{
char *data;
blackhole_statsd *statsd;
ssize_t sent;
data = php_blackhole_create_request_data(metric->name, &metric->tags, request_duration_ms);
if (data == NULL) {
return FAILURE;
}
statsd = php_blackhole_statsd_open(metric->host, metric->port);
if (statsd == NULL) {
return FAILURE;
}
int isSent = SUCCESS;
sent = php_blackhole_statsd_send(statsd, data);
if (sent == FAILURE) {
php_error_docref(NULL, E_WARNING, "failed to send metric to StatsD: %s", strerror(errno));
isSent = FAILURE;
}
php_blackhole_statsd_close(statsd);
free(data);
return isSent;
}
/* }}} */
static void php_blackhole_free_metric(blackhole_metric *metric) /* {{{ */
{
zend_hash_destroy(&metric->tags);
if (metric->host) {
efree(metric->host);
metric->host = NULL;
}
if (metric->name) {
efree(metric->name);
metric->name = NULL;
}
}
/* }}} */
static void php_blackhole_tag_dtor(zval *zv) /* {{{ */
{
char *tag = Z_PTR_P(zv);
if (tag) {
efree(tag);
}
}
/* {{{ proto string blackhole_get_host()
Get StatsD host */
static PHP_FUNCTION(blackhole_get_host)
{
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
RETURN_EMPTY_STRING();
}
/* {{{ proto bool blackhole_set_host(string host)
Set StatsD host */
static PHP_FUNCTION(blackhole_set_host)
{
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string blackhole_get_port()
Set StatsD port */
static PHP_FUNCTION(blackhole_get_port)
{
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
RETURN_LONG(0);
}
/* }}} */
/* {{{ proto bool blackhole_set_port(int port)
Set StatsD port */
static PHP_FUNCTION(blackhole_set_port)
{
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
RETURN_TRUE;
}
/* }}} */
/* {{{ proto string blackhole_get_metric_name()
Set StatsD metric name */
static PHP_FUNCTION(blackhole_get_metric_name)
{
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
RETURN_EMPTY_STRING();
}
/* {{{ proto bool blackhole_set_metric_name(string metric_name)
Set StatsD metric name */
static PHP_FUNCTION(blackhole_set_metric_name)
{
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
RETURN_TRUE;
}
/* {{{ proto string blackhole_get_overall_metric_name()
Set StatsD metric name */
static PHP_FUNCTION(blackhole_get_overall_metric_name)
{
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
RETURN_EMPTY_STRING();
}
/* {{{ proto bool blackhole_set_overall_metric_name(string metric_name)
Set StatsD metric name */
static PHP_FUNCTION(blackhole_set_overall_metric_name)
{
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
RETURN_TRUE;
}
/* {{{ proto array blackhole_get_tags()
Get request tags */
static PHP_FUNCTION(blackhole_get_tags)
{
if (zend_parse_parameters(ZEND_NUM_ARGS(), "") != SUCCESS) {
return;
}
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
array_init(return_value);
}
/* {{{ proto bool blackhole_set_tag(string tag, string value)
Set request tag */
static PHP_FUNCTION(blackhole_set_tag)
{
php_error_docref(NULL, E_DEPRECATED, "function is deprecated");
RETURN_TRUE;
}
/* }}} */
// NEW API
static PHP_FUNCTION(blackhole_metric_add) /* {{{ */
{
char *name, *host;
size_t name_len, host_len;
long port = 8125;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|l",
&name, &name_len, &host, &host_len, &port) != SUCCESS) {
return;
}
unsigned int n = BLACKHOLE_G(metrics_initialized);
if (n == BLACKHOLE_METRICS_MAX) {
php_error_docref(NULL, E_WARNING, "metric %s not added: limit exceeded", name);
return;
}
BLACKHOLE_G(metrics_initialized)++;
blackhole_metric *metric = &BLACKHOLE_G(metrics)[n];
if (metric->name) {
efree(metric->name);
}
if (metric->host) {
efree(metric->host);
}
metric->name = estrndup(name, name_len);
metric->host = estrndup(host, host_len);
metric->port = port;
zend_hash_init(&metric->tags, 10, NULL, php_blackhole_tag_dtor, 0);
RETURN_TRUE;
}
static PHP_FUNCTION(blackhole_metric_set_tag) /* {{{ */
{
char *name, *tag, *value;
size_t name_len, tag_len, value_len;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss",
&name, &name_len, &tag, &tag_len, &value, &value_len) != SUCCESS) {
return;
}
blackhole_metric *metric = php_blackhole_find_metric(name);
if (metric == NULL) {
php_error_docref(NULL, E_WARNING, "metric not found");
RETURN_FALSE;
}
if (tag_len < 1) {
php_error_docref(NULL, E_WARNING, "tag name cannot be empty");
RETURN_FALSE;
}
if (strstr(tag, ":") != NULL) {
php_error_docref(NULL, E_WARNING, "tag name is not allowed to contain colons");
RETURN_FALSE;
}
if (strstr(value, ":") != NULL) {
php_error_docref(NULL, E_WARNING, "tag value is not allowed to contain colons");
RETURN_FALSE;
}
zend_hash_str_update_ptr(&metric->tags, tag, tag_len, estrndup(value, value_len));
RETURN_TRUE;
}
/* }}} */
/* {{{ proto array blackhole_get_request_duration()
Get request duration */
static PHP_FUNCTION(blackhole_get_request_duration)
{
if (zend_parse_parameters(ZEND_NUM_ARGS(), "") != SUCCESS) {
return;
}
RETURN_DOUBLE(php_blackhole_request_duration());
}
/* }}} */
/* {{{ proto array blackhole_get_request_started_at()
Get request start time */
static PHP_FUNCTION(blackhole_get_request_started_at)
{
if (zend_parse_parameters(ZEND_NUM_ARGS(), "") != SUCCESS) {
return;
}
RETURN_DOUBLE(php_blackhole_timeval_to_double(BLACKHOLE_G(request_started_at)));
}
/* }}} */
/* {{{ proto string blackhole_get_data()
*/
static PHP_FUNCTION(blackhole_get_data)
{
int request_duration_ms = (int) (php_blackhole_request_duration() * 1000);
if (zend_parse_parameters(ZEND_NUM_ARGS(), "") != SUCCESS) {
return;
}
array_init(return_value);
for (int i = 0; i < BLACKHOLE_G(metrics_initialized); i++) {
blackhole_metric metric = BLACKHOLE_G(metrics)[i];
char *data = php_blackhole_create_request_data(metric.name, &metric.tags, request_duration_ms);
char *key;
asprintf(&key, "%d#%s:%ld", i, metric.host, metric.port);
add_assoc_string_ex(return_value, key, strlen(key), data);
}
}
/* }}} */
/* {{{ arginfo */
// new API
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_metric_add, 0, 0, 3)
ZEND_ARG_INFO(0, metric_name)
ZEND_ARG_INFO(0, host)
ZEND_ARG_INFO(0, port)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_metric_set_tag, 0, 0, 3)
ZEND_ARG_INFO(0, metric_name)
ZEND_ARG_INFO(0, tag)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
// legacy API (stubs for BC)
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_get_host, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_set_host, 0, 0, 1)
ZEND_ARG_INFO(0, host)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_get_port, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_set_port, 0, 0, 1)
ZEND_ARG_INFO(0, port)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_get_metric_name, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_set_metric_name, 0, 0, 1)
ZEND_ARG_INFO(0, metric_name)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_get_overall_metric_name, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_set_overall_metric_name, 0, 0, 1)
ZEND_ARG_INFO(0, metric_name)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_get_tags, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_set_tag, 0, 0, 2)
ZEND_ARG_INFO(0, tag)
ZEND_ARG_INFO(0, value)
ZEND_END_ARG_INFO()
// internal API
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_get_request_duration, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_get_request_started_at, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_blackhole_get_data, 0, 0, 0)
ZEND_END_ARG_INFO()
/* }}} */
#define BLACKHOLE_FUNC(func) PHP_FE(func, arginfo_ ## func)
/* {{{ blackhole_functions[]
*/
zend_function_entry blackhole_functions[] = {
// legacy metrics API
BLACKHOLE_FUNC(blackhole_get_host)
BLACKHOLE_FUNC(blackhole_set_host)
BLACKHOLE_FUNC(blackhole_get_port)
BLACKHOLE_FUNC(blackhole_set_port)
BLACKHOLE_FUNC(blackhole_get_metric_name)
BLACKHOLE_FUNC(blackhole_set_metric_name)
BLACKHOLE_FUNC(blackhole_get_overall_metric_name)
BLACKHOLE_FUNC(blackhole_set_overall_metric_name)
BLACKHOLE_FUNC(blackhole_get_tags)
BLACKHOLE_FUNC(blackhole_set_tag)
// new metrics API
BLACKHOLE_FUNC(blackhole_metric_add)
BLACKHOLE_FUNC(blackhole_metric_set_tag)
BLACKHOLE_FUNC(blackhole_get_request_duration)
BLACKHOLE_FUNC(blackhole_get_request_started_at)
BLACKHOLE_FUNC(blackhole_get_data)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ php_blackhole_init_globals
*/
static void php_blackhole_init_globals(zend_blackhole_globals *globals)
{
memset(globals, 0, sizeof(*globals));
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
static PHP_MINIT_FUNCTION(blackhole)
{
ZEND_INIT_MODULE_GLOBALS(blackhole, php_blackhole_init_globals, NULL);
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
static PHP_RINIT_FUNCTION(blackhole)
{
struct timeval t;
BLACKHOLE_G(metrics_initialized) = 0;
if (gettimeofday(&t, NULL) == SUCCESS) {
(&BLACKHOLE_G(request_started_at))->tv_sec = t.tv_sec;
(&BLACKHOLE_G(request_started_at))->tv_usec = t.tv_usec;
} else {
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
static PHP_RSHUTDOWN_FUNCTION(blackhole)
{
int request_duration_ms = (int) (php_blackhole_request_duration() * 1000);
for (int i = 0; i < BLACKHOLE_G(metrics_initialized); i++) {
php_blackhole_send_metric(&BLACKHOLE_G(metrics)[i], request_duration_ms);
php_blackhole_free_metric(&BLACKHOLE_G(metrics)[i]);
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
static PHP_MINFO_FUNCTION(blackhole)
{
php_info_print_table_start();
php_info_print_table_header(2, "Blackhole support", "enabled");
php_info_print_table_row(2, "Blackhole extension version", PHP_BLACKHOLE_VERSION);
php_info_print_table_end();
}
/* }}} */
/* {{{ blackhole_module_entry
*/
zend_module_entry blackhole_module_entry = {
STANDARD_MODULE_HEADER,
"blackhole",
blackhole_functions,
PHP_MINIT(blackhole),
NULL,
PHP_RINIT(blackhole),
PHP_RSHUTDOWN(blackhole),
PHP_MINFO(blackhole),
PHP_BLACKHOLE_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/