From: Timo Sirainen Date: Tue, 9 Mar 2021 14:29:49 +0000 (+0200) Subject: lib-master, stats: Send global events to stats process X-Git-Tag: 2.3.18~370 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9d93a2482430f2eea0fd6f11419d3689dc6beea5;p=thirdparty%2Fdovecot%2Fcore.git lib-master, stats: Send global events to stats process --- diff --git a/src/lib-master/stats-client.c b/src/lib-master/stats-client.c index 798a1c5b4b..59d9b24472 100644 --- a/src/lib-master/stats-client.c +++ b/src/lib-master/stats-client.c @@ -127,7 +127,7 @@ static void stats_client_destroy(struct connection *conn) static const struct connection_settings stats_client_set = { .service_name_in = "stats-server", .service_name_out = "stats-client", - .major_version = 3, + .major_version = 4, .minor_version = 0, .input_max_size = SIZE_MAX, @@ -141,8 +141,8 @@ static const struct connection_vfuncs stats_client_vfuncs = { }; static void -stats_event_write(struct event *event, const struct failure_context *ctx, - string_t *str, bool begin) +stats_event_write(struct event *event, struct event *global_event, + const struct failure_context *ctx, string_t *str, bool begin) { struct event *merged_event; struct event *parent_event; @@ -154,7 +154,7 @@ stats_event_write(struct event *event, const struct failure_context *ctx, if (parent_event != NULL) { if (parent_event->sent_to_stats_id != parent_event->change_id) - stats_event_write(parent_event, ctx, str, TRUE); + stats_event_write(parent_event, NULL, ctx, str, TRUE); i_assert(parent_event->sent_to_stats_id != 0); } if (begin) { @@ -164,7 +164,8 @@ stats_event_write(struct event *event, const struct failure_context *ctx, str_printfa(str, "%s\t%"PRIu64"\t", cmd, event->id); event->sent_to_stats_id = event->change_id; } else { - str_append(str, "EVENT\t"); + str_printfa(str, "EVENT\t%"PRIu64"\t", + global_event == NULL ? 0 : global_event->id); } str_printfa(str, "%"PRIu64"\t", parent_event == NULL ? 0 : parent_event->id); @@ -187,9 +188,13 @@ stats_client_send_event(struct stats_client *client, struct event *event, return; /* Need to send the event for stats and/or export */ - string_t *str = t_str_new(256); - stats_event_write(event, ctx, str, FALSE); + + struct event *global_event = event_get_global(); + if (global_event != NULL) + stats_event_write(global_event, NULL, ctx, str, TRUE); + + stats_event_write(event, global_event, ctx, str, FALSE); o_stream_nsend(client->conn.output, str_data(str), str_len(str)); } diff --git a/src/lib-master/test-event-stats.c b/src/lib-master/test-event-stats.c index f49d6b726c..565118b4d5 100644 --- a/src/lib-master/test-event-stats.c +++ b/src/lib-master/test-event-stats.c @@ -157,7 +157,7 @@ static void stats_conn_input(struct connection *_conn) int fd; struct ostream *stats_data_out; struct server_connection *conn = (struct server_connection *)_conn; - const char *handshake = "VERSION\tstats-server\t3\t0\n" + const char *handshake = "VERSION\tstats-server\t4\t0\n" "FILTER\tcategory=test1 OR category=test2 OR category=test3 OR " "category=test4 OR category=test5\n"; const char *line = NULL; @@ -321,7 +321,7 @@ static void test_no_merging1(void) event_unref(&single_ev); test_assert( compare_test_stats_to( - "EVENT 0 1 0 0" + "EVENT 0 0 1 0 0" " s"__FILE__" %d" " l0 0 ctest1 Skey1 str1\n", l)); test_end(); @@ -345,7 +345,7 @@ static void test_no_merging2(void) event_unref(&child_ev); test_assert( compare_test_stats_to( - "EVENT %"PRIu64" 1 0 0" + "EVENT 0 %"PRIu64" 1 0 0" " s"__FILE__" %d" " l0 0 ctest2\n" "END 9\n", id, l)); @@ -374,7 +374,7 @@ static void test_no_merging3(void) compare_test_stats_to( "BEGIN %"PRIu64" 0 1 0 0" " s"__FILE__" %d ctest1\n" - "EVENT %"PRIu64" 1 1 0" + "EVENT 0 %"PRIu64" 1 1 0" " s"__FILE__" %d" " l1 0 ctest2\n" "END\t%"PRIu64"\n", idp, lp, idp, l, idp)); @@ -402,7 +402,7 @@ static void test_merge_events1(void) event_unref(&merge_ev2); test_assert( compare_test_stats_to( - "EVENT 0 1 0 0" + "EVENT 0 0 1 0 0" " s"__FILE__" %d l0 0" " ctest3 ctest2 ctest1 Tkey3" " 10 0 Ikey2 20" @@ -437,7 +437,7 @@ static void test_merge_events2(void) event_unref(&merge_ev2); test_assert( compare_test_stats_to( - "EVENT %"PRIu64" 1 0 0" + "EVENT 0 %"PRIu64" 1 0 0" " s"__FILE__" %d l0 0" " ctest3 ctest2 ctest1 Tkey3" " 10 0 Ikey2 20" @@ -472,7 +472,7 @@ static void test_skip_parents(void) compare_test_stats_to( "BEGIN %"PRIu64" 0 1 0 0" " s"__FILE__" %d ctest1\n" - "EVENT %"PRIu64" 1 3 0 " + "EVENT 0 %"PRIu64" 1 3 0 " "s"__FILE__" %d l3 0" " ctest2\nEND\t%"PRIu64"\n", id, lp, id, l, id)); test_end(); @@ -514,7 +514,7 @@ static void test_merge_events_skip_parents(void) compare_test_stats_to( "BEGIN %"PRIu64" 0 1 0 0" " s"__FILE__" %d ctest1\n" - "EVENT %"PRIu64" 1 3 0 " + "EVENT 0 %"PRIu64" 1 3 0 " "s"__FILE__" %d l3 0 " "ctest4 ctest5 Tkey3 10 0 Skey4" " str4\nEND\t%"PRIu64"\n", id, lp, id, l, id)); @@ -586,7 +586,7 @@ static void test_parent_update_post_send(void) "BEGIN %"PRIu64" 0 1 0 0" " s"__FILE__" %d ctest1" " Ia 1\n" - "EVENT %"PRIu64" 1 1 0" + "EVENT 0 %"PRIu64" 1 1 0" " s"__FILE__" %d" " l1 0 ctest2" " Ib 2\n" /* second e_info() */ @@ -596,7 +596,7 @@ static void test_parent_update_post_send(void) "BEGIN %"PRIu64" %"PRIu64" 1 0 0" " s"__FILE__" %d" " l0 0 ctest2 Ib 2\n" - "EVENT %"PRIu64" 1 1 0" + "EVENT 0 %"PRIu64" 1 1 0" " s"__FILE__" %d" " l1 0 ctest3" " Ic 3\n" @@ -642,19 +642,19 @@ static void test_large_event_id(void) test_assert( compare_test_stats_to( /* first e_info() */ - "EVENT %"PRIu64" 1 1 0" + "EVENT 0 %"PRIu64" 1 1 0" " s"__FILE__" %d" " l1 0 ctest1\n" "BEGIN %"PRIu64" 0 1 0 0" " s"__FILE__" %d" " l0 0 ctest1\n" - "EVENT %"PRIu64" 1 1 0" + "EVENT 0 %"PRIu64" 1 1 0" " s"__FILE__" %d" " l1 0 ctest2\n" "UPDATE %"PRIu64" 0 1 0" " s"__FILE__" %d" " l1 0 ctest1 Itest1 1\n" - "EVENT %"PRIu64" 1 1 0" + "EVENT 0 %"PRIu64" 1 1 0" " s"__FILE__" %d" " l1 0 ctest2\n" "END %"PRIu64"\n", @@ -670,6 +670,47 @@ static void test_large_event_id(void) test_end(); } +static void test_global_event(void) +{ + TST_BEGIN("merge events global"); + struct event *merge_ev1 = event_create(NULL); + event_add_category(merge_ev1, &test_cats[0]); + event_add_str(merge_ev1,test_fields[0].key, test_fields[0].value.str); + struct event *merge_ev2 = event_create(merge_ev1); + event_add_int(merge_ev2,test_fields[1].key, test_fields[1].value.intmax); + + struct event *global_event = event_create(NULL); + int global_event_line = __LINE__ - 1; + uint64_t global_event_id = global_event->id; + event_add_str(global_event, "global", "value"); + event_push_global(global_event); + + struct timeval tv; + event_get_create_time(merge_ev1, &tv); + + e_info(merge_ev2, "info message"); + int log_line = __LINE__ - 1; + + event_pop_global(global_event); + event_unref(&merge_ev1); + event_unref(&merge_ev2); + event_unref(&global_event); + + test_assert( + compare_test_stats_to( + "BEGIN\t%"PRIu64"\t0\t1\t0\t0" + "\ts"__FILE__"\t%d" + "\tSglobal\tvalue\n" + "EVENT\t%"PRIu64"\t0\t1\t0\t0" + "\ts"__FILE__"\t%d\tl0\t0" + "\tctest1\tIkey2\t20\tSkey1\tstr1\n" + "END\t%"PRIu64"\n", + global_event_id, global_event_line, + global_event_id, log_line, + global_event_id)); + test_end(); +} + static int run_tests(void) { int ret; @@ -683,6 +724,7 @@ static int run_tests(void) test_merge_events_skip_parents, test_parent_update_post_send, test_large_event_id, + test_global_event, NULL }; struct ioloop *ioloop = io_loop_create(); diff --git a/src/stats/client-writer.c b/src/stats/client-writer.c index d949415038..40772cf79b 100644 --- a/src/stats/client-writer.c +++ b/src/stats/client-writer.c @@ -140,14 +140,35 @@ static bool writer_client_input_event(struct writer_client *client, const char *const *args, const char **error_r) { - struct event *event; - uint64_t parent_event_id; + struct event *event, *global_event = NULL; + uint64_t parent_event_id, global_event_id; + bool ret; - if (args[0] == NULL || str_to_uint64(args[0], &parent_event_id) < 0) { + if (args[1] == NULL || str_to_uint64(args[0], &global_event_id) < 0) { + *error_r = "Invalid global event ID"; + return FALSE; + } + if (args[1] == NULL || str_to_uint64(args[1], &parent_event_id) < 0) { *error_r = "Invalid parent ID"; return FALSE; } - if (!writer_client_run_event(client, parent_event_id, args+1, &event, error_r)) + + if (global_event_id != 0) { + struct stats_event *stats_global_event = + writer_client_find_event(client, global_event_id); + if (stats_global_event == NULL) { + *error_r = "Unknown global event ID"; + return FALSE; + } + global_event = stats_global_event->event; + event_push_global(global_event); + } + + ret = writer_client_run_event(client, parent_event_id, args+2, + &event, error_r); + if (global_event != NULL) + event_pop_global(global_event); + if (!ret) return FALSE; event_unref(&event); return TRUE; @@ -306,7 +327,7 @@ writer_client_input_args(struct connection *conn, const char *const *args) static struct connection_settings client_set = { .service_name_in = "stats-client", .service_name_out = "stats-server", - .major_version = 3, + .major_version = 4, .minor_version = 0, .input_max_size = 1024*128, /* "big enough" */ diff --git a/src/stats/test-client-writer.c b/src/stats/test-client-writer.c index 290c3ec266..1256da9d30 100644 --- a/src/stats/test-client-writer.c +++ b/src/stats/test-client-writer.c @@ -38,7 +38,7 @@ static int test_writer_server_input_args(struct connection *conn, static struct connection_settings client_set = { .service_name_in = "stats-server", .service_name_out = "stats-client", - .major_version = 3, + .major_version = 4, .minor_version = 0, .input_max_size = SIZE_MAX,