]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: stats: hide px/sv/li fields in applet struct
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 2 Oct 2020 16:32:02 +0000 (18:32 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 5 Oct 2020 08:48:58 +0000 (10:48 +0200)
Use an opaque pointer to store proxy instance. Regroup server/listener
as a single opaque pointer. This has the benefit to render the structure
more evolutive to support statistics on other types of objects in the
future.

This patch is needed to extend stat support for components other than
proxies objects.

The prometheus module has been adapted for these changes.

contrib/prometheus-exporter/service-prometheus.c
include/haproxy/applet-t.h
src/stats.c

index 027626dd3fb46cc454192bbfcc3020f9e59c7e14..98f6b174c5e584584bb021df649f2d153017ea43 100644 (file)
@@ -1272,8 +1272,8 @@ static int promex_dump_metric(struct appctx *appctx, struct htx *htx,
                        goto full;
        }
        else {
-               struct proxy *px = appctx->ctx.stats.px;
-               struct server *srv = appctx->ctx.stats.sv;
+               struct proxy *px = appctx->ctx.stats.obj1;
+               struct server *srv = appctx->ctx.stats.obj2;
                const struct ist label = promex_st_metric_labels[appctx->st2];
 
                if (istcat(out, name, max) == -1 ||
@@ -1542,8 +1542,8 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
        int ret = 1;
 
        while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
-               while (appctx->ctx.stats.px) {
-                       px = appctx->ctx.stats.px;
+               while (appctx->ctx.stats.obj1) {
+                       px = appctx->ctx.stats.obj1;
 
                        /* skip the disabled proxies, global frontend and non-networked ones */
                        if (px->state == PR_STSTOPPED || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
@@ -1692,11 +1692,11 @@ static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
                        if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
                                goto full;
                  next_px:
-                       appctx->ctx.stats.px = px->next;
+                       appctx->ctx.stats.obj1 = px->next;
                }
          next_metric:
                appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
-               appctx->ctx.stats.px = proxies_list;
+               appctx->ctx.stats.obj1 = proxies_list;
                appctx->st2 = promex_front_metrics[appctx->st2];
        }
 
@@ -1727,8 +1727,8 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
        double secs;
 
        while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
-               while (appctx->ctx.stats.px) {
-                       px = appctx->ctx.stats.px;
+               while (appctx->ctx.stats.obj1) {
+                       px = appctx->ctx.stats.obj1;
 
                        /* skip the disabled proxies, global frontend and non-networked ones */
                        if (px->state == PR_STSTOPPED || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
@@ -1936,11 +1936,11 @@ static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
                        if (!promex_dump_metric(appctx, htx, prefix, &metric, &out, max))
                                goto full;
                  next_px:
-                       appctx->ctx.stats.px = px->next;
+                       appctx->ctx.stats.obj1 = px->next;
                }
          next_metric:
                appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
-               appctx->ctx.stats.px = proxies_list;
+               appctx->ctx.stats.obj1 = proxies_list;
                appctx->st2 = promex_back_metrics[appctx->st2];
        }
 
@@ -1972,15 +1972,15 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
        double secs;
 
        while (appctx->st2 && appctx->st2 < ST_F_TOTAL_FIELDS) {
-               while (appctx->ctx.stats.px) {
-                       px = appctx->ctx.stats.px;
+               while (appctx->ctx.stats.obj1) {
+                       px = appctx->ctx.stats.obj1;
 
                        /* skip the disabled proxies, global frontend and non-networked ones */
                        if (px->state == PR_STSTOPPED || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
                                goto next_px;
 
-                       while (appctx->ctx.stats.sv) {
-                               sv = appctx->ctx.stats.sv;
+                       while (appctx->ctx.stats.obj2) {
+                               sv = appctx->ctx.stats.obj2;
 
                                if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
                                        goto next_sv;
@@ -2192,17 +2192,17 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
                                        goto full;
 
                          next_sv:
-                               appctx->ctx.stats.sv = sv->next;
+                               appctx->ctx.stats.obj2 = sv->next;
                        }
 
                  next_px:
-                       appctx->ctx.stats.px = px->next;
-                       appctx->ctx.stats.sv = (appctx->ctx.stats.px ? appctx->ctx.stats.px->srv : NULL);
+                       appctx->ctx.stats.obj1 = px->next;
+                       appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
                }
          next_metric:
                appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
-               appctx->ctx.stats.px = proxies_list;
-               appctx->ctx.stats.sv = (appctx->ctx.stats.px ? appctx->ctx.stats.px->srv : NULL);
+               appctx->ctx.stats.obj1 = proxies_list;
+               appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
                appctx->st2 = promex_srv_metrics[appctx->st2];
        }
 
@@ -2221,15 +2221,17 @@ static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
 
 /* Dump all metrics (global, frontends, backends and servers) depending on the
  * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
- * -1 in case of any error. */
+ * -1 in case of any error.
+ * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
+ * a pointer to the current server/listener. */
 static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
 {
        int ret;
 
        switch (appctx->st1) {
                case PROMEX_DUMPER_INIT:
-                       appctx->ctx.stats.px = NULL;
-                       appctx->ctx.stats.sv = NULL;
+                       appctx->ctx.stats.obj1 = NULL;
+                       appctx->ctx.stats.obj2 = NULL;
                        appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
                        appctx->st2 = promex_global_metrics[INF_NAME];
                        appctx->st1 = PROMEX_DUMPER_GLOBAL;
@@ -2245,8 +2247,8 @@ static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *s
                                }
                        }
 
-                       appctx->ctx.stats.px = proxies_list;
-                       appctx->ctx.stats.sv = NULL;
+                       appctx->ctx.stats.obj1 = proxies_list;
+                       appctx->ctx.stats.obj2 = NULL;
                        appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
                        appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STATS_METRIC);
                        appctx->st2 = promex_front_metrics[ST_F_PXNAME];
@@ -2263,8 +2265,8 @@ static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *s
                                }
                        }
 
-                       appctx->ctx.stats.px = proxies_list;
-                       appctx->ctx.stats.sv = NULL;
+                       appctx->ctx.stats.obj1 = proxies_list;
+                       appctx->ctx.stats.obj2 = NULL;
                        appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
                        appctx->st2 = promex_back_metrics[ST_F_PXNAME];
                        appctx->st1 = PROMEX_DUMPER_BACK;
@@ -2280,8 +2282,8 @@ static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *s
                                }
                        }
 
-                       appctx->ctx.stats.px = proxies_list;
-                       appctx->ctx.stats.sv = (appctx->ctx.stats.px ? appctx->ctx.stats.px->srv : NULL);
+                       appctx->ctx.stats.obj1 = proxies_list;
+                       appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
                        appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
                        appctx->st2 = promex_srv_metrics[ST_F_PXNAME];
                        appctx->st1 = PROMEX_DUMPER_SRV;
@@ -2297,8 +2299,8 @@ static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *s
                                }
                        }
 
-                       appctx->ctx.stats.px = NULL;
-                       appctx->ctx.stats.sv = NULL;
+                       appctx->ctx.stats.obj1 = NULL;
+                       appctx->ctx.stats.obj2 = NULL;
                        appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC|PROMEX_FL_STATS_METRIC);
                        appctx->st2 = 0;
                        appctx->st1 = PROMEX_DUMPER_DONE;
