diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-05-18 16:13:31 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-05-18 16:13:31 +0100 |
commit | 1cf0d5687baf976760681b0db6eed754555ca89b (patch) | |
tree | f6364189af4c0141caa56b46273cb14c04ec3311 | |
parent | 56e039715266749c29f03eaf1a200ec3b740766e (diff) | |
download | rspamd-1cf0d5687baf976760681b0db6eed754555ca89b.tar.gz rspamd-1cf0d5687baf976760681b0db6eed754555ca89b.zip |
[Fix] Rework metrics handling
Issue: #3754
-rw-r--r-- | src/controller.c | 135 | ||||
-rw-r--r-- | src/libserver/worker_util.c | 6 | ||||
-rw-r--r-- | src/libserver/worker_util.h | 4 |
3 files changed, 79 insertions, 66 deletions
diff --git a/src/controller.c b/src/controller.c index ce4d9b21b..185970efa 100644 --- a/src/controller.c +++ b/src/controller.c @@ -2732,6 +2732,7 @@ rspamd_controller_metrics_fin_task (void *ud) { ucl_object_t *top; GList *fuzzy_elts, *cur; struct rspamd_fuzzy_stat_entry *entry; + rspamd_fstring_t *output; gint i; conn_ent = cbdata->conn_ent; @@ -2740,87 +2741,103 @@ rspamd_controller_metrics_fin_task (void *ud) { ucl_object_insert_key (top, ucl_object_fromint (cbdata->learned), "total_learns", 0, false); - GString* output = g_string_new (""); - g_string_append_printf (output, "build_info{version=\"%s\"} 1\n", + output = rspamd_fstring_sized_new (1024); + rspamd_printf_fstring (&output, "build_info{version=\"%s\"} 1\n", ucl_object_tostring (ucl_object_lookup (top, "version"))); - g_string_append_printf (output, "config{id=\"%s\"} 1\n", + rspamd_printf_fstring (&output, "config{id=\"%s\"} 1\n", ucl_object_tostring (ucl_object_lookup (top, "config_id"))); - g_string_append_printf (output, "process_start_time_seconds %" PRId64 "\n", + rspamd_printf_fstring (&output, "process_start_time_seconds %L\n", ucl_object_toint (ucl_object_lookup (top, "start_time"))); - g_string_append_printf (output, "read_only %" PRId64 "\n", + rspamd_printf_fstring (&output, "read_only %L\n", ucl_object_toint (ucl_object_lookup (top, "read_only"))); - g_string_append_printf (output, "scanned %" PRId64 "\n", + rspamd_printf_fstring (&output, "scanned %L\n", ucl_object_toint (ucl_object_lookup (top, "scanned"))); - g_string_append_printf (output, "learned %" PRId64 "\n", + rspamd_printf_fstring (&output, "learned %L\n", ucl_object_toint (ucl_object_lookup (top, "learned"))); - g_string_append_printf (output, "spam_count %" PRId64 "\n", + rspamd_printf_fstring (&output, "spam_count %L\n", ucl_object_toint (ucl_object_lookup (top, "spam_count"))); - g_string_append_printf (output, "ham_count %" PRId64 "\n", + rspamd_printf_fstring (&output, "ham_count %L\n", ucl_object_toint (ucl_object_lookup (top, "ham_count"))); - g_string_append_printf (output, "connections %" PRId64 "\n", + rspamd_printf_fstring (&output, "connections %L\n", ucl_object_toint (ucl_object_lookup (top, "connections"))); - g_string_append_printf (output, "control_connections %" PRId64 "\n", + rspamd_printf_fstring (&output, "control_connections %L\n", ucl_object_toint (ucl_object_lookup (top, "control_connections"))); - g_string_append_printf (output, "pools_allocated %" PRId64 "\n", + rspamd_printf_fstring (&output, "pools_allocated %L\n", ucl_object_toint (ucl_object_lookup (top, "pools_allocated"))); - g_string_append_printf (output, "pools_freed %" PRId64 "\n", + rspamd_printf_fstring (&output, "pools_freed %" PRId64 "\n", ucl_object_toint (ucl_object_lookup (top, "pools_freed"))); - g_string_append_printf (output, "bytes_allocated %" PRId64 "\n", + rspamd_printf_fstring (&output, "bytes_allocated %L\n", ucl_object_toint (ucl_object_lookup (top, "bytes_allocated"))); - g_string_append_printf (output, "chunks_allocated %" PRId64 "\n", + rspamd_printf_fstring (&output, "chunks_allocated %L\n", ucl_object_toint (ucl_object_lookup (top, "chunks_allocated"))); - g_string_append_printf (output, "shared_chunks_allocated %" PRId64 "\n", + rspamd_printf_fstring (&output, "shared_chunks_allocated %L\n", ucl_object_toint (ucl_object_lookup (top, "shared_chunks_allocated"))); - g_string_append_printf (output, "chunks_freed %" PRId64 "\n", + rspamd_printf_fstring (&output, "chunks_freed %L\n", ucl_object_toint (ucl_object_lookup (top, "chunks_freed"))); - g_string_append_printf (output, "chunks_oversized %" PRId64 "\n", + rspamd_printf_fstring (&output, "chunks_oversized %L\n", ucl_object_toint (ucl_object_lookup (top, "chunks_oversized"))); - g_string_append_printf (output, "fragmented %" PRId64 "\n", + rspamd_printf_fstring (&output, "fragmented %L\n", ucl_object_toint (ucl_object_lookup (top, "fragmented"))); - g_string_append_printf (output, "total_learns %" PRId64 "\n", + rspamd_printf_fstring (&output, "total_learns %L\n", ucl_object_toint (ucl_object_lookup (top, "total_learns"))); - for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - gchar* path = malloc (strlen (rspamd_action_to_str(i))); - g_string_append_printf (output, "actions{type=\"%s\"} %" PRId64 "\n", - rspamd_action_to_str (i), - ucl_object_toint (ucl_object_lookup_path (top, path))); - g_free (path); + + const ucl_object_t *acts_obj = ucl_object_lookup (top, "actions"); + + if (acts_obj) { + for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { + const char *str_act = rspamd_action_to_str (i); + const ucl_object_t *act = ucl_object_lookup (acts_obj, str_act); + + if (act) { + rspamd_printf_fstring(&output, "actions{type=\"%s\"} %L\n", + str_act, + ucl_object_toint(act)); + } + else { + rspamd_printf_fstring (&output, "actions{type=\"%s\"} 0\n", + str_act); + } + } } if (cbdata->stat) { const ucl_object_t *cur_elt; - ucl_object_iter_t it = NULL; - while ((cur_elt = ucl_object_iterate (cbdata->stat, &it, true))) { - if (ucl_object_lookup_path (cur_elt, "symbol") && ucl_object_lookup_path (cur_elt, "type")) { - g_string_append_printf (output, "statfiles_revision{symbol=\"%s\", type=\"%s\"} %" PRId64 "\n", - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "symbol")), - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "type")), - ucl_object_toint (ucl_object_lookup_path (cur_elt, "revision"))); - g_string_append_printf (output, "statfiles_used{symbol=\"%s\", type=\"%s\"} %" PRId64 "\n", - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "symbol")), - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "type")), - ucl_object_toint (ucl_object_lookup_path (cur_elt, "used"))); - g_string_append_printf (output, "statfiles_total{symbol=\"%s\", type=\"%s\"} %" PRId64 "\n", - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "symbol")), - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "type")), - ucl_object_toint (ucl_object_lookup_path (cur_elt, "total"))); - g_string_append_printf (output, "statfiles_size{symbol=\"%s\", type=\"%s\"} %" PRId64 "\n", - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "symbol")), - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "type")), - ucl_object_toint (ucl_object_lookup_path (cur_elt, "size"))); - g_string_append_printf (output, "statfiles_languages{symbol=\"%s\", type=\"%s\"} %" PRId64 "\n", - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "symbol")), - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "type")), - ucl_object_toint (ucl_object_lookup_path (cur_elt, "languages"))); - g_string_append_printf (output, "statfiles_users{symbol=\"%s\", type=\"%s\"} %" PRId64 "\n", - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "symbol")), - ucl_object_tostring (ucl_object_lookup_path (cur_elt, "type")), - ucl_object_toint (ucl_object_lookup_path (cur_elt, "users"))); + ucl_object_iter_t it = NULL; + while ((cur_elt = ucl_object_iterate (cbdata->stat, &it, true))) { + if (ucl_object_lookup (cur_elt, "symbol") && + ucl_object_lookup (cur_elt, "type")) { + + const char *sym = ucl_object_tostring (ucl_object_lookup (cur_elt, "symbol")); + const char *type = ucl_object_tostring (ucl_object_lookup (cur_elt, "type")); + + rspamd_printf_fstring (&output, "statfiles_revision{symbol=\"%s\", type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint (ucl_object_lookup (cur_elt, "revision"))); + rspamd_printf_fstring (&output, "statfiles_used{symbol=\"%s\", type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint (ucl_object_lookup (cur_elt, "used"))); + rspamd_printf_fstring (&output, "statfiles_total{symbol=\"%s\", type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint (ucl_object_lookup (cur_elt, "total"))); + rspamd_printf_fstring (&output, "statfiles_size{symbol=\"%s\", type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint (ucl_object_lookup (cur_elt, "size"))); + rspamd_printf_fstring (&output, "statfiles_languages{symbol=\"%s\", type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint (ucl_object_lookup (cur_elt, "languages"))); + rspamd_printf_fstring (&output, "statfiles_users{symbol=\"%s\", type=\"%s\"} %L\n", + sym, + type, + ucl_object_toint (ucl_object_lookup (cur_elt, "users"))); } } } - fuzzy_elts = rspamd_mempool_get_variable (cbdata->task->task_pool, "fuzzy_stat"); if (fuzzy_elts) { @@ -2828,17 +2845,15 @@ rspamd_controller_metrics_fin_task (void *ud) { entry = cur->data; if (entry->name) { - g_string_append_printf (output, "fuzzy_stat{storage=\"%s\"} %" PRIu32 "\n", - entry->name, entry->fuzzy_cnt); + rspamd_printf_fstring (&output, "fuzzy_stat{storage=\"%s\"} %ud\n", + entry->name, entry->fuzzy_cnt); } } } - g_string_append (output, "# EOF\n"); - rspamd_controller_send_openmetrics (conn_ent, g_string_free (output, FALSE)); - - // TODO implement statfile metrics + rspamd_printf_fstring (&output, "# EOF\n"); + rspamd_controller_send_openmetrics (conn_ent, output); return TRUE; } diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index c1840c76d..cf20d9591 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -601,19 +601,17 @@ rspamd_controller_send_error (struct rspamd_http_connection_entry *entry, void rspamd_controller_send_openmetrics (struct rspamd_http_connection_entry *entry, - const gchar *str) + rspamd_fstring_t *str) { struct rspamd_http_message *msg; - rspamd_fstring_t *reply; msg = rspamd_http_new_message (HTTP_RESPONSE); msg->date = time (NULL); msg->code = 200; msg->status = rspamd_fstring_new_init ("OK", 2); - reply = rspamd_fstring_new_init (str, strlen (str)); rspamd_http_message_set_body_from_fstring_steal (msg, - rspamd_controller_maybe_compress (entry, reply, msg)); + rspamd_controller_maybe_compress (entry, str, msg)); rspamd_http_connection_reset (entry->conn); rspamd_http_router_insert_headers (entry->rt, msg); rspamd_http_connection_write_message (entry->conn, diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h index 973990974..67d598b88 100644 --- a/src/libserver/worker_util.h +++ b/src/libserver/worker_util.h @@ -121,11 +121,11 @@ void rspamd_controller_send_error (struct rspamd_http_connection_entry *entry, /** * Send openmetrics-formatted strings using HTTP * @param entry router entry - * @param str string to send + * @param str rspamd fstring buffer, ownership is transferred */ void rspamd_controller_send_openmetrics (struct rspamd_http_connection_entry *entry, - const gchar *str); + rspamd_fstring_t *str); /** * Send a custom string using HTTP |