forked from zebrafishlabs/nginx-statsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngx_http_statsd.c
773 lines (622 loc) · 20.4 KB
/
ngx_http_statsd.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
/*
* nginx-statsd module
* Copyright (C) 2012 Zebrafish Labs Inc.
*
* Much of this source code was derived from nginx-udplog-module which
* has the following copyright. Please refer to the LICENSE file for
* details.
*
* Copyright (C) 2010 Valery Kholodkov
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
#define STATSD_DEFAULT_PORT 8125
#define STATSD_TYPE_COUNTER 0x0001
#define STATSD_TYPE_TIMING 0x0002
#define ngx_conf_merge_ptr_value(conf, prev, default) \
if (conf == NGX_CONF_UNSET_PTR) { \
conf = (prev == NGX_CONF_UNSET_PTR) ? default : prev; \
}
#if defined nginx_version && nginx_version >= 8021
typedef ngx_addr_t ngx_statsd_addr_t;
#else
typedef ngx_peer_addr_t ngx_statsd_addr_t;
#endif
typedef struct {
ngx_statsd_addr_t peer_addr;
ngx_udp_connection_t *udp_connection;
} ngx_udp_endpoint_t;
typedef struct {
ngx_array_t *endpoints;
} ngx_http_statsd_main_conf_t;
typedef struct {
ngx_uint_t type;
ngx_str_t key;
ngx_uint_t metric;
ngx_flag_t valid;
ngx_http_complex_value_t *ckey;
ngx_http_complex_value_t *cmetric;
ngx_http_complex_value_t *cvalid;
} ngx_statsd_stat_t;
typedef struct {
int off;
ngx_udp_endpoint_t *endpoint;
ngx_uint_t sample_rate;
ngx_array_t *stats;
} ngx_http_statsd_conf_t;
ngx_int_t ngx_udp_connect(ngx_udp_connection_t *uc);
static void ngx_statsd_updater_cleanup(void *data);
static ngx_int_t ngx_http_statsd_udp_send(ngx_udp_endpoint_t *l, u_char *buf, size_t len);
static void *ngx_http_statsd_create_main_conf(ngx_conf_t *cf);
static void *ngx_http_statsd_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_statsd_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static char *ngx_http_statsd_set_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_statsd_add_stat(ngx_conf_t *cf, ngx_command_t *cmd, void *conf, ngx_uint_t type);
static char *ngx_http_statsd_add_count(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_statsd_add_timing(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_str_t ngx_http_statsd_key_get_value(ngx_http_request_t *r, ngx_http_complex_value_t *cv, ngx_str_t v);
static ngx_str_t ngx_http_statsd_key_value(ngx_str_t *str);
static ngx_uint_t ngx_http_statsd_metric_get_value(ngx_http_request_t *r, ngx_http_complex_value_t *cv, ngx_uint_t v);
static ngx_uint_t ngx_http_statsd_metric_value(ngx_str_t *str);
static ngx_flag_t ngx_http_statsd_valid_get_value(ngx_http_request_t *r, ngx_http_complex_value_t *cv, ngx_flag_t v);
static ngx_flag_t ngx_http_statsd_valid_value(ngx_str_t *str);
uintptr_t ngx_escape_statsd_key(u_char *dst, u_char *src, size_t size);
static ngx_int_t ngx_http_statsd_init(ngx_conf_t *cf);
static ngx_command_t ngx_http_statsd_commands[] = {
{ ngx_string("statsd_server"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_statsd_set_server,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("statsd_sample_rate"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_statsd_conf_t, sample_rate),
NULL },
{ ngx_string("statsd_count"),
NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE23,
ngx_http_statsd_add_count,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("statsd_timing"),
NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE23,
ngx_http_statsd_add_timing,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_statsd_module_ctx = {
NULL, /* preconfiguration */
ngx_http_statsd_init, /* postconfiguration */
ngx_http_statsd_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_statsd_create_loc_conf, /* create location configration */
ngx_http_statsd_merge_loc_conf /* merge location configration */
};
ngx_module_t ngx_http_statsd_module = {
NGX_MODULE_V1,
&ngx_http_statsd_module_ctx, /* module context */
ngx_http_statsd_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_str_t
ngx_http_statsd_key_get_value(ngx_http_request_t *r, ngx_http_complex_value_t *cv, ngx_str_t v)
{
ngx_str_t val;
if (cv == NULL) {
return v;
}
if (ngx_http_complex_value(r, cv, &val) != NGX_OK) {
return (ngx_str_t) ngx_null_string;
};
return ngx_http_statsd_key_value(&val);
};
static ngx_str_t
ngx_http_statsd_key_value(ngx_str_t *value)
{
return *value;
};
static ngx_uint_t
ngx_http_statsd_metric_get_value(ngx_http_request_t *r, ngx_http_complex_value_t *cv, ngx_uint_t v)
{
ngx_str_t val;
if (cv == NULL) {
return v;
}
if (ngx_http_complex_value(r, cv, &val) != NGX_OK) {
return 0;
};
return ngx_http_statsd_metric_value(&val);
};
static ngx_uint_t
ngx_http_statsd_metric_value(ngx_str_t *value)
{
ngx_int_t n, m;
if (value->len == 1 && value->data[0] == '-') {
return (ngx_uint_t) -1;
};
/* Hack to convert milliseconds to a number. */
if (value->len > 4 && value->data[value->len - 4] == '.') {
n = ngx_atoi(value->data, value->len - 4);
m = ngx_atoi(value->data + (value->len - 3), 3);
return (ngx_uint_t) ((n * 1000) + m);
} else {
n = ngx_atoi(value->data, value->len);
if (n > 0) {
return (ngx_uint_t) n;
};
};
return 0;
};
static ngx_flag_t
ngx_http_statsd_valid_get_value(ngx_http_request_t *r, ngx_http_complex_value_t *cv, ngx_flag_t v)
{
ngx_str_t val;
if (cv == NULL) {
return v;
}
if (ngx_http_complex_value(r, cv, &val) != NGX_OK) {
return 0;
};
return ngx_http_statsd_valid_value(&val);
};
static ngx_flag_t
ngx_http_statsd_valid_value(ngx_str_t *value)
{
return (ngx_flag_t) (value->len > 0 ? 1 : 0);
};
ngx_int_t
ngx_http_statsd_handler(ngx_http_request_t *r)
{
u_char *line, *p;
uint line_len;
size_t len;
ngx_http_statsd_conf_t *ulcf;
ngx_statsd_stat_t *stats;
ngx_statsd_stat_t stat;
ngx_uint_t c;
ngx_uint_t n;
ngx_str_t s;
ngx_flag_t b;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http statsd handler");
ulcf = ngx_http_get_module_loc_conf(r, ngx_http_statsd_module);
if (ulcf->off == 1 || ulcf->endpoint == NULL) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "statsd: handler off");
return NGX_OK;
}
// Use a random distribution to sample at sample rate.
if (ulcf->sample_rate < 100 && (uint) (ngx_random() % 100) >= ulcf->sample_rate) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "statsd: skipping sample");
return NGX_OK;
}
line_len = 100;
#if defined nginx_version && nginx_version >= 7003
line = ngx_pnalloc(r->pool, line_len);
#else
line = ngx_palloc(r->pool, line_len);
#endif
if (line == NULL) {
return NGX_ERROR;
}
stats = ulcf->stats->elts;
for (c = 0; c < ulcf->stats->nelts; c++) {
stat = stats[c];
s = ngx_http_statsd_key_get_value(r, stat.ckey, stat.key);
ngx_escape_statsd_key(s.data, s.data, s.len);
n = ngx_http_statsd_metric_get_value(r, stat.cmetric, stat.metric);
b = ngx_http_statsd_valid_get_value(r, stat.cvalid, stat.valid);
if (b == 0 || s.len == 0 || n <= 0) {
// Do not log if not valid, key is invalid, or valud is lte 0.
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "statsd: no value to send");
continue;
};
len = s.len;
len += sizeof(":") - 1;
len += NGX_INT_T_LEN;
len += sizeof("|[email protected]") - 1;
if (line_len < len) {
// Redimension buffer.
line_len = len;
#if defined nginx_version && nginx_version >= 7003
line = ngx_pnalloc(r->pool, line_len);
#else
line = ngx_palloc(r->pool, line_len);
#endif
if (line == NULL) {
return NGX_ERROR;
};
};
if (stat.type == STATSD_TYPE_COUNTER) {
if (ulcf->sample_rate < 100) {
p = ngx_sprintf(line, "%V:%d|c@0.%02d", &s, n, ulcf->sample_rate);
} else {
p = ngx_sprintf(line, "%V:%d|c", &s, n);
}
ngx_http_statsd_udp_send(ulcf->endpoint, line, p - line);
} else if (stat.type == STATSD_TYPE_TIMING) {
p = ngx_sprintf(line, "%V:%d|ms", &s, n);
ngx_http_statsd_udp_send(ulcf->endpoint, line, p - line);
}
}
return NGX_OK;
}
static ngx_int_t ngx_statsd_init_endpoint(ngx_conf_t *cf, ngx_udp_endpoint_t *endpoint) {
ngx_pool_cleanup_t *cln;
ngx_udp_connection_t *uc;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
"statsd: initting endpoint");
cln = ngx_pool_cleanup_add(cf->pool, 0);
if(cln == NULL) {
return NGX_ERROR;
}
cln->handler = ngx_statsd_updater_cleanup;
cln->data = endpoint;
uc = ngx_calloc(sizeof(ngx_udp_connection_t), cf->log);
if (uc == NULL) {
return NGX_ERROR;
}
endpoint->udp_connection = uc;
uc->sockaddr = endpoint->peer_addr.sockaddr;
uc->socklen = endpoint->peer_addr.socklen;
uc->server = endpoint->peer_addr.name;
#if defined nginx_version && ( nginx_version >= 7054 && nginx_version < 8032 )
uc->log = &cf->cycle->new_log;
#else
uc->log = cf->cycle->new_log;
#if defined nginx_version && nginx_version >= 8032
uc->log.handler = NULL;
uc->log.data = NULL;
uc->log.action = "logging";
#endif
#endif
return NGX_OK;
}
static void
ngx_statsd_updater_cleanup(void *data)
{
ngx_udp_endpoint_t *e = data;
ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
"cleanup statsd_updater");
if(e->udp_connection) {
if(e->udp_connection->connection) {
ngx_close_connection(e->udp_connection->connection);
}
ngx_free(e->udp_connection);
}
}
static void ngx_http_statsd_udp_dummy_handler(ngx_event_t *ev)
{
}
static ngx_int_t
ngx_http_statsd_udp_send(ngx_udp_endpoint_t *l, u_char *buf, size_t len)
{
ssize_t n;
ngx_udp_connection_t *uc;
uc = l->udp_connection;
if (uc->connection == NULL) {
if(ngx_udp_connect(uc) != NGX_OK) {
if(uc->connection != NULL) {
ngx_free_connection(uc->connection);
uc->connection = NULL;
}
return NGX_ERROR;
}
uc->connection->data = l;
uc->connection->read->handler = ngx_http_statsd_udp_dummy_handler;
uc->connection->read->resolver = 0;
}
n = ngx_send(uc->connection, buf, len);
if (n == -1) {
return NGX_ERROR;
}
if ((size_t) n != (size_t) len) {
#if defined nginx_version && nginx_version >= 8032
ngx_log_error(NGX_LOG_CRIT, &uc->log, 0, "send() incomplete");
#else
ngx_log_error(NGX_LOG_CRIT, uc->log, 0, "send() incomplete");
#endif
return NGX_ERROR;
}
return NGX_OK;
}
static void *
ngx_http_statsd_create_main_conf(ngx_conf_t *cf)
{
ngx_http_statsd_main_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_statsd_main_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
return conf;
}
static void *
ngx_http_statsd_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_statsd_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_statsd_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->endpoint = NGX_CONF_UNSET_PTR;
conf->off = NGX_CONF_UNSET;
conf->sample_rate = NGX_CONF_UNSET_UINT;
conf->stats = NULL;
return conf;
}
static char *
ngx_http_statsd_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_statsd_conf_t *prev = parent;
ngx_http_statsd_conf_t *conf = child;
ngx_statsd_stat_t *stat;
ngx_statsd_stat_t prev_stat;
ngx_statsd_stat_t *prev_stats;
ngx_uint_t i;
ngx_uint_t sz;
ngx_conf_merge_ptr_value(conf->endpoint, prev->endpoint, NULL);
ngx_conf_merge_off_value(conf->off, prev->off, 1);
ngx_conf_merge_uint_value(conf->sample_rate, prev->sample_rate, 100);
if (conf->stats == NULL) {
sz = (prev->stats != NULL ? prev->stats->nelts : 2);
conf->stats = ngx_array_create(cf->pool, sz, sizeof(ngx_statsd_stat_t));
if (conf->stats == NULL) {
return NGX_CONF_ERROR;
}
}
if (prev->stats != NULL) {
prev_stats = prev->stats->elts;
for (i = 0; i < prev->stats->nelts; i++) {
stat = ngx_array_push(conf->stats);
ngx_memzero(stat, sizeof(ngx_statsd_stat_t));
prev_stat = prev_stats[i];
stat->type = prev_stat.type;
stat->key = prev_stat.key;
stat->metric = prev_stat.metric;
stat->ckey = prev_stat.ckey;
stat->cmetric = prev_stat.cmetric;
stat->valid = prev_stat.valid;
stat->cvalid = prev_stat.cvalid;
};
};
return NGX_CONF_OK;
}
static ngx_udp_endpoint_t *
ngx_http_statsd_add_endpoint(ngx_conf_t *cf, ngx_statsd_addr_t *peer_addr)
{
ngx_http_statsd_main_conf_t *umcf;
ngx_udp_endpoint_t *endpoint;
umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_statsd_module);
if(umcf->endpoints == NULL) {
umcf->endpoints = ngx_array_create(cf->pool, 2, sizeof(ngx_udp_endpoint_t));
if (umcf->endpoints == NULL) {
return NULL;
}
}
endpoint = ngx_array_push(umcf->endpoints);
if (endpoint == NULL) {
return NULL;
}
endpoint->peer_addr = *peer_addr;
return endpoint;
}
static char *
ngx_http_statsd_set_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_statsd_conf_t *ulcf = conf;
ngx_str_t *value;
ngx_url_t u;
value = cf->args->elts;
if (ngx_strcmp(value[1].data, "off") == 0) {
ulcf->off = 1;
return NGX_CONF_OK;
}
ulcf->off = 0;
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
u.default_port = STATSD_DEFAULT_PORT;
u.no_resolve = 0;
if(ngx_parse_url(cf->pool, &u) != NGX_OK) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V: %s", &u.host, u.err);
return NGX_CONF_ERROR;
}
ulcf->endpoint = ngx_http_statsd_add_endpoint(cf, &u.addrs[0]);
if(ulcf->endpoint == NULL) {
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static char *
ngx_http_statsd_add_stat(ngx_conf_t *cf, ngx_command_t *cmd, void *conf, ngx_uint_t type) {
ngx_http_statsd_conf_t *ulcf = conf;
ngx_http_complex_value_t key_cv;
ngx_http_compile_complex_value_t key_ccv;
ngx_http_complex_value_t metric_cv;
ngx_http_compile_complex_value_t metric_ccv;
ngx_http_complex_value_t valid_cv;
ngx_http_compile_complex_value_t valid_ccv;
ngx_str_t *value;
ngx_statsd_stat_t *stat;
ngx_int_t n;
ngx_str_t s;
ngx_flag_t b;
value = cf->args->elts;
if (ulcf->stats == NULL) {
ulcf->stats = ngx_array_create(cf->pool, 10, sizeof(ngx_statsd_stat_t));
if (ulcf->stats == NULL) {
return NGX_CONF_ERROR;
}
}
stat = ngx_array_push(ulcf->stats);
if (stat == NULL) {
return NGX_CONF_ERROR;
}
ngx_memzero(stat, sizeof(ngx_statsd_stat_t));
stat->type = type;
stat->valid = 1;
ngx_memzero(&key_ccv, sizeof(ngx_http_compile_complex_value_t));
key_ccv.cf = cf;
key_ccv.value = &value[1];
key_ccv.complex_value = &key_cv;
if (ngx_http_compile_complex_value(&key_ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
if (key_cv.lengths == NULL) {
s = ngx_http_statsd_key_value(&value[1]);
/*if (n < 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", &value[2]);
return NGX_CONF_ERROR;
};*/
stat->key = (ngx_str_t) s;
} else {
stat->ckey = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
if (stat->ckey == NULL) {
return NGX_CONF_ERROR;
}
*stat->ckey = key_cv;
}
ngx_memzero(&metric_ccv, sizeof(ngx_http_compile_complex_value_t));
metric_ccv.cf = cf;
metric_ccv.value = &value[2];
metric_ccv.complex_value = &metric_cv;
if (ngx_http_compile_complex_value(&metric_ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
if (metric_cv.lengths == NULL) {
n = ngx_http_statsd_metric_value(&value[2]);
if (n < 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", &value[2]);
return NGX_CONF_ERROR;
};
stat->metric = (ngx_uint_t) n;
} else {
stat->cmetric = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
if (stat->cmetric == NULL) {
return NGX_CONF_ERROR;
}
*stat->cmetric = metric_cv;
}
if (cf->args->nelts > 3) {
ngx_memzero(&valid_ccv, sizeof(ngx_http_compile_complex_value_t));
valid_ccv.cf = cf;
valid_ccv.value = &value[3];
valid_ccv.complex_value = &valid_cv;
if (ngx_http_compile_complex_value(&valid_ccv) != NGX_OK) {
return NGX_CONF_ERROR;
}
if (valid_cv.lengths == NULL) {
b = ngx_http_statsd_valid_value(&value[3]);
if (b < 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid parameter \"%V\"", &value[3]);
return NGX_CONF_ERROR;
};
stat->valid = (ngx_flag_t) b;
} else {
stat->cvalid = ngx_palloc(cf->pool, sizeof(ngx_http_complex_value_t));
if (stat->cvalid == NULL) {
return NGX_CONF_ERROR;
}
*stat->cvalid = valid_cv;
}
}
return NGX_CONF_OK;
}
static char *
ngx_http_statsd_add_count(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
return ngx_http_statsd_add_stat(cf, cmd, conf, STATSD_TYPE_COUNTER);
}
static char *
ngx_http_statsd_add_timing(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
return ngx_http_statsd_add_stat(cf, cmd, conf, STATSD_TYPE_TIMING);
}
static ngx_int_t
ngx_http_statsd_init(ngx_conf_t *cf)
{
ngx_int_t rc;
ngx_uint_t i;
ngx_http_core_main_conf_t *cmcf;
ngx_http_statsd_main_conf_t *umcf;
ngx_http_handler_pt *h;
ngx_udp_endpoint_t *e;
umcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_statsd_module);
if(umcf->endpoints != NULL) {
e = umcf->endpoints->elts;
for(i = 0;i < umcf->endpoints->nelts;i++) {
rc = ngx_statsd_init_endpoint(cf, e + i);
if(rc != NGX_OK) {
return NGX_ERROR;
}
}
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_statsd_handler;
}
return NGX_OK;
}
uintptr_t
ngx_escape_statsd_key(u_char *dst, u_char *src, size_t size)
{
ngx_uint_t n;
uint32_t *escape;
/* " ", "#", """, "%", "'", %00-%1F, %7F-%FF */
static uint32_t statsd_key[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0xfc00bfff, /* 1111 1100 0000 0000 1011 1111 1111 1111 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x78000001, /* 0111 1000 0000 0000 0000 0000 0000 0001 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0xf8000001, /* 1111 1000 0000 0000 0000 0000 0000 0001 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */
};
static uint32_t *map[] =
{ statsd_key };
escape = map[0];
if (dst == NULL) {
/* find the number of the characters to be escaped */
n = 0;
while (size) {
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
n++;
}
src++;
size--;
}
return (uintptr_t) n;
}
while (size) {
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
*dst++ = '_';
src++;
} else {
*dst++ = *src++;
}
size--;
}
return (uintptr_t) dst;
}