MINOR: counters: retrieve detailed errmsg upon failure with counters_{fe,be}_shared_prepare()

counters_{fe,be}_shared_prepare now take an extra <errmsg> parameter
that contains additional hints about the error in case of failure.

It must be freed accordingly since it is allocated using memprintf
This commit is contained in:
Aurelien DARRAGON 2025-08-27 14:51:48 +02:00
parent 46463d6850
commit cb08bcb9d6
4 changed files with 29 additions and 19 deletions

View File

@ -27,8 +27,8 @@
#include <haproxy/counters-t.h> #include <haproxy/counters-t.h>
#include <haproxy/guid-t.h> #include <haproxy/guid-t.h>
int counters_fe_shared_prepare(struct fe_counters_shared *counters, const struct guid_node *guid); int counters_fe_shared_prepare(struct fe_counters_shared *counters, const struct guid_node *guid, char **errmsg);
int counters_be_shared_prepare(struct be_counters_shared *counters, const struct guid_node *guid); int counters_be_shared_prepare(struct be_counters_shared *counters, const struct guid_node *guid, char **errmsg);
void counters_fe_shared_drop(struct fe_counters_shared *counters); void counters_fe_shared_drop(struct fe_counters_shared *counters);
void counters_be_shared_drop(struct be_counters_shared *counters); void counters_be_shared_drop(struct be_counters_shared *counters);

View File

