diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-06-21 14:43:14 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2024-06-21 14:43:14 +0100 |
commit | 2d5d3c2a0dc6bb2600cce2ebcd73c95cb21a58db (patch) | |
tree | 6a252cfd13f51422cfa8a92cd9b7cca8a9243eca | |
parent | 90b73439d20d6e5b9b9e61cecbaa9809d7d0ddcd (diff) | |
download | rspamd-2d5d3c2a0dc6bb2600cce2ebcd73c95cb21a58db.tar.gz rspamd-2d5d3c2a0dc6bb2600cce2ebcd73c95cb21a58db.zip |
[Rework] Allow metrics endpoint to be enabled for each scanner
-rw-r--r-- | src/controller.c | 85 | ||||
-rw-r--r-- | src/libserver/worker_util.c | 68 | ||||
-rw-r--r-- | src/libserver/worker_util.h | 11 |
3 files changed, 90 insertions, 74 deletions
diff --git a/src/controller.c b/src/controller.c index 5526d9197..8ba12b605 100644 --- a/src/controller.c +++ b/src/controller.c @@ -3094,22 +3094,21 @@ rspamd_controller_handle_metrics_common( gboolean do_reset) { struct rspamd_controller_session *session = conn_ent->ud; - ucl_object_t *top, *sub; - int i; - int64_t uptime; - uint64_t spam = 0, ham = 0; - rspamd_mempool_stat_t mem_st; - struct rspamd_stat *stat, stat_copy; + ucl_object_t *top; + ev_tstamp uptime; + struct rspamd_stat stat_copy; struct rspamd_controller_worker_ctx *ctx; struct rspamd_task *task; struct rspamd_stat_cbdata *cbdata; - memset(&mem_st, 0, sizeof(mem_st)); - rspamd_mempool_stat(&mem_st); - memcpy(&stat_copy, session->ctx->worker->srv->stat, sizeof(stat_copy)); - stat = &stat_copy; + uptime = ev_time() - session->ctx->start_time; ctx = session->ctx; + memcpy(&stat_copy, session->ctx->worker->srv->stat, sizeof(stat_copy)); + top = rspamd_worker_metrics_object(session->ctx->cfg, &stat_copy, uptime); + ucl_object_insert_key(top, ucl_object_fromint(session->ctx->start_time), "start_time", 0, false); + ucl_object_insert_key(top, ucl_object_frombool(session->is_read_only), + "read_only", 0, false); task = rspamd_task_new(session->ctx->worker, session->cfg, session->pool, ctx->lang_det, ctx->event_loop, FALSE); task->resolver = ctx->resolver; @@ -3117,7 +3116,6 @@ rspamd_controller_handle_metrics_common( cbdata->conn_ent = conn_ent; cbdata->task = task; cbdata->ctx = ctx; - top = ucl_object_typed_new(UCL_OBJECT); cbdata->top = top; task->s = rspamd_session_create(session->pool, @@ -3127,75 +3125,20 @@ rspamd_controller_handle_metrics_common( cbdata); task->fin_arg = cbdata; task->http_conn = rspamd_http_connection_ref(conn_ent->conn); - ; task->sock = conn_ent->conn->fd; - ucl_object_insert_key(top, ucl_object_fromstring(RVERSION), "version", 0, false); - ucl_object_insert_key(top, ucl_object_fromstring(session->ctx->cfg->checksum), "config_id", 0, false); - uptime = ev_time() - session->ctx->start_time; - ucl_object_insert_key(top, ucl_object_fromint(uptime), "uptime", 0, false); - ucl_object_insert_key(top, ucl_object_fromint(session->ctx->start_time), "start_time", 0, false); - ucl_object_insert_key(top, ucl_object_frombool(session->is_read_only), - "read_only", 0, false); - ucl_object_insert_key(top, ucl_object_fromint(stat->messages_scanned), "scanned", 0, false); - ucl_object_insert_key(top, ucl_object_fromint(stat->messages_learned), "learned", 0, false); + if (stat_copy.messages_scanned > 0 && do_reset) { + for (int i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - if (stat->messages_scanned > 0) { - sub = ucl_object_typed_new(UCL_OBJECT); - for (i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { - ucl_object_insert_key(sub, - ucl_object_fromint(stat->actions_stat[i]), - rspamd_action_to_str(i), 0, false); - if (i < METRIC_ACTION_GREYLIST) { - spam += stat->actions_stat[i]; - } - else { - ham += stat->actions_stat[i]; - } - if (do_reset) { #ifndef HAVE_ATOMIC_BUILTINS - session->ctx->worker->srv->stat->actions_stat[i] = 0; + session->ctx->worker->srv->stat->actions_stat[i] = 0; #else - __atomic_store_n(&session->ctx->worker->srv->stat->actions_stat[i], - 0, __ATOMIC_RELEASE); + __atomic_store_n(&session->ctx->worker->srv->stat->actions_stat[i], + 0, __ATOMIC_RELEASE); #endif - } } - ucl_object_insert_key(top, sub, "actions", 0, false); } - ucl_object_insert_key(top, ucl_object_fromint(spam), "spam_count", 0, false); - ucl_object_insert_key(top, ucl_object_fromint(ham), "ham_count", 0, false); - ucl_object_insert_key(top, - ucl_object_fromint(stat->connections_count), "connections", 0, false); - ucl_object_insert_key(top, - ucl_object_fromint(stat->control_connections_count), - "control_connections", 0, false); - - ucl_object_insert_key(top, - ucl_object_fromint(mem_st.pools_allocated), "pools_allocated", 0, - false); - ucl_object_insert_key(top, - ucl_object_fromint(mem_st.pools_freed), "pools_freed", 0, false); - ucl_object_insert_key(top, - ucl_object_fromint(mem_st.bytes_allocated), "bytes_allocated", 0, - false); - ucl_object_insert_key(top, - ucl_object_fromint( - mem_st.chunks_allocated), - "chunks_allocated", 0, false); - ucl_object_insert_key(top, - ucl_object_fromint(mem_st.shared_chunks_allocated), - "shared_chunks_allocated", 0, false); - ucl_object_insert_key(top, - ucl_object_fromint(mem_st.chunks_freed), "chunks_freed", 0, false); - ucl_object_insert_key(top, - ucl_object_fromint( - mem_st.oversized_chunks), - "chunks_oversized", 0, false); - ucl_object_insert_key(top, - ucl_object_fromint(mem_st.fragmented_size), "fragmented", 0, false); - if (do_reset) { session->ctx->srv->stat->messages_scanned = 0; session->ctx->srv->stat->messages_learned = 0; diff --git a/src/libserver/worker_util.c b/src/libserver/worker_util.c index 02b0cd5ae..3e8133a16 100644 --- a/src/libserver/worker_util.c +++ b/src/libserver/worker_util.c @@ -2354,3 +2354,71 @@ rspamd_worker_check_and_adjust_timeout(struct rspamd_config *cfg, double timeout /* TODO: maybe adjust timeout */ return timeout; } + +ucl_object_t * +rspamd_worker_metrics_object(struct rspamd_config *cfg, struct rspamd_stat *stat, ev_tstamp uptime) +{ + rspamd_mempool_stat_t mem_st; + memset(&mem_st, 0, sizeof(mem_st)); + rspamd_mempool_stat(&mem_st); + + ucl_object_t *top = ucl_object_typed_new(UCL_OBJECT); + + ucl_object_insert_key(top, ucl_object_fromstring(RVERSION), "version", 0, false); + ucl_object_insert_key(top, ucl_object_fromstring(cfg->checksum), "config_id", 0, false); + ucl_object_insert_key(top, ucl_object_fromdouble(uptime), "uptime", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_scanned), "scanned", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(stat->messages_learned), "learned", 0, false); + + unsigned spam = 0, ham = 0; + + if (stat->messages_scanned > 0) { + ucl_object_t *sub = ucl_object_typed_new(UCL_OBJECT); + for (int i = METRIC_ACTION_REJECT; i <= METRIC_ACTION_NOACTION; i++) { + ucl_object_insert_key(sub, + ucl_object_fromint(stat->actions_stat[i]), + rspamd_action_to_str(i), 0, false); + if (i < METRIC_ACTION_GREYLIST) { + spam += stat->actions_stat[i]; + } + else { + ham += stat->actions_stat[i]; + } + } + ucl_object_insert_key(top, sub, "actions", 0, false); + } + + ucl_object_insert_key(top, ucl_object_fromint(spam), "spam_count", 0, false); + ucl_object_insert_key(top, ucl_object_fromint(ham), "ham_count", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->connections_count), "connections", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(stat->control_connections_count), + "control_connections", 0, false); + + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.pools_allocated), "pools_allocated", 0, + false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.pools_freed), "pools_freed", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.bytes_allocated), "bytes_allocated", 0, + false); + ucl_object_insert_key(top, + ucl_object_fromint( + mem_st.chunks_allocated), + "chunks_allocated", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.shared_chunks_allocated), + "shared_chunks_allocated", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.chunks_freed), "chunks_freed", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint( + mem_st.oversized_chunks), + "chunks_oversized", 0, false); + ucl_object_insert_key(top, + ucl_object_fromint(mem_st.fragmented_size), "fragmented", 0, false); + + return top; +}
\ No newline at end of file diff --git a/src/libserver/worker_util.h b/src/libserver/worker_util.h index e3627092d..1be7e0180 100644 --- a/src/libserver/worker_util.h +++ b/src/libserver/worker_util.h @@ -1,11 +1,11 @@ -/*- - * Copyright 2016 Vsevolod Stakhov +/* + * Copyright 2024 Vsevolod Stakhov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -299,6 +299,11 @@ void rspamd_worker_init_controller(struct rspamd_worker *worker, void rspamd_controller_store_saved_stats(struct rspamd_main *rspamd_main, struct rspamd_config *cfg); +/** + * Get metrics object for a worker + */ +ucl_object_t *rspamd_worker_metrics_object(struct rspamd_config *cfg, struct rspamd_stat *stat, ev_tstamp uptime); + #ifdef WITH_HYPERSCAN struct rspamd_control_command; |