port useful code from incomplete PRs (#16863)
* port useful code from incomplete PRs * change systemd-journal plugin default thread name
Costa Tsaousis committed
Jan 29, 2024 at 11:08 UTC
3be077bb2a5f28e259a190696aeed76015269379
9 files changed
+86
-32
CMakeLists.txt
+1
@@ -677,6 +677,7 @@ set(LIBNETDATA_FILES
677
libnetdata/config/dyncfg.c
678
libnetdata/config/dyncfg.h
679
libnetdata/json/json-c-parser-inline.h
680
+ libnetdata/template-enum.h
681
)
682
683
if(ENABLE_PLUGIN_EBPF)
collectors/systemd-journal.plugin/systemd-main.c
+1
-1
@@ -19,7 +19,7 @@ static bool journal_data_direcories_exist() {
19
20
int main(int argc __maybe_unused, char **argv __maybe_unused) {
21
clocks_init();
22
- netdata_thread_set_tag("SDMAIN");
22
+ netdata_thread_set_tag("sd-jrnl.plugin");
23
nd_log_initialize_for_external_plugins("systemd-journal.plugin");
24
25
netdata_configured_host_prefix = getenv("NETDATA_HOST_PREFIX");
libnetdata/http/http_defs.c
+13
-25
@@ -2,33 +2,21 @@
2
3
#include "../libnetdata.h"
4
5
-const char *http_request_method2string(HTTP_REQUEST_MODE mode) {
6
- switch(mode) {
7
- case HTTP_REQUEST_MODE_OPTIONS:
8
- return "OPTIONS";
5
+ENUM_STR_MAP_DEFINE(HTTP_REQUEST_MODE) =
6
+{
7
+ { .name = "OPTIONS", .id = HTTP_REQUEST_MODE_OPTIONS },
8
+ { .name = "GET", .id = HTTP_REQUEST_MODE_GET },
9
+ { .name = "FILECOPY", .id = HTTP_REQUEST_MODE_FILECOPY },
10
+ { .name = "POST", .id = HTTP_REQUEST_MODE_POST },
11
+ { .name = "PUT", .id = HTTP_REQUEST_MODE_PUT },
12
+ { .name = "DELETE", .id = HTTP_REQUEST_MODE_DELETE },
13
+ { .name = "STREAM", .id = HTTP_REQUEST_MODE_STREAM },
14
10
- case HTTP_REQUEST_MODE_GET:
11
- return "GET";
12
-
13
- case HTTP_REQUEST_MODE_FILECOPY:
14
- return "FILECOPY";
15
-
16
- case HTTP_REQUEST_MODE_POST:
17
- return "POST";
18
-
19
- case HTTP_REQUEST_MODE_PUT:
20
- return "PUT";
21
-
22
- case HTTP_REQUEST_MODE_DELETE:
23
- return "DELETE";
24
-
25
- case HTTP_REQUEST_MODE_STREAM:
26
- return "STREAM";
15
+ // terminator
16
+ { .name = NULL, .id = 0 }
17
+};
18
28
- default:
29
- return "UNKNOWN";
30
- }
31
-}
19
+ENUM_STR_DEFINE_FUNCTIONS(HTTP_REQUEST_MODE, 0, "UNKNOWN");
20
21
const char *http_response_code2string(int code) {
22
switch(code) {
libnetdata/http/http_defs.h
+2
-1
@@ -53,7 +53,8 @@ typedef enum __attribute__((__packed__)) {
53
HTTP_REQUEST_MODE_STREAM = 7,
54
} HTTP_REQUEST_MODE;
55
56
-const char *http_request_method2string(HTTP_REQUEST_MODE mode);
56
+ENUM_STR_DEFINE_FUNCTIONS_EXTERN(HTTP_REQUEST_MODE);
57
+
58
const char *http_response_code2string(int code);
59
HTTP_CONTENT_TYPE contenttype_for_filename(const char *filename);
60
libnetdata/json/json-c-parser-inline.h
+23
@@ -25,6 +25,29 @@
25
} \
26
} while(0)
27
28
+#define JSONC_PARSE_TXT2BUFFER_OR_ERROR_AND_RETURN(jobj, path, member, dst, error, required) do { \
29
+ json_object *_j; \
30
+ if (json_object_object_get_ex(jobj, member, &_j) && json_object_is_type(_j, json_type_string)) { \
31
+ const char *_s = json_object_get_string(_j); \
32
+ if(!_s || !*_s) { \
33
+ buffer_free(dst); \
34
+ dst = NULL; \
35
+ } \
36
+ else { \
37
+ if (dst) \
38
+ buffer_flush(dst); \
39
+ else \
40
+ dst = buffer_create(0, NULL); \
41
+ if (_s && *_s) \
42
+ buffer_strcat(dst, _s); \
43
+ } \
44
+ } \
45
+ else if(required) { \
46
+ buffer_sprintf(error, "missing or invalid type for '%s.%s' string", path, member); \
47
+ return false; \
48
+ } \
49
+} while(0)
50
+
51
#define JSONC_PARSE_TXT2PATTERN_OR_ERROR_AND_RETURN(jobj, path, member, dst, error) do { \
52
json_object *_j; \
53
if (json_object_object_get_ex(jobj, member, &_j) && json_object_is_type(_j, json_type_string)) { \
libnetdata/libnetdata.h
+1
@@ -705,6 +705,7 @@ extern char *netdata_configured_host_prefix;
705
#include "xxhash.h"
706
707
#include "uuid/uuid.h"
708
+#include "template-enum.h"
709
#include "http/http_access.h"
710
#include "http/content_type.h"
711
#include "config/dyncfg.h"
libnetdata/query_progress/progress.c
+1
-1
@@ -444,7 +444,7 @@ int progress_function_result(BUFFER *wb, const char *hostname) {
444
445
buffer_json_add_array_item_uuid_compact(wb, &qp->transaction);
446
buffer_json_add_array_item_uint64(wb, qp->started_ut);
447
- buffer_json_add_array_item_string(wb, http_request_method2string(qp->mode));
447
+ buffer_json_add_array_item_string(wb, HTTP_REQUEST_MODE_2str(qp->mode));
448
buffer_json_add_array_item_string(wb, buffer_tostring(qp->query));
449
450
if(!buffer_strlen(qp->client)) {
libnetdata/template-enum.h
new
+40
@@ -0,0 +1,40 @@
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
+
3
+#ifndef NETDATA_TEMPLATE_ENUM_H
4
+#define NETDATA_TEMPLATE_ENUM_H
5
+
6
+#define ENUM_STR_MAP_DEFINE(type) \
7
+ static struct { \
8
+ type id; \
9
+ const char *name; \
10
+ } type ## _names[]
11
+
12
+#define ENUM_STR_DEFINE_FUNCTIONS_EXTERN(type) \
13
+ type type ## _2id(const char *str); \
14
+ const char *type##_2str(type id);
15
+
16
+#define ENUM_STR_DEFINE_FUNCTIONS(type, def, def_str) \
17
+ type type##_2id(const char *str) \
18
+ { \
19
+ if (!str || !*str) \
20
+ return def; \
21
+ \
22
+ for (size_t i = 0; type ## _names[i].name; i++) { \
23
+ if (strcmp(type ## _names[i].name, str) == 0) \
24
+ return type ## _names[i].id; \
25
+ } \
26
+ \
27
+ return def; \
28
+ } \
29
+ \
30
+ const char *type##_2str(type id) \
31
+ { \
32
+ for (size_t i = 0; type ## _names[i].name; i++) { \
33
+ if (id == type ## _names[i].id) \
34
+ return type ## _names[i].name; \
35
+ } \
36
+ \
37
+ return def_str; \
38
+ }
39
+
40
+#endif //NETDATA_TEMPLATE_ENUM_H
web/server/web_client.c
+4
-4
@@ -239,7 +239,7 @@ void web_client_log_completed_request(struct web_client *w, bool update_web_stat
239
ND_LOG_FIELD_U64(NDF_CONNECTION_ID, w->id),
240
ND_LOG_FIELD_UUID(NDF_TRANSACTION_ID, &w->transaction),
241
ND_LOG_FIELD_TXT(NDF_NIDL_NODE, w->client_host),
242
- ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, http_request_method2string(w->mode)),
242
+ ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, HTTP_REQUEST_MODE_2str(w->mode)),
243
ND_LOG_FIELD_BFR(NDF_REQUEST, w->url_as_received),
244
ND_LOG_FIELD_U64(NDF_RESPONSE_CODE, w->response.code),
245
ND_LOG_FIELD_U64(NDF_RESPONSE_SENT_BYTES, sent),
@@ -635,7 +635,7 @@ int web_client_api_request(RRDHOST *host, struct web_client *w, char *url_path_f
635
ND_LOG_FIELD_TXT(NDF_SRC_FORWARDED_HOST, w->forwarded_host),
636
ND_LOG_FIELD_TXT(NDF_SRC_FORWARDED_FOR, w->forwarded_for),
637
ND_LOG_FIELD_TXT(NDF_NIDL_NODE, w->client_host),
638
- ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, http_request_method2string(w->mode)),
638
+ ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, HTTP_REQUEST_MODE_2str(w->mode)),
639
ND_LOG_FIELD_BFR(NDF_REQUEST, w->url_as_received),
640
ND_LOG_FIELD_U64(NDF_CONNECTION_ID, w->id),
641
ND_LOG_FIELD_UUID(NDF_TRANSACTION_ID, &w->transaction),
@@ -1170,7 +1170,7 @@ int web_client_api_request_with_node_selection(RRDHOST *host, struct web_client
1170
// entry point for all API requests
1171
1172
ND_LOG_STACK lgs[] = {
1173
- ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, http_request_method2string(w->mode)),
1173
+ ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, HTTP_REQUEST_MODE_2str(w->mode)),
1174
ND_LOG_FIELD_BFR(NDF_REQUEST, w->url_as_received),
1175
ND_LOG_FIELD_U64(NDF_CONNECTION_ID, w->id),
1176
ND_LOG_FIELD_UUID(NDF_TRANSACTION_ID, &w->transaction),
@@ -1397,7 +1397,7 @@ void web_client_process_request_from_web_server(struct web_client *w) {
1397
ND_LOG_FIELD_TXT(NDF_SRC_FORWARDED_HOST, w->forwarded_host),
1398
ND_LOG_FIELD_TXT(NDF_SRC_FORWARDED_FOR, w->forwarded_for),
1399
ND_LOG_FIELD_TXT(NDF_NIDL_NODE, w->client_host),
1400
- ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, http_request_method2string(w->mode)),
1400
+ ND_LOG_FIELD_TXT(NDF_REQUEST_METHOD, HTTP_REQUEST_MODE_2str(w->mode)),
1401
ND_LOG_FIELD_BFR(NDF_REQUEST, w->url_as_received),
1402
ND_LOG_FIELD_U64(NDF_CONNECTION_ID, w->id),
1403
ND_LOG_FIELD_UUID(NDF_TRANSACTION_ID, &w->transaction),