fix statsd sampling rates calculations; fixes #3630
Costa Tsaousis (ktsaou) committed
Apr 23, 2018 at 22:15 UTC
bb17769f73d4fad7f8d4b00b3698365e4891054b
1 file changed
+26
-10
src/statsd.c
+26
-10
@@ -433,6 +433,13 @@ static inline LONG_DOUBLE statsd_parse_float(const char *v, LONG_DOUBLE def) {
433
return value;
434
}
435
436
+static inline LONG_DOUBLE statsd_parse_sampling_rate(const char *v) {
437
+ LONG_DOUBLE sampling_rate = statsd_parse_float(v, 1.0);
438
+ if(unlikely(isless(sampling_rate, 0.001))) sampling_rate = 0.001;
439
+ if(unlikely(isgreater(sampling_rate, 1.0))) sampling_rate = 1.0;
440
+ return sampling_rate;
441
+}
442
+
443
static inline long long statsd_parse_int(const char *v, long long def) {
444
long long value;
445
@@ -482,9 +489,9 @@ static inline void statsd_process_gauge(STATSD_METRIC *m, const char *value, con
489
}
490
else {
491
if (unlikely(*value == '+' || *value == '-'))
485
- m->gauge.value += statsd_parse_float(value, 1.0) / statsd_parse_float(sampling, 1.0);
492
+ m->gauge.value += statsd_parse_float(value, 1.0) / statsd_parse_sampling_rate(sampling);
493
else
487
- m->gauge.value = statsd_parse_float(value, 1.0) / statsd_parse_float(sampling, 1.0);
494
+ m->gauge.value = statsd_parse_float(value, 1.0);
495
496
m->events++;
497
m->count++;
@@ -502,7 +509,7 @@ static inline void statsd_process_counter_or_meter(STATSD_METRIC *m, const char
509
// magic loading of metric, without affecting anything
510
}
511
else {
505
- m->counter.value += llrintl((LONG_DOUBLE) statsd_parse_int(value, 1) / statsd_parse_float(sampling, 1.0));
512
+ m->counter.value += llrintl((LONG_DOUBLE) statsd_parse_int(value, 1) / statsd_parse_sampling_rate(sampling));
513
514
m->events++;
515
m->count++;
@@ -529,14 +536,23 @@ static inline void statsd_process_histogram_or_timer(STATSD_METRIC *m, const cha
536
// magic loading of metric, without affecting anything
537
}
538
else {
532
- if (unlikely(m->histogram.ext->used == m->histogram.ext->size)) {
533
- netdata_mutex_lock(&m->histogram.ext->mutex);
534
- m->histogram.ext->size += statsd.histogram_increase_step;
535
- m->histogram.ext->values = reallocz(m->histogram.ext->values, sizeof(LONG_DOUBLE) * m->histogram.ext->size);
536
- netdata_mutex_unlock(&m->histogram.ext->mutex);
537
- }
539
+ LONG_DOUBLE v = statsd_parse_float(value, 1.0);
540
+ LONG_DOUBLE sampling_rate = statsd_parse_sampling_rate(sampling);
541
+ if(unlikely(isless(sampling_rate, 0.01))) sampling_rate = 0.01;
542
+ if(unlikely(isgreater(sampling_rate, 1.0))) sampling_rate = 1.0;
543
+
544
+ long long samples = llrintl(1.0 / sampling_rate);
545
+ while(samples-- > 0) {
546
+
547
+ if(unlikely(m->histogram.ext->used == m->histogram.ext->size)) {
548
+ netdata_mutex_lock(&m->histogram.ext->mutex);
549
+ m->histogram.ext->size += statsd.histogram_increase_step;
550
+ m->histogram.ext->values = reallocz(m->histogram.ext->values, sizeof(LONG_DOUBLE) * m->histogram.ext->size);
551
+ netdata_mutex_unlock(&m->histogram.ext->mutex);
552
+ }
553
539
- m->histogram.ext->values[m->histogram.ext->used++] = statsd_parse_float(value, 1.0) / statsd_parse_float(sampling, 1.0);
554
+ m->histogram.ext->values[m->histogram.ext->used++] = v;
555
+ }
556
557
m->events++;
558
m->count++;