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 and backend
side.
 From this patch it should be possible to remove most of the duplicate
code on prometheuse side for the server.

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


William,

Here too, some comments inlined.

[...]

-/* Fill <stats> with the server statistics. <stats> is
- * preallocated array of length <len>. The length of the array
- * must be at least ST_F_TOTAL_FIELDS. If this length is less
- * then this value, the function returns 0, otherwise, it
- * returns 1. <flags> can take the value STAT_SHLGNDS.
+/* Compute server state helper
   */
-int stats_fill_sv_stats(struct proxy *px, struct server *sv, int flags,
-                        struct field *stats, int len)
+static void stats_fill_sv_stats_computestate(struct server *sv, struct server 
*via,
+                                            struct server *ref, enum 
srv_stats_state *state)
  {

The "via" parameter is unused. No reason to pass it.

+/* Fill <stats> with the backend statistics. <stats> is preallocated array of
+ * length <len>. If <selected_field> 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 servers, the
+ * function returns 0, otherwise, it returns 1. <flags> can take the value
+ * STAT_SHLGNDS.
+ */
+int stats_fill_sv_stats(struct proxy *px, struct server *sv, 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;
+       struct server *via = sv->track ? sv->track : sv;
+       struct server *ref = via;
+       enum srv_stats_state state = 0;
+       char str[INET6_ADDRSTRLEN];
+       struct buffer *out = get_trash_chunk();
+       char *fld_status;
+       long long srv_samples_counter;
+       unsigned int srv_samples_window = TIME_STATS_SAMPLES;

Same comment than for stats_fill_b_stats. The metric variable must be 
initialized.

--
Christopher Faulet

Reply via email to