Skip to content

Commit

Permalink
Remove includes outside of libnetdata. (netdata#16607)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
vkalintiris and stelfrag authored Dec 18, 2023
1 parent 1402ab9 commit f56ba4f
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 40 deletions.
2 changes: 1 addition & 1 deletion collectors/freebsd.plugin/plugin_freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void *freebsd_main(void *ptr)

// initialize FreeBSD plugin
if (freebsd_plugin_init())
netdata_cleanup_and_exit(1);
netdata_cleanup_and_exit(1, NULL, NULL, NULL);

// check the enabled status for each module
int i;
Expand Down
29 changes: 20 additions & 9 deletions daemon/analytics.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,9 @@ void *analytics_main(void *ptr)

analytics_gather_immutable_meta_data();
analytics_gather_mutable_meta_data();
send_statistics("META_START", "-", "-");

analytics_statistic_t statistic = { "META_START", "-", "-" };
analytics_statistic_send(&statistic);
analytics_log_data();

sec = 0;
Expand All @@ -609,8 +611,11 @@ void *analytics_main(void *ptr)
continue;

analytics_gather_mutable_meta_data();
send_statistics("META", "-", "-");

analytics_statistic_t statistic = { "META", "-", "-" };
analytics_statistic_send(&statistic);
analytics_log_data();

sec = 0;
}

Expand Down Expand Up @@ -936,7 +941,10 @@ void set_global_environment() {
setenv("LC_ALL", "C", 1);
}

