Re: [PATCH 3/6] MEDIUM: stats: allow to select one field in `stats_fill_be_stats`

2021-01-25 Thread William Dauchy
Hi Christopher,

Thanks for the review.

On Mon, Jan 25, 2021 at 4:26 PM Christopher Faulet  wrote:
> Here, you must take care to increment "*nbup" and "*nbsrv" in the for loop. 
> But
> changing it this way gcc then complains these variables are now unused. Thus 
> to
> make it happy, I must use local variables.

ah yes, I forgot the * indeed.
I did modify the code to use local variables but it somehow looks
uglier than before.
see in v2.

> Take a look at the commit 8596bfbaf ("BUG/MINOR: stats: Init the metric 
> variable
> when frontend stats are filled"). The metric variable must be initialized.

fixed in v2


--
William



Re: [PATCH 3/6] MEDIUM: stats: allow to select one field in `stats_fill_be_stats`

2021-01-25 Thread Christopher Faulet

Le 22/01/2021 à 21:09, William Dauchy a écrit :

prometheus approach requires to output all values for a given metric
name; meaning we iterate through all metrics, and then iterate in the
inner loop on all objects for this metric.
In order to allow more code reuse, adapt the stats API to be able to
select one field or fill them all otherwise.
This patch follows what has already been done on frontend side.
 From this patch it should be possible to remove most of the duplicate
code on prometheuse side for the backend

A few things to note though:
- status and uweight field requires prior compute, so I moved that to a
   sort of helper `stats_fill_be_stats_computesrv`.
- all ST_F*TIME fields requires some minor compute, so I moved it at te
   beginning of the function under a condition.



William,

Some comments inlined. I can handle the changes if you want by amending your 
patch. But I can wait a new version if you prefer to handle the changes 
yourself. Just let me know.


[...]


+/* Helper to compute srv values for a given backend
   */
-int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int 
len)
+static void stats_fill_be_stats_computesrv(struct proxy *px, int *nbup, int 
*nbsrv, int *totuw)
  {
-   long long be_samples_counter;
-   unsigned int be_samples_window = TIME_STATS_SAMPLES;
-   struct buffer *out = get_trash_chunk();
const struct server *srv;
-   int nbup, nbsrv;
-   int totuw;
-   char *fld;
-
-   if (len < ST_F_TOTAL_FIELDS)
-   return 0;
  
-	totuw = 0;

-   nbup = nbsrv = 0;
+   *nbup = *nbsrv = *totuw = 0;
for (srv = px->srv; srv; srv = srv->next) {
if (srv->cur_state != SRV_ST_STOPPED) {
nbup++;
if (srv_currently_usable(srv) &&
(!px->srv_act ^ !(srv->flags & SRV_F_BACKUP)))
-   totuw += srv->uweight;
+   *totuw += srv->uweight;
}
nbsrv++;
}
  
  	HA_RWLOCK_RDLOCK(LBPRM_LOCK, >lbprm.lock);

if (!px->srv_act && px->lbprm.fbck)
-   totuw = px->lbprm.fbck->uweight;
+   *totuw = px->lbprm.fbck->uweight;
HA_RWLOCK_RDUNLOCK(LBPRM_LOCK, >lbprm.lock);
+}
  


Here, you must take care to increment "*nbup" and "*nbsrv" in the for loop. But 
changing it this way gcc then complains these variables are now unused. Thus to 
make it happy, I must use local variables.


[...]


+/* Fill  with the backend statistics.  is preallocated array of
+ * length . If  is != NULL, only fill this one. The length
+ * of the array must be at least ST_F_TOTAL_FIELDS. If this length is less than
+ * this value, or if the selected field is not implemented for backends, the
+ * function returns 0, otherwise, it returns 1.  can take the value
+ * STAT_SHLGNDS.
+ */
+int stats_fill_be_stats(struct proxy *px, int flags, struct field *stats, int 
len,
+   enum stat_field *selected_field)
+{
+   enum stat_field current_field = (selected_field != NULL ? 
*selected_field : 0);
+   struct field metric;
+   long long be_samples_counter;
+   unsigned int be_samples_window = TIME_STATS_SAMPLES;
+   struct buffer *out = get_trash_chunk();
+   int nbup, nbsrv, totuw = 0;
+   char *fld;
  


Take a look at the commit 8596bfbaf ("BUG/MINOR: stats: Init the metric variable 
when frontend stats are filled"). The metric variable must be initialized.



--
Christopher Faulet