Facets: fixes 5 (#15976)
Costa Tsaousis committed
Sep 15, 2023 at 14:51 UTC
717f258c307599ca5955fd34753dbc0430966143
2 files changed
+44
-1
collectors/systemd-journal.plugin/systemd-journal.c
+42
@@ -600,6 +600,40 @@ static void netdata_systemd_journal_rich_message(FACETS *facets __maybe_unused,
600
}
601
602
static void function_systemd_journal(const char *transaction, char *function, char *line_buffer __maybe_unused, int line_max __maybe_unused, int timeout __maybe_unused) {
603
+ static struct {
604
+ BUFFER *tmp;
605
+ BUFFER *wb;
606
+ BUFFER *function;
607
+ int response;
608
+ time_t expires;
609
+ } cache = {
610
+ .tmp = NULL,
611
+ .wb = NULL,
612
+ .function = NULL,
613
+ .response = 0,
614
+ .expires = 0,
615
+ };
616
+
617
+ if(unlikely(!cache.wb)) {
618
+ cache.tmp = buffer_create(0, NULL);
619
+ cache.wb = buffer_create(0, NULL);
620
+ cache.function = buffer_create(0, NULL);
621
+ }
622
+
623
+ if(buffer_strlen(cache.function) && buffer_strlen(cache.wb) && strcmp(buffer_tostring(cache.function), function) == 0) {
624
+ // repeated the same request
625
+ netdata_mutex_lock(&stdout_mutex);
626
+ if(cache.response == HTTP_RESP_OK)
627
+ pluginsd_function_result_to_stdout(transaction, cache.response, "application/json", cache.expires, cache.wb);
628
+ else
629
+ pluginsd_function_json_error_to_stdout(transaction, cache.response, "failed");
630
+ netdata_mutex_unlock(&stdout_mutex);
631
+ return;
632
+ }
633
+
634
+ buffer_flush(cache.tmp);
635
+ buffer_strcat(cache.tmp, function);
636
+
637
BUFFER *wb = buffer_create(0, NULL);
638
buffer_flush(wb);
639
buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_NEWLINE_ON_ARRAY_ITEMS);
@@ -838,6 +872,14 @@ static void function_systemd_journal(const char *transaction, char *function, ch
872
goto cleanup;
873
}
874
875
+ // keep this response in the cache
876
+ cache.response = response;
877
+ cache.expires = expires;
878
+ buffer_flush(cache.wb);
879
+ buffer_fast_strcat(cache.wb, buffer_tostring(wb), buffer_strlen(wb));
880
+ buffer_flush(cache.function);
881
+ buffer_fast_strcat(cache.function, buffer_tostring(cache.tmp), buffer_strlen(cache.tmp));
882
+
883
output:
884
netdata_mutex_lock(&stdout_mutex);
885
pluginsd_function_result_to_stdout(transaction, response, "application/json", expires, wb);
libnetdata/facets/facets.c
+2
-1
@@ -187,7 +187,7 @@ static usec_t calculate_histogram_bar_width(usec_t after_ut, usec_t before_ut) {
187
static int array_size = sizeof(valid_durations_s) / sizeof(valid_durations_s[0]);
188
189
usec_t duration_ut = before_ut - after_ut;
190
- usec_t bar_width_ut = 1 * 60 * USEC_PER_SEC;
190
+ usec_t bar_width_ut = 1 * USEC_PER_SEC;
191
192
for (int i = array_size - 1; i >= 0; --i) {
193
if (duration_ut / (valid_durations_s[i] * USEC_PER_SEC) >= HISTOGRAM_COLUMNS) {
@@ -1456,6 +1456,7 @@ void facets_report(FACETS *facets, BUFFER *wb) {
1456
buffer_json_member_add_boolean(wb, "enabled", true);
1457
buffer_json_member_add_string(wb, "key", "anchor");
1458
buffer_json_member_add_string(wb, "column", "timestamp");
1459
+ buffer_json_member_add_string(wb, "units", "timestamp_usec");
1460
}
1461
buffer_json_object_close(wb); // pagination
1462