]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Rework] Allow metrics endpoint to be enabled for each scanner
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 21 Jun 2024 13:43:14 +0000 (14:43 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 21 Jun 2024 13:43:14 +0000 (14:43 +0100)
src/controller.c
src/libserver/worker_util.c
src/libserver/worker_util.h

index 5526d9197f7ca80d5c960fe43457e4fada87460f..8ba12b605b4155781f328dec37506dc9330e7039 100644 (file)
@@ -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;
index 02b0cd5aeb310a12cfeaed160b11b06a93b32f81..3e8133a16300b1f90a231ec488a50c57425f0593 100644 (file)
@@ -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
index e3627092d0ad4e5b6368685e593fe75dcf2e903e..1be7e01809acc6173cd2aa1553044cddd1977915 100644 (file)
@@ -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;