@cryptotaxi247 / netdata-1 / commits / f56ba4f8d

Remove includes outside of libnetdata. (#16607)

* Remove includes outside of libnetdata. libnetdata ended up using the header daemon/main.h. This commit tries to resolve this issue by: - Moving the thread tags under libnetdata/threads/threads.h - Dropping the definition of `send_analytics` from libnetdata/ and, thus, the requirement for an extra dummy function. - Adding a new analytics_statistic_send() function. - Changing netdata_cleanup_and_exit() to accept the raw parts of an analytics statistic. After this whole ordeal, only the declaration of netdata_cleanup_and_exit() is required from libnetdata/ * Fix function hidden under NETDATA_INTERNAL_CHECKS * Fix field access. Co-authored-by: Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com> * s/action_data/data/g * Fix analytics statistics for START/CRASH. * Pass all args to netdata_cleanup_and_exit in FreeBSD plugin --------- Co-authored-by: Stelios Fragkakis <52996999+stelfrag@users.noreply.github.com>

vkalintiris committed Dec 18, 2023 at 21:52 UTC f56ba4f8d66aea1c783b73fe85a24ade74b5b290
16 files changed +69 -40
collectors/freebsd.plugin/plugin_freebsd.c
+1 -1
@@ -91,7 +91,7 @@ void *freebsd_main(void *ptr)
91
92 // initialize FreeBSD plugin
93 if (freebsd_plugin_init())
94 - netdata_cleanup_and_exit(1);
94 + netdata_cleanup_and_exit(1, NULL, NULL, NULL);
95
96 // check the enabled status for each module
97 int i;
daemon/analytics.c
+20 -9
@@ -594,7 +594,9 @@ void *analytics_main(void *ptr)
594
595 analytics_gather_immutable_meta_data();
596 analytics_gather_mutable_meta_data();
597 - send_statistics("META_START", "-", "-");
597 +
598 + analytics_statistic_t statistic = { "META_START", "-", "-" };
599 + analytics_statistic_send(&statistic);
600 analytics_log_data();
601
602 sec = 0;
@@ -609,8 +611,11 @@ void *analytics_main(void *ptr)
611 continue;
612
613 analytics_gather_mutable_meta_data();
612 - send_statistics("META", "-", "-");
614 +
615 + analytics_statistic_t statistic = { "META", "-", "-" };
616 + analytics_statistic_send(&statistic);
617 analytics_log_data();
618 +
619 sec = 0;
620 }
621
@@ -936,7 +941,10 @@ void set_global_environment() {
941 setenv("LC_ALL", "C", 1);
942 }
943
939 -void send_statistics(const char *action, const char *action_result, const char *action_data) {
944 +void analytics_statistic_send(const analytics_statistic_t *statistic) {
945 + if (!statistic)
946 + return;
947 +
948 static char *as_script;
949
950 if (netdata_anonymous_statistics_enabled == -1) {
@@ -973,16 +981,19 @@ void send_statistics(const char *action, const char *action_result, const char *
981 freez(optout_file);
982 }
983
976 - if (!netdata_anonymous_statistics_enabled || !action)
984 + if (!netdata_anonymous_statistics_enabled || !statistic->action)
985 return;
986
979 - if (!action_result)
987 + const char *action_result = statistic->result;
988 + const char *action_data = statistic->data;
989 +
990 + if (!statistic->result)
991 action_result = "";
981 - if (!action_data)
992 + if (!statistic->data)
993 action_data = "";
994
995 char *command_to_run = mallocz(
985 - sizeof(char) * (strlen(action) + strlen(action_result) + strlen(action_data) + strlen(as_script) +
996 + sizeof(char) * (strlen(statistic->action) + strlen(action_result) + strlen(action_data) + strlen(as_script) +
997 analytics_data.data_length + (ANALYTICS_NO_OF_ITEMS * 3) + 15));
998 pid_t command_pid;
999
@@ -990,7 +1001,7 @@ void send_statistics(const char *action, const char *action_result, const char *
1001 command_to_run,
1002 "%s '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' '%s' ",
1003 as_script,
993 - action,
1004 + statistic->action,
1005 action_result,
1006 action_data,
1007 analytics_data.netdata_config_stream_enabled,
@@ -1036,7 +1047,7 @@ void send_statistics(const char *action, const char *action_result, const char *
1047
1048 nd_log(NDLS_DAEMON, NDLP_DEBUG,
1049 "%s '%s' '%s' '%s'",
1039 - as_script, action, action_result, action_data);
1050 + as_script, statistic->action, action_result, action_data);
1051
1052 FILE *fp_child_input;
1053 FILE *fp_child_output = netdata_popen(command_to_run, &command_pid, &fp_child_input);
daemon/analytics.h
+8 -1
@@ -78,7 +78,6 @@ struct analytics_data {
78 void set_late_global_environment(struct rrdhost_system_info *system_info);
79 void analytics_free_data(void);
80 void set_global_environment(void);
81 -void send_statistics(const char *action, const char *action_result, const char *action_data);
81 void analytics_log_shell(void);
82 void analytics_log_json(void);
83 void analytics_log_prometheus(void);
@@ -87,6 +86,14 @@ void analytics_gather_mutable_meta_data(void);
86 void analytics_report_oom_score(long long int score);
87 void get_system_timezone(void);
88
89 +typedef struct {
90 + const char *action;
91 + const char *result;
92 + const char *data;
93 +} analytics_statistic_t;
94 +
95 +void analytics_statistic_send(const analytics_statistic_t *statistic);
96 +
97 extern struct analytics_data analytics_data;
98
99 #endif //NETDATA_ANALYTICS_H
daemon/commands.c
+1 -1
@@ -183,7 +183,7 @@ static cmd_status_t cmd_exit_execute(char *args, char **message)
183
184 nd_log_limits_unlimited();
185 netdata_log_info("COMMAND: Cleaning up to exit.");
186 - netdata_cleanup_and_exit(0);
186 + netdata_cleanup_and_exit(0, NULL, NULL, NULL);
187 exit(0);
188
189 return CMD_STATUS_SUCCESS;
daemon/daemon.h
+1 -1
@@ -7,7 +7,7 @@ int become_user(const char *username, int pid_fd);
7
8 int become_daemon(int dont_fork, const char *user);
9
10 -void netdata_cleanup_and_exit(int i);
10 +void netdata_cleanup_and_exit(int ret, const char *action, const char *action_result, const char *action_data);
11
12 void get_netdata_execution_path(void);
13
daemon/main.c
+21 -8
@@ -309,7 +309,7 @@ static bool service_wait_exit(SERVICE_TYPE service, usec_t timeout_ut) {
309
310 void web_client_cache_destroy(void);
311
312 -void netdata_cleanup_and_exit(int ret) {
312 +void netdata_cleanup_and_exit(int ret, const char *action, const char *action_result, const char *action_data) {
313 usec_t started_ut = now_monotonic_usec();
314 usec_t last_ut = started_ut;
315 const char *prev_msg = NULL;
@@ -318,7 +318,13 @@ void netdata_cleanup_and_exit(int ret) {
318 nd_log_limits_unlimited();
319 netdata_log_info("NETDATA SHUTDOWN: initializing shutdown with code %d...", ret);
320
321 - send_statistics("EXIT", ret?"ERROR":"OK","-");
321 + // send the stat from our caller
322 + analytics_statistic_t statistic = { action, action_result, action_data };
323 + analytics_statistic_send(&statistic);
324 +
325 + // notify we are exiting
326 + statistic = (analytics_statistic_t) {"EXIT", ret?"ERROR":"OK","-"};
327 + analytics_statistic_send(&statistic);
328
329 delta_shutdown_time("create shutdown file");
330
@@ -2205,11 +2211,16 @@ int main(int argc, char **argv) {
2211 netdata_log_info("NETDATA STARTUP: completed in %llu ms. Enjoy real-time performance monitoring!", (ready_ut - started_ut) / USEC_PER_MS);
2212 netdata_ready = true;
2213
2208 - send_statistics("START", "-", "-");
2209 - if (crash_detected)
2210 - send_statistics("CRASH", "-", "-");
2211 - if (incomplete_shutdown_detected)
2212 - send_statistics("INCOMPLETE_SHUTDOWN", "-", "-");
2214 + analytics_statistic_t start_statistic = { "START", "-", "-" };
2215 + analytics_statistic_send(&start_statistic);
2216 + if (crash_detected) {
2217 + analytics_statistic_t crash_statistic = { "CRASH", "-", "-" };
2218 + analytics_statistic_send(&crash_statistic);
2219 + }
2220 + if (incomplete_shutdown_detected) {
2221 + analytics_statistic_t incomplete_shutdown_statistic = { "INCOMPLETE_SHUTDOWN", "-", "-" };
2222 + analytics_statistic_send(&incomplete_shutdown_statistic);
2223 + }
2224
2225 //check if ANALYTICS needs to start
2226 if (netdata_anonymous_statistics_enabled == 1) {
@@ -2231,7 +2242,9 @@ int main(int argc, char **argv) {
2242 char filename[FILENAME_MAX + 1];
2243 snprintfz(filename, FILENAME_MAX, "%s/.aclk_report_sent", netdata_configured_varlib_dir);
2244 if (netdata_anonymous_statistics_enabled > 0 && access(filename, F_OK)) { // -1 -> not initialized
2234 - send_statistics("ACLK_DISABLED", "-", "-");
2245 + analytics_statistic_t statistic = { "ACLK_DISABLED", "-", "-" };
2246 + analytics_statistic_send(&statistic);
2247 +
2248 int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 444);
2249 if (fd == -1)
2250 netdata_log_error("Cannot create file '%s'. Please fix this.", filename);
daemon/main.h
-1
@@ -24,7 +24,6 @@ struct option_def {
24
25 void cancel_main_threads(void);
26 int killpid(pid_t pid);
27 -void netdata_cleanup_and_exit(int ret) NORETURN;
27
28 typedef enum {
29 ABILITY_DATA_QUERIES = (1 << 0),
daemon/signals.c
+1 -1
@@ -227,7 +227,7 @@ void signals_handle(void) {
227 nd_log_limits_unlimited();
228 netdata_log_info("SIGNAL: Received %s. Cleaning up to exit...", name);
229 commands_exit();
230 - netdata_cleanup_and_exit(0);
230 + netdata_cleanup_and_exit(0, NULL, NULL, NULL);
231 exit(0);
232 break;
233
exporting/exporting_engine.c
+3 -1
@@ -184,7 +184,9 @@ void *exporting_main(void *ptr)
184
185 if (init_connectors(engine) != 0) {
186 netdata_log_error("EXPORTING: cannot initialize exporting connectors");
187 - send_statistics("EXPORTING_START", "FAIL", "-");
187 +
188 + analytics_statistic_t statistic = { "EXPORTING_START", "FAIL", "-" };
189 + analytics_statistic_send(&statistic);
190 goto cleanup;
191 }
192
exporting/init_connectors.c
+2 -1
@@ -99,7 +99,8 @@ int init_connectors(struct engine *engine)
99 snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "EXPORTING-%zu", instance->index);
100 uv_thread_set_name_np(instance->thread, threadname);
101
102 - send_statistics("EXPORTING_START", "OK", instance->config.type_name);
102 + analytics_statistic_t statistic = { "EXPORTING_START", "OK", instance->config.type_name };
103 + analytics_statistic_send(&statistic);
104 }
105
106 return 0;
libnetdata/libnetdata.h
+1 -1
@@ -690,7 +690,7 @@ typedef enum {
690 } OPEN_FD_EXCLUDE;
691 void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds);
692
693 -void netdata_cleanup_and_exit(int ret) NORETURN;
693 +void netdata_cleanup_and_exit(int ret, const char *action, const char *action_result, const char *action_data) NORETURN;
694 extern char *netdata_configured_host_prefix;
695
696 #define XXH_INLINE_ALL
libnetdata/log/log.c
+2 -4
@@ -5,7 +5,6 @@
5 #define SD_JOURNAL_SUPPRESS_LOCATION
6
7 #include "../libnetdata.h"
8 -#include <daemon/main.h>
8
9 #ifdef __FreeBSD__
10 #include <sys/endian.h>
@@ -2285,7 +2284,6 @@ void netdata_logger_fatal( const char *file, const char *function, const unsigne
2284
2285 char action_data[70+1];
2286 snprintfz(action_data, 70, "%04lu@%-10.10s:%-15.15s/%d", line, file, function, saved_errno);
2288 - char action_result[60+1];
2287
2288 const char *thread_tag = thread_log_fields[NDF_THREAD_TAG].entry.txt;
2289 if(!thread_tag)
@@ -2299,8 +2297,8 @@ void netdata_logger_fatal( const char *file, const char *function, const unsigne
2297 if(strncmp(thread_tag, THREAD_TAG_STREAM_SENDER, strlen(THREAD_TAG_STREAM_SENDER)) == 0)
2298 tag_to_send = THREAD_TAG_STREAM_SENDER;
2299
2300 + char action_result[60+1];
2301 snprintfz(action_result, 60, "%s:%s", program_name, tag_to_send);
2303 - send_statistics("FATAL", action_result, action_data);
2302
2303 #ifdef HAVE_BACKTRACE
2304 int fd = nd_log.sources[NDLS_DAEMON].fd;
@@ -2319,7 +2317,7 @@ void netdata_logger_fatal( const char *file, const char *function, const unsigne
2317 abort();
2318 #endif
2319
2322 - netdata_cleanup_and_exit(1);
2320 + netdata_cleanup_and_exit(1, "FATAL", action_result, action_data);
2321 }
2322
2323 // ----------------------------------------------------------------------------
libnetdata/required_dummies.h
+3 -6
@@ -4,16 +4,13 @@
4 #define NETDATA_LIB_DUMMIES_H 1
5
6 // callback required by fatal()
7 -void netdata_cleanup_and_exit(int ret)
8 -{
9 - exit(ret);
10 -}
11 -
12 -void send_statistics(const char *action, const char *action_result, const char *action_data)
7 +void netdata_cleanup_and_exit(int ret, const char *action, const char *action_result, const char *action_data)
8 {
9 (void)action;
10 (void)action_result;
11 (void)action_data;
12 +
13 + exit(ret);
14 }
15
16 // callbacks required by popen()
libnetdata/threads/threads.h
+4
@@ -59,6 +59,10 @@ struct netdata_static_thread {
59 const char *netdata_thread_tag(void);
60 int netdata_thread_tag_exists(void);
61
62 +#define THREAD_TAG_STREAM_RECEIVER "RCVR"
63 +#define THREAD_TAG_STREAM_SENDER "SNDR"
64 +
65 +
66 size_t netdata_threads_init(void);
67 void netdata_threads_init_after_fork(size_t stacksize);
68 void netdata_threads_init_for_external_plugins(size_t stacksize);
streaming/rrdpush.h
-3
@@ -459,9 +459,6 @@ void rrdpush_send_claimed_id(RRDHOST *host);
459 void rrdpush_send_global_functions(RRDHOST *host);
460 void rrdpush_send_dyncfg(RRDHOST *host);
461
462 -#define THREAD_TAG_STREAM_RECEIVER "RCVR" // "[host]" is appended
463 -#define THREAD_TAG_STREAM_SENDER "SNDR" // "[host]" is appended
464 -
462 int rrdpush_receiver_thread_spawn(struct web_client *w, char *decoded_query_string, void *h2o_ctx);
463 void rrdpush_sender_thread_stop(RRDHOST *host, STREAM_HANDSHAKE reason, bool wait);
464
web/server/web_client.c
+1 -1
@@ -1490,7 +1490,7 @@ static inline int web_client_process_url(RRDHOST *host, struct web_client *w, ch
1490 buffer_strcat(w->response.data, "I am doing it already");
1491
1492 netdata_log_error("web request to exit received.");
1493 - netdata_cleanup_and_exit(0);
1493 + netdata_cleanup_and_exit(0, NULL, NULL, NULL);
1494 return HTTP_RESP_OK;
1495 }
1496 else if(unlikely(hash == hash_debug && strcmp(tok, "debug") == 0)) {