@@ -2316,8 +2318,8 @@ static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *s
        return 0;
   error:
        /* unrecoverable error */
-       appctx->ctx.stats.px = NULL;
-       appctx->ctx.stats.sv = NULL;
+       appctx->ctx.stats.obj1 = NULL;
+       appctx->ctx.stats.obj2 = NULL;
        appctx->ctx.stats.flags = 0;
        appctx->st2 = 0;
        appctx->st1 = PROMEX_DUMPER_DONE;
index 60f30c56f081e0b446beb1fc39d12bf08909e6eb..7c97f191764c5819b8915b9801f33c6877522974 100644 (file)
@@ -118,9 +118,8 @@ struct appctx {
                 * keep the grouped together and avoid adding new ones.
                 */
                struct {
-                       struct proxy *px;
-                       struct server *sv;
-                       void *l;
+                       void *obj1;             /* context pointer used in stats dump */
+                       void *obj2;             /* context pointer used in stats dump */
                        int scope_str;          /* limit scope to a frontend/backend substring */
                        int scope_len;          /* length of the string above in the buffer */
                        int px_st;              /* STAT_PX_ST* */
index 6ba3f62cde98107c36572acff87d0794ee133624..4334c5860636c0e0703f6fbd097c084a55b17504 100644 (file)
@@ -2212,13 +2212,13 @@ int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
                                goto full;
                }
 
-               appctx->ctx.stats.l = px->conf.listeners.n;
+               appctx->ctx.stats.obj2 = px->conf.listeners.n;
                appctx->ctx.stats.px_st = STAT_PX_ST_LI;
                /* fall through */
 
        case STAT_PX_ST_LI:
-               /* stats.l has been initialized above */
-               for (; appctx->ctx.stats.l != &px->conf.listeners; appctx->ctx.stats.l = l->by_fe.n) {
+               /* obj2 points to listeners list as initialized above */
+               for (; appctx->ctx.stats.obj2 != &px->conf.listeners; appctx->ctx.stats.obj2 = l->by_fe.n) {
                        if (htx) {
                                if (htx_almost_full(htx))
                                        goto full;
@@ -2228,7 +2228,7 @@ int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
                                        goto full;
                        }
 
-                       l = LIST_ELEM(appctx->ctx.stats.l, struct listener *, by_fe);
+                       l = LIST_ELEM(appctx->ctx.stats.obj2, struct listener *, by_fe);
                        if (!l->counters)
                                continue;
 
@@ -2247,13 +2247,13 @@ int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
                        }
                }
 
-               appctx->ctx.stats.sv = px->srv; /* may be NULL */
+               appctx->ctx.stats.obj2 = px->srv; /* may be NULL */
                appctx->ctx.stats.px_st = STAT_PX_ST_SV;
                /* fall through */
 
        case STAT_PX_ST_SV:
-               /* stats.sv has been initialized above */
-               for (; appctx->ctx.stats.sv != NULL; appctx->ctx.stats.sv = sv->next) {
+               /* obj2 points to servers list as initialized above */
+               for (; appctx->ctx.stats.obj2 != NULL; appctx->ctx.stats.obj2 = sv->next) {
                        if (htx) {
                                if (htx_almost_full(htx))
                                        goto full;
@@ -2263,7 +2263,7 @@ int stats_dump_proxy_to_buffer(struct stream_interface *si, struct htx *htx,
                                        goto full;
                        }
 
-                       sv = appctx->ctx.stats.sv;
+                       sv = appctx->ctx.stats.obj2;
 
                        if (appctx->ctx.stats.flags & STAT_BOUND) {
                                if (!(appctx->ctx.stats.type & (1 << STATS_TYPE_SV)))
@@ -2776,14 +2776,14 @@ static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *ht
                                goto full;
                }
 
-               appctx->ctx.stats.px = proxies_list;
+               appctx->ctx.stats.obj1 = proxies_list;
                appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
                appctx->st2 = STAT_ST_LIST;
                /* fall through */
 
        case STAT_ST_LIST:
                /* dump proxies */
-               while (appctx->ctx.stats.px) {
+               while (appctx->ctx.stats.obj1) {
                        if (htx) {
                                if (htx_almost_full(htx))
                                        goto full;
@@ -2793,13 +2793,13 @@ static int stats_dump_stat_to_buffer(struct stream_interface *si, struct htx *ht
                                        goto full;
                        }
 
-                       px = appctx->ctx.stats.px;
+                       px = appctx->ctx.stats.obj1;
                        /* skip the disabled proxies, global frontend and non-networked ones */
                        if (px->state != PR_STSTOPPED && px->uuid > 0 && (px->cap & (PR_CAP_FE | PR_CAP_BE)))
                                if (stats_dump_proxy_to_buffer(si, htx, px, uri) == 0)
                                        return 0;
 
-                       appctx->ctx.stats.px = px->next;
+                       appctx->ctx.stats.obj1 = px->next;
                        appctx->ctx.stats.px_st = STAT_PX_ST_INIT;
                }
                /* here, we just have reached the last proxy */