summaryrefslogtreecommitdiffstats
path: root/src/libserver
diff options
context:
space:
mode:
Diffstat (limited to 'src/libserver')
-rw-r--r--src/libserver/protocol.c30
-rw-r--r--src/libserver/protocol.h4
-rw-r--r--src/libserver/protocol_internal.h8
-rw-r--r--src/libserver/task.c2
-rw-r--r--src/libserver/task.h7
5 files changed, 40 insertions, 11 deletions
diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c
index c8c7bc76a..db83b0bfb 100644
--- a/src/libserver/protocol.c
+++ b/src/libserver/protocol.c
@@ -25,6 +25,7 @@
#include "unix-std.h"
#include "protocol_internal.h"
#include "libserver/mempool_vars_internal.h"
+#include "libserver/worker_util.h"
#include "contrib/fastutf8/fastutf8.h"
#include "task.h"
#include "lua/lua_classnames.h"
@@ -210,6 +211,19 @@ rspamd_protocol_handle_url(struct rspamd_task *task,
goto err;
}
break;
+ case 'M':
+ case 'm':
+ /* metrics, process */
+ if (COMPARE_CMD(p, MSG_CMD_METRICS, pathlen)) {
+ msg_debug_protocol("got metrics command");
+ task->cmd = CMD_METRICS;
+ task->flags |= RSPAMD_TASK_FLAG_SKIP;
+ task->processed_stages |= RSPAMD_TASK_STAGE_DONE; /* Skip all */
+ }
+ else {
+ goto err;
+ }
+ break;
default:
goto err;
}
@@ -2098,11 +2112,12 @@ void rspamd_protocol_write_log_pipe(struct rspamd_task *task)
g_array_free(extra, TRUE);
}
-void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout)
+void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout, struct rspamd_main *srv)
{
struct rspamd_http_message *msg;
const char *ctype = "application/json";
rspamd_fstring_t *reply;
+ ev_tstamp now = ev_time();
msg = rspamd_http_new_message(HTTP_RESPONSE);
@@ -2163,6 +2178,8 @@ void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout)
}
}
else {
+ rspamd_fstring_t *output;
+ struct rspamd_stat stat_copy;
msg->status = rspamd_fstring_new_init("OK", 2);
switch (task->cmd) {
@@ -2179,6 +2196,15 @@ void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout)
rspamd_http_message_set_body(msg, "pong" CRLF, 6);
ctype = "text/plain";
break;
+ case CMD_METRICS:
+ msg_debug_protocol("writing metrics to client");
+
+ memcpy(&stat_copy, srv->stat, sizeof(stat_copy));
+ output = rspamd_metrics_to_prometheus_string(
+ rspamd_worker_metrics_object(srv->cfg, &stat_copy, now - srv->start_time));
+ rspamd_http_message_set_body_from_fstring_steal(msg, output);
+ ctype = "application/openmetrics-text; version=1.0.0; charset=utf-8";
+ break;
default:
msg_err_protocol("BROKEN");
break;
@@ -2186,7 +2212,7 @@ void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout)
}
ev_now_update(task->event_loop);
- msg->date = ev_time();
+ msg->date = now;
rspamd_http_connection_reset(task->http_conn);
rspamd_http_connection_write_message(task->http_conn, msg, NULL,
diff --git a/src/libserver/protocol.h b/src/libserver/protocol.h
index 94fbcbf04..9d2b985da 100644
--- a/src/libserver/protocol.h
+++ b/src/libserver/protocol.h
@@ -51,7 +51,7 @@ struct rspamd_protocol_log_message_sum {
struct rspamd_protocol_log_symbol_result results[];
};
-struct rspamd_metric;
+struct rspamd_main;
/**
* Process headers into HTTP message and set appropriate task fields
@@ -126,7 +126,7 @@ ucl_object_t *rspamd_protocol_write_ucl(struct rspamd_task *task,
* @param task task object
* @return 0 if we wrote reply and -1 if there was some error
*/
-void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout);
+void rspamd_protocol_write_reply(struct rspamd_task *task, ev_tstamp timeout, struct rspamd_main *srv);
/**
* Convert rspamd output to legacy protocol reply
diff --git a/src/libserver/protocol_internal.h b/src/libserver/protocol_internal.h
index c604e9630..7a70ccef0 100644
--- a/src/libserver/protocol_internal.h
+++ b/src/libserver/protocol_internal.h
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2017 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,
@@ -55,6 +55,8 @@ extern "C" {
* Return a confirmation that spamd is alive
*/
#define MSG_CMD_PING "ping"
+
+#define MSG_CMD_METRICS "metrics"
/*
* Process this message as described above and return modified message
*/
diff --git a/src/libserver/task.c b/src/libserver/task.c
index 63babf990..637f401a9 100644
--- a/src/libserver/task.c
+++ b/src/libserver/task.c
@@ -133,7 +133,7 @@ rspamd_task_reply(struct rspamd_task *task)
}
else {
if (!(task->processed_stages & RSPAMD_TASK_STAGE_REPLIED)) {
- rspamd_protocol_write_reply(task, write_timeout);
+ rspamd_protocol_write_reply(task, write_timeout, task->worker->srv);
}
}
}
diff --git a/src/libserver/task.h b/src/libserver/task.h
index cba9bbbd4..7e6341a84 100644
--- a/src/libserver/task.h
+++ b/src/libserver/task.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,
@@ -36,6 +36,7 @@ enum rspamd_command {
CMD_CHECK_RSPAMC, /* Legacy rspamc format (like SA one) */
CMD_CHECK, /* Legacy check - metric json reply */
CMD_CHECK_V2, /* Modern check - symbols in json reply */
+ CMD_METRICS,
};
enum rspamd_task_stage {