@ -24,6 +24,7 @@
#include <haproxy/counters.h> #include <haproxy/counters.h>
#include <haproxy/global.h> #include <haproxy/global.h>
#include <haproxy/time.h> #include <haproxy/time.h>
#include <haproxy/tools.h>
static void _counters_shared_drop(void *counters) static void _counters_shared_drop(void *counters)
{ {
@ -55,9 +56,11 @@ void counters_be_shared_drop(struct be_counters_shared *counters)
/* prepare shared counters pointer for a given <guid> object /* prepare shared counters pointer for a given <guid> object
* <size> hint is expected to reflect the actual tg member size (fe/be) * <size> hint is expected to reflect the actual tg member size (fe/be)
* if <guid> is not set, then sharing is disabled * if <guid> is not set, then sharing is disabled
* Returns the pointer on success or NULL on failure * Returns the pointer on success or NULL on failure, in which case
* <errmsg> will contain additional hints about the error and must be freed accordingly
*/ */
static int _counters_shared_prepare(struct counters_shared *shared, const struct guid_node *guid, size_t size) static int _counters_shared_prepare(struct counters_shared *shared,
const struct guid_node *guid, size_t size, char **errmsg)
{ {
int it = 0; int it = 0;
@ -69,6 +72,7 @@ static int _counters_shared_prepare(struct counters_shared *shared, const struct
while (it < global.nbtgroups) { while (it < global.nbtgroups) {
shared->tg[it] = calloc(1, size); shared->tg[it] = calloc(1, size);
if (!shared->tg[it]) { if (!shared->tg[it]) {
memprintf(errmsg, "memory error, calloc failed");
_counters_shared_drop(shared); _counters_shared_drop(shared);
return 0; return 0;
} }
@ -83,13 +87,13 @@ static int _counters_shared_prepare(struct counters_shared *shared, const struct
} }
/* prepare shared fe counters pointer for a given <guid> object */ /* prepare shared fe counters pointer for a given <guid> object */
int counters_fe_shared_prepare(struct fe_counters_shared *shared, const struct guid_node *guid) int counters_fe_shared_prepare(struct fe_counters_shared *shared, const struct guid_node *guid, char **errmsg)
{ {
return _counters_shared_prepare((struct counters_shared *)shared, guid, sizeof(struct fe_counters_shared_tg)); return _counters_shared_prepare((struct counters_shared *)shared, guid, sizeof(struct fe_counters_shared_tg), errmsg);
} }
/* prepare shared be counters pointer for a given <guid> object */ /* prepare shared be counters pointer for a given <guid> object */
int counters_be_shared_prepare(struct be_counters_shared *shared, const struct guid_node *guid) int counters_be_shared_prepare(struct be_counters_shared *shared, const struct guid_node *guid, char **errmsg)
{ {
return _counters_shared_prepare((struct counters_shared *)shared, guid, sizeof(struct be_counters_shared_tg)); return _counters_shared_prepare((struct counters_shared *)shared, guid, sizeof(struct be_counters_shared_tg), errmsg);
} }

View File

@ -1749,6 +1749,7 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg)
static int proxy_postcheck(struct proxy *px) static int proxy_postcheck(struct proxy *px)
{ {
struct listener *listener; struct listener *listener;
char *errmsg = NULL;
int err_code = ERR_NONE; int err_code = ERR_NONE;
/* allocate private memory for shared counters: used as a fallback /* allocate private memory for shared counters: used as a fallback
@ -1757,9 +1758,10 @@ static int proxy_postcheck(struct proxy *px)
* proxy postparsing, see proxy_postparse() * proxy postparsing, see proxy_postparse()
*/ */
if (px->cap & PR_CAP_FE) { if (px->cap & PR_CAP_FE) {
if (!counters_fe_shared_prepare(&px->fe_counters.shared, &px->guid)) { if (!counters_fe_shared_prepare(&px->fe_counters.shared, &px->guid, &errmsg)) {
ha_alert("out of memory while setting up shared counters for %s %s\n", ha_alert("out of memory while setting up shared counters for %s %s : %s\n",
proxy_type_str(px), px->id); proxy_type_str(px), px->id, errmsg);
ha_free(&errmsg);
err_code |= ERR_ALERT | ERR_FATAL; err_code |= ERR_ALERT | ERR_FATAL;
goto out; goto out;
} }
@ -1769,9 +1771,10 @@ static int proxy_postcheck(struct proxy *px)
* be_counters may be used even if the proxy lacks the backend * be_counters may be used even if the proxy lacks the backend
* capability * capability
*/ */
if (!counters_be_shared_prepare(&px->be_counters.shared, &px->guid)) { if (!counters_be_shared_prepare(&px->be_counters.shared, &px->guid, &errmsg)) {
ha_alert("out of memory while setting up shared counters for %s %s\n", ha_alert("out of memory while setting up shared counters for %s %s : %s\n",
proxy_type_str(px), px->id); proxy_type_str(px), px->id, errmsg);
ha_free(&errmsg);
err_code |= ERR_ALERT | ERR_FATAL; err_code |= ERR_ALERT | ERR_FATAL;
goto out; goto out;
} }
@ -1780,10 +1783,11 @@ static int proxy_postcheck(struct proxy *px)
list_for_each_entry(listener, &px->conf.listeners, by_fe) { list_for_each_entry(listener, &px->conf.listeners, by_fe) {
if (listener->counters) { if (listener->counters) {
if (!counters_fe_shared_prepare(&listener->counters->shared, &listener->guid)) { if (!counters_fe_shared_prepare(&listener->counters->shared, &listener->guid, &errmsg)) {
ha_free(&listener->counters); ha_free(&listener->counters);
ha_alert("out of memory while setting up shared listener counters for %s %s\n", ha_alert("out of memory while setting up shared listener counters for %s %s : %s\n",
proxy_type_str(px), px->id); proxy_type_str(px), px->id, errmsg);
ha_free(&errmsg);
err_code |= ERR_ALERT | ERR_FATAL; err_code |= ERR_ALERT | ERR_FATAL;
goto out; goto out;
} }

View File

@ -3466,14 +3466,16 @@ static int init_srv_slowstart(struct server *srv);
int srv_postinit(struct server *srv) int srv_postinit(struct server *srv)
{ {
int err_code = ERR_NONE; int err_code = ERR_NONE;
char *errmsg = NULL;
err_code |= _srv_check_proxy_mode(srv, 1); err_code |= _srv_check_proxy_mode(srv, 1);
if (err_code & ERR_CODE) if (err_code & ERR_CODE)
goto out; goto out;
if (!counters_be_shared_prepare(&srv->counters.shared, &srv->guid)) { if (!counters_be_shared_prepare(&srv->counters.shared, &srv->guid, &errmsg)) {
ha_alert("memory error while setting up shared counters for %s/%s server\n", srv->proxy->id, srv->id); ha_alert("memory error while setting up shared counters for %s/%s server : %s\n", srv->proxy->id, srv->id, errmsg);
ha_free(&errmsg);
err_code |= ERR_ALERT | ERR_FATAL; err_code |= ERR_ALERT | ERR_FATAL;
goto out; goto out;
} }