added statsd performance charts
Costa Tsaousis (ktsaou) committed
Apr 23, 2017 at 22:40 UTC
75736878da88e905047eac52154cc712c20575a6
1 file changed
+141
-3
src/statsd.c
+141
-3
@@ -1,6 +1,23 @@
1
#include "common.h"
2
3
-#define STATSD_MAX_METRIC_LENGTH 200
3
+static struct statsd {
4
+ size_t events;
5
+ size_t events_gauge;
6
+ size_t events_counter;
7
+ size_t events_timer;
8
+ size_t events_meter;
9
+ size_t events_histogram;
10
+
11
+ size_t metrics;
12
+ size_t metrics_gauge;
13
+ size_t metrics_counter;
14
+ size_t metrics_timer;
15
+ size_t metrics_meter;
16
+ size_t metrics_histogram;
17
+} statsd = {
18
+ .events = 0,
19
+ .metrics = 0
20
+};
21
22
// --------------------------------------------------------------------------------------
23
@@ -83,6 +100,8 @@ static inline STATSD_METRIC *stasd_metric_index_find(const char *name, uint32_t
100
static inline void statsd_collected_value(STATSD_METRIC *m, calculated_number value, calculated_number sample_rate, uint32_t options) {
101
debug(D_STATSD, "Updating metric '%s'", m->name);
102
103
+ statsd.events++;
104
+
105
m->last_collected_ut = now_realtime_usec();
106
m->events++;
107
m->count += sample_rate;
@@ -95,21 +114,25 @@ static inline void statsd_collected_value(STATSD_METRIC *m, calculated_number va
114
115
switch(m->type) {
116
case STATSD_METRIC_TYPE_HISTOGRAM:
117
+ statsd.events_histogram++;
118
// FIXME: not implemented yet
119
m->value += value;
120
break;
121
122
case STATSD_METRIC_TYPE_METER:
123
+ statsd.events_meter++;
124
// we add to this metric
125
m->value += value;
126
break;
127
128
case STATSD_METRIC_TYPE_TIMER:
129
+ statsd.events_timer++;
130
// we add time to this metric
131
m->value += value;
132
break;
133
134
case STATSD_METRIC_TYPE_GAUGE:
135
+ statsd.events_gauge++;
136
if(unlikely(options & STATSD_GAUGE_COLLECTION_RELATIVE))
137
// we add the collected value
138
m->value += value;
@@ -119,6 +142,7 @@ static inline void statsd_collected_value(STATSD_METRIC *m, calculated_number va
142
break;
143
144
case STATSD_METRIC_TYPE_COUNTER:
145
+ statsd.events_counter++;
146
// we add the collected value
147
m->value += value;
148
break;
@@ -151,6 +175,28 @@ static inline void statsd_process_metric(const char *metric, STATSD_METRIC_TYPE
175
m->hash = hash;
176
m->type = type;
177
m = (STATSD_METRIC *)avl_insert(&statsd_index, (avl *)m);
178
+
179
+ statsd.metrics++;
180
+ switch(type) {
181
+ case STATSD_METRIC_TYPE_COUNTER:
182
+ statsd.metrics_counter++;
183
+ break;
184
+
185
+ case STATSD_METRIC_TYPE_GAUGE:
186
+ statsd.metrics_gauge++;
187
+ break;
188
+
189
+ case STATSD_METRIC_TYPE_METER:
190
+ statsd.metrics_meter++;
191
+ break;
192
+
193
+ case STATSD_METRIC_TYPE_HISTOGRAM:
194
+ statsd.metrics_histogram++;
195
+ break;
196
+
197
+ case STATSD_METRIC_TYPE_TIMER:
198
+ statsd.metrics_timer++;
199
+ }
200
}
201
202
statsd_collected_value(m, value, sample_rate, options);
@@ -398,12 +444,94 @@ static int statsd_rcv_callback(int fd, int socktype, void *data, short int *even
444
}
445
446
447
+// --------------------------------------------------------------------------------------------------------------------
448
+// statsd child thread to update netdata
449
+
450
+void *statsd_child_thread(void *ptr) {
451
+ info("STATSD thread created with task id %d", gettid());
452
+
453
+ if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
454
+ error("Cannot set pthread cancel type to DEFERRED.");
455
+
456
+ if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
457
+ error("Cannot set pthread cancel state to ENABLE.");
458
+
459
+
460
+ RRDSET *st_metrics = rrdset_create_localhost(
461
+ "netdata"
462
+ , "statsd_metrics"
463
+ , NULL
464
+ , "statsd"
465
+ , NULL
466
+ , "Metrics in the netdata statsd database"
467
+ , "metrics"
468
+ , 132000
469
+ , localhost->rrd_update_every
470
+ , RRDSET_TYPE_STACKED
471
+ );
472
+ RRDDIM *rd_metrics_gauge = rrddim_add(st_metrics, "gauge", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
473
+ RRDDIM *rd_metrics_counter = rrddim_add(st_metrics, "counter", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
474
+ RRDDIM *rd_metrics_timer = rrddim_add(st_metrics, "timer", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
475
+ RRDDIM *rd_metrics_meter = rrddim_add(st_metrics, "meter", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
476
+ RRDDIM *rd_metrics_histogram = rrddim_add(st_metrics, "histogram", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
477
+
478
+ RRDSET *st_events = rrdset_create_localhost(
479
+ "netdata"
480
+ , "statsd_events"
481
+ , NULL
482
+ , "statsd"
483
+ , NULL
484
+ , "Events processed by the netdata statsd server"
485
+ , "events/s"
486
+ , 132001
487
+ , localhost->rrd_update_every
488
+ , RRDSET_TYPE_STACKED
489
+ );
490
+ RRDDIM *rd_events_gauge = rrddim_add(st_events, "gauge", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
491
+ RRDDIM *rd_events_counter = rrddim_add(st_events, "counter", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
492
+ RRDDIM *rd_events_timer = rrddim_add(st_events, "timer", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
493
+ RRDDIM *rd_events_meter = rrddim_add(st_events, "meter", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
494
+ RRDDIM *rd_events_histogram = rrddim_add(st_events, "histogram", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
495
+
496
+ usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
497
+ heartbeat_t hb;
498
+ heartbeat_init(&hb);
499
+ for(;;) {
500
+ usec_t hb_dt = heartbeat_next(&hb, step);
501
+
502
+ if(unlikely(netdata_exit))
503
+ break;
504
+
505
+ if(hb_dt) {
506
+ rrdset_next(st_metrics);
507
+ rrdset_next(st_events);
508
+ }
509
+
510
+ rrddim_set_by_pointer(st_metrics, rd_metrics_gauge, (collected_number)statsd.metrics_gauge);
511
+ rrddim_set_by_pointer(st_metrics, rd_metrics_counter, (collected_number)statsd.metrics_counter);
512
+ rrddim_set_by_pointer(st_metrics, rd_metrics_timer, (collected_number)statsd.metrics_timer);
513
+ rrddim_set_by_pointer(st_metrics, rd_metrics_meter, (collected_number)statsd.metrics_meter);
514
+ rrddim_set_by_pointer(st_metrics, rd_metrics_histogram, (collected_number)statsd.metrics_histogram);
515
+
516
+ rrddim_set_by_pointer(st_events, rd_events_gauge, (collected_number)statsd.events_gauge);
517
+ rrddim_set_by_pointer(st_events, rd_events_counter, (collected_number)statsd.events_counter);
518
+ rrddim_set_by_pointer(st_events, rd_events_timer, (collected_number)statsd.events_timer);
519
+ rrddim_set_by_pointer(st_events, rd_events_meter, (collected_number)statsd.events_meter);
520
+ rrddim_set_by_pointer(st_events, rd_events_histogram, (collected_number)statsd.events_histogram);
521
+
522
+ rrdset_done(st_metrics);
523
+ rrdset_done(st_events);
524
+ }
525
+
526
+ pthread_exit(NULL);
527
+ return NULL;
528
+}
529
+
530
+
531
// --------------------------------------------------------------------------------------------------------------------
532
// statsd main thread
533
534
void *statsd_main(void *ptr) {
405
- struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
406
-
535
info("STATSD thread created with task id %d", gettid());
536
537
if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
@@ -418,6 +546,14 @@ void *statsd_main(void *ptr) {
546
goto cleanup;
547
}
548
549
+ pthread_t thread;
550
+
551
+ if(pthread_create(&thread, NULL, statsd_child_thread, (void *)NULL))
552
+ error("STATSD: failed to create child thread.");
553
+
554
+ else if(pthread_detach(thread))
555
+ error("STATSD: cannot request detach of child thread.");
556
+
557
poll_events(&statsd_sockets
558
, statsd_add_callback
559
, statsd_del_callback
@@ -425,6 +561,8 @@ void *statsd_main(void *ptr) {
561
);
562
563
cleanup:
564
+ pthread_cancel(thread);
565
+
566
debug(D_WEB_CLIENT, "STATSD: exit!");
567
listen_sockets_close(&statsd_sockets);
568