void send_statistics(const char *action, const char *action_result, const char *action_data) {
void analytics_statistic_send(const analytics_statistic_t *statistic) {
if (!statistic)
return;

static char *as_script;

if (netdata_anonymous_statistics_enabled == -1) {
Expand Down Expand Up @@ -973,24 +981,27 @@ void send_statistics(const char *action, const char *action_result, const char *
freez(optout_file);
}

if (!netdata_anonymous_statistics_enabled || !action)
if (!netdata_anonymous_statistics_enabled || !statistic->action)
return;

if (!action_result)
const char *action_result = statistic->result;
const char *action_data = statistic->data;

if (!statistic->result)
action_result = "";
if (!action_data)
if (!statistic->data)
action_data = "";

char *command_to_run = mallocz(
sizeof(char) * (strlen(action) + strlen(action_result) + strlen(action_data) + strlen(as_script) +
sizeof(char) * (strlen(statistic->action) + strlen(action_result) + strlen(action_data) + strlen(as_script) +
analytics_data.data_length + (ANALYTICS_NO_OF_ITEMS * 3) + 15));
pid_t command_pid;

sprintf(
command_to_run,
"%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' ",
as_script,
action,
statistic->action,
action_result,
action_data,
analytics_data.netdata_config_stream_enabled,
Expand Down Expand Up @@ -1036,7 +1047,7 @@ void send_statistics(const char *action, const char *action_result, const char *

nd_log(NDLS_DAEMON, NDLP_DEBUG,
"%s '%s' '%s' '%s'",
as_script, action, action_result, action_data);
as_script, statistic->action, action_result, action_data);

FILE *fp_child_input;
FILE *fp_child_output = netdata_popen(command_to_run, &command_pid, &fp_child_input);
Expand Down
9 changes: 8 additions & 1 deletion daemon/analytics.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ struct analytics_data {
void set_late_global_environment(struct rrdhost_system_info *system_info);
void analytics_free_data(void);
void set_global_environment(void);
void send_statistics(const char *action, const char *action_result, const char *action_data);
void analytics_log_shell(void);
void analytics_log_json(void);
void analytics_log_prometheus(void);
Expand All @@ -87,6 +86,14 @@ void analytics_gather_mutable_meta_data(void);
void analytics_report_oom_score(long long int score);
void get_system_timezone(void);

typedef struct {
const char *action;
const char *result;
const char *data;
} analytics_statistic_t;

void analytics_statistic_send(const analytics_statistic_t *statistic);

extern struct analytics_data analytics_data;

#endif //NETDATA_ANALYTICS_H
2 changes: 1 addition & 1 deletion daemon/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static cmd_status_t cmd_exit_execute(char *args, char **message)

nd_log_limits_unlimited();
netdata_log_info("COMMAND: Cleaning up to exit.");
netdata_cleanup_and_exit(0);
netdata_cleanup_and_exit(0, NULL, NULL, NULL);
exit(0);

return CMD_STATUS_SUCCESS;
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ int become_user(const char *username, int pid_fd);

int become_daemon(int dont_fork, const char *user);

void netdata_cleanup_and_exit(int i);
void netdata_cleanup_and_exit(int ret, const char *action, const char *action_result, const char *action_data);

void get_netdata_execution_path(void);

Expand Down
29 changes: 21 additions & 8 deletions daemon/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ static bool service_wait_exit(SERVICE_TYPE service, usec_t timeout_ut) {

void web_client_cache_destroy(void);

void netdata_cleanup_and_exit(int ret) {
void netdata_cleanup_and_exit(int ret, const char *action, const char *action_result, const char *action_data) {
usec_t started_ut = now_monotonic_usec();
usec_t last_ut = started_ut;
const char *prev_msg = NULL;
Expand All @@ -318,7 +318,13 @@ void netdata_cleanup_and_exit(int ret) {
nd_log_limits_unlimited();
netdata_log_info("NETDATA SHUTDOWN: initializing shutdown with code %d...", ret);

send_statistics("EXIT", ret?"ERROR":"OK","-");
// send the stat from our caller
analytics_statistic_t statistic = { action, action_result, action_data };
analytics_statistic_send(&statistic);

// notify we are exiting
statistic = (analytics_statistic_t) {"EXIT", ret?"ERROR":"OK","-"};
analytics_statistic_send(&statistic);

delta_shutdown_time("create shutdown file");

Expand Down Expand Up @@ -2205,11 +2211,16 @@ int main(int argc, char **argv) {
netdata_log_info("NETDATA STARTUP: completed in %llu ms. Enjoy real-time performance monitoring!", (ready_ut - started_ut) / USEC_PER_MS);
netdata_ready = true;

send_statistics("START", "-", "-");
if (crash_detected)
send_statistics("CRASH", "-", "-");
if (incomplete_shutdown_detected)
send_statistics("INCOMPLETE_SHUTDOWN", "-", "-");
analytics_statistic_t start_statistic = { "START", "-", "-" };
analytics_statistic_send(&start_statistic);
if (crash_detected) {
analytics_statistic_t crash_statistic = { "CRASH", "-", "-" };
analytics_statistic_send(&crash_statistic);
}
if (incomplete_shutdown_detected) {
analytics_statistic_t incomplete_shutdown_statistic = { "INCOMPLETE_SHUTDOWN", "-", "-" };
analytics_statistic_send(&incomplete_shutdown_statistic);
}

//check if ANALYTICS needs to start
if (netdata_anonymous_statistics_enabled == 1) {
Expand All @@ -2231,7 +2242,9 @@ int main(int argc, char **argv) {
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s/.aclk_report_sent", netdata_configured_varlib_dir);
if (netdata_anonymous_statistics_enabled > 0 && access(filename, F_OK)) { // -1 -> not initialized
send_statistics("ACLK_DISABLED", "-", "-");
analytics_statistic_t statistic = { "ACLK_DISABLED", "-", "-" };
analytics_statistic_send(&statistic);

int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 444);
if (fd == -1)
netdata_log_error("Cannot create file '%s'. Please fix this.", filename);
Expand Down
1 change: 0 additions & 1 deletion daemon/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ struct option_def {

void cancel_main_threads(void);
int killpid(pid_t pid);
void netdata_cleanup_and_exit(int ret) NORETURN;

typedef enum {
ABILITY_DATA_QUERIES = (1 << 0),
Expand Down
2 changes: 1 addition & 1 deletion daemon/signals.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ void signals_handle(void) {
nd_log_limits_unlimited();
netdata_log_info("SIGNAL: Received %s. Cleaning up to exit...", name);
commands_exit();
netdata_cleanup_and_exit(0);
netdata_cleanup_and_exit(0, NULL, NULL, NULL);
exit(0);
break;

Expand Down
4 changes: 3 additions & 1 deletion exporting/exporting_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ void *exporting_main(void *ptr)

if (init_connectors(engine) != 0) {
netdata_log_error("EXPORTING: cannot initialize exporting connectors");
send_statistics("EXPORTING_START", "FAIL", "-");

analytics_statistic_t statistic = { "EXPORTING_START", "FAIL", "-" };
analytics_statistic_send(&statistic);
goto cleanup;
}

Expand Down
3 changes: 2 additions & 1 deletion exporting/init_connectors.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ int init_connectors(struct engine *engine)
snprintfz(threadname, NETDATA_THREAD_NAME_MAX, "EXPORTING-%zu", instance->index);
uv_thread_set_name_np(instance->thread, threadname);

send_statistics("EXPORTING_START", "OK", instance->config.type_name);
analytics_statistic_t statistic = { "EXPORTING_START", "OK", instance->config.type_name };
analytics_statistic_send(&statistic);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion libnetdata/libnetdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ typedef enum {
} OPEN_FD_EXCLUDE;
void for_each_open_fd(OPEN_FD_ACTION action, OPEN_FD_EXCLUDE excluded_fds);

void netdata_cleanup_and_exit(int ret) NORETURN;
void netdata_cleanup_and_exit(int ret, const char *action, const char *action_result, const char *action_data) NORETURN;
extern char *netdata_configured_host_prefix;

#define XXH_INLINE_ALL
Expand Down
6 changes: 2 additions & 4 deletions libnetdata/log/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#define SD_JOURNAL_SUPPRESS_LOCATION

#include "../libnetdata.h"
#include <daemon/main.h>

#ifdef __FreeBSD__
#include <sys/endian.h>
Expand Down Expand Up @@ -2285,7 +2284,6 @@ void netdata_logger_fatal( const char *file, const char *function, const unsigne

char action_data[70+1];
snprintfz(action_data, 70, "%04lu@%-10.10s:%-15.15s/%d", line, file, function, saved_errno);
char action_result[60+1];

const char *thread_tag = thread_log_fields[NDF_THREAD_TAG].entry.txt;
if(!thread_tag)
Expand All @@ -2299,8 +2297,8 @@ void netdata_logger_fatal( const char *file, const char *function, const unsigne
if(strncmp(thread_tag, THREAD_TAG_STREAM_SENDER, strlen(THREAD_TAG_STREAM_SENDER)) == 0)
tag_to_send = THREAD_TAG_STREAM_SENDER;

char action_result[60+1];
snprintfz(action_result, 60, "%s:%s", program_name, tag_to_send);
send_statistics("FATAL", action_result, action_data);

#ifdef HAVE_BACKTRACE
int fd = nd_log.sources[NDLS_DAEMON].fd;
Expand All @@ -2319,7 +2317,7 @@ void netdata_logger_fatal( const char *file, const char *function, const unsigne
abort();
#endif

netdata_cleanup_and_exit(1);
netdata_cleanup_and_exit(1, "FATAL", action_result, action_data);
}

// ----------------------------------------------------------------------------
Expand Down
9 changes: 3 additions & 6 deletions libnetdata/required_dummies.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@
#define NETDATA_LIB_DUMMIES_H 1

// callback required by fatal()
void netdata_cleanup_and_exit(int ret)
{
exit(ret);
}

void send_statistics(const char *action, const char *action_result, const char *action_data)
void netdata_cleanup_and_exit(int ret, const char *action, const char *action_result, const char *action_data)
{
(void)action;
(void)action_result;
(void)action_data;

exit(ret);
}

// callbacks required by popen()
Expand Down
4 changes: 4 additions & 0 deletions libnetdata/threads/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ struct netdata_static_thread {
const char *netdata_thread_tag(void);
int netdata_thread_tag_exists(void);

#define THREAD_TAG_STREAM_RECEIVER "RCVR"
#define THREAD_TAG_STREAM_SENDER "SNDR"


size_t netdata_threads_init(void);
void netdata_threads_init_after_fork(size_t stacksize);
void netdata_threads_init_for_external_plugins(size_t stacksize);
Expand Down
3 changes: 0 additions & 3 deletions streaming/rrdpush.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,9 +459,6 @@ void rrdpush_send_claimed_id(RRDHOST *host);
void rrdpush_send_global_functions(RRDHOST *host);
void rrdpush_send_dyncfg(RRDHOST *host);

#define THREAD_TAG_STREAM_RECEIVER "RCVR" // "[host]" is appended
#define THREAD_TAG_STREAM_SENDER "SNDR" // "[host]" is appended

int rrdpush_receiver_thread_spawn(struct web_client *w, char *decoded_query_string, void *h2o_ctx);
void rrdpush_sender_thread_stop(RRDHOST *host, STREAM_HANDSHAKE reason, bool wait);

Expand Down
2 changes: 1 addition & 1 deletion web/server/web_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ static inline int web_client_process_url(RRDHOST *host, struct web_client *w, ch
buffer_strcat(w->response.data, "I am doing it already");

netdata_log_error("web request to exit received.");
netdata_cleanup_and_exit(0);
netdata_cleanup_and_exit(0, NULL, NULL, NULL);
return HTTP_RESP_OK;
}
else if(unlikely(hash == hash_debug && strcmp(tok, "debug") == 0)) {
Expand Down

0 comments on commit f56ba4f

Please sign in to comment.