]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: counters: retrieve detailed errmsg upon failure with counters_{fe,be}_shared_p...
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 27 Aug 2025 12:51:48 +0000 (14:51 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Wed, 3 Sep 2025 13:59:17 +0000 (15:59 +0200)
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

include/haproxy/counters.h
src/counters.c
src/proxy.c
src/server.c

index 803e8ec41dc633df4315bbfaf9877f71eb02f8f9..2484ece68a8cd5dc76f709e775c2331be44cbe03 100644 (file)
@@ -27,8 +27,8 @@
 #include <haproxy/counters-t.h>
 #include <haproxy/guid-t.h>
 
-int counters_fe_shared_prepare(struct fe_counters_shared *counters, const struct guid_node *guid);
-int counters_be_shared_prepare(struct be_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, char **errmsg);
 
 void counters_fe_shared_drop(struct fe_counters_shared *counters);
 void counters_be_shared_drop(struct be_counters_shared *counters);
index a7a1a864d5cf0d8b115ee5fdc9d9a09fece0dfc8..6f480b2a945b2166b6ca688cd0ae43dc49ee8960 100644 (file)
@@ -24,6 +24,7 @@
 #include <haproxy/counters.h>
 #include <haproxy/global.h>
 #include <haproxy/time.h>
+#include <haproxy/tools.h>
 
 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
  * <size> hint is expected to reflect the actual tg member size (fe/be)
  * 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;
 
@@ -69,6 +72,7 @@ static int _counters_shared_prepare(struct counters_shared *shared, const struct
        while (it < global.nbtgroups) {
                shared->tg[it] = calloc(1, size);
                if (!shared->tg[it]) {
+                       memprintf(errmsg, "memory error, calloc failed");
                        _counters_shared_drop(shared);
                        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 */
-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 */
-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);
 }
index 05a91355df44e21868ce38f9356e26309354cbdf..b2de70bdea877408c1ea45b6ee3fec0d08e0852e 100644 (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)
 {
        struct listener *listener;
+       char *errmsg = NULL;
        int err_code = ERR_NONE;
 
        /* 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()
         */
        if (px->cap & PR_CAP_FE) {
-               if (!counters_fe_shared_prepare(&px->fe_counters.shared, &px->guid)) {
-                       ha_alert("out of memory while setting up shared counters for %s %s\n",
-                                proxy_type_str(px), px->id);
+               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 : %s\n",
+                                proxy_type_str(px), px->id, errmsg);
+                       ha_free(&errmsg);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        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
                 * capability
                 */
-               if (!counters_be_shared_prepare(&px->be_counters.shared, &px->guid)) {
-                       ha_alert("out of memory while setting up shared counters for %s %s\n",
-                                proxy_type_str(px), px->id);
+               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 : %s\n",
+                                proxy_type_str(px), px->id, errmsg);
+                       ha_free(&errmsg);
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
                }
@@ -1780,10 +1783,11 @@ static int proxy_postcheck(struct proxy *px)
 
        list_for_each_entry(listener, &px->conf.listeners, by_fe) {
                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_alert("out of memory while setting up shared listener counters for %s %s\n",
-                                        proxy_type_str(px), px->id);
+                               ha_alert("out of memory while setting up shared listener counters for %s %s : %s\n",
+                                        proxy_type_str(px), px->id, errmsg);
+                               ha_free(&errmsg);
                                err_code |= ERR_ALERT | ERR_FATAL;
                                goto out;
                        }
index cfdf9cdf2151b2d3163c42f2993729c099dc10a7..01ef6dcc039c8d9d003b67c8f81251bf0c8599da 100644 (file)
@@ -3466,14 +3466,16 @@ static int init_srv_slowstart(struct server *srv);
 int srv_postinit(struct server *srv)
 {
        int err_code = ERR_NONE;
+       char *errmsg = NULL;
 
        err_code |= _srv_check_proxy_mode(srv, 1);
 
        if (err_code & ERR_CODE)
                goto out;
 
-       if (!counters_be_shared_prepare(&srv->counters.shared, &srv->guid)) {
-               ha_alert("memory error while setting up shared counters for %s/%s server\n", srv->proxy->id, srv->id);
+       if (!counters_be_shared_prepare(&srv->counters.shared, &srv->guid, &errmsg)) {
+               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;
                goto out;
        }