The flt_ot_smp_init() function has been added to make initializing the
sample structure easier.  The contents of the structure in question are
set in several places in the source of the OpenTracing filter, so it is
better to do this in one place.

--
Miroslav Zagorac
Senior Developer
>From 9015197281886afb6d8d23c0bbe539b63a5709c5 Mon Sep 17 00:00:00 2001
From: Miroslav Zagorac <mzago...@haproxy.com>
Date: Tue, 1 Mar 2022 18:41:36 +0100
Subject: [PATCH 05/16] CLEANUP: opentracing: added flt_ot_smp_init() function

The flt_ot_smp_init() function has been added to make initializing the
sample structure easier.  The contents of the structure in question are
set in several places in the source of the OpenTracing filter, so it is
better to do this in one place.
---
 addons/ot/src/vars.c | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/addons/ot/src/vars.c b/addons/ot/src/vars.c
index 82b089b82..fa2a0bd06 100644
--- a/addons/ot/src/vars.c
+++ b/addons/ot/src/vars.c
@@ -83,6 +83,36 @@ void flt_ot_vars_dump(struct stream *s)
 #endif /* DEBUG_OT */
 
 
+/***
+ * NAME
+ *   flt_ot_smp_init -
+ *
+ * ARGUMENTS
+ *   s    -
+ *   smp  -
+ *   opt  -
+ *   type -
+ *   data -
+ *
+ * DESCRIPTION
+ *   -
+ *
+ * RETURN VALUE
+ *   This function does not return a value.
+ */
+static inline void flt_ot_smp_init(struct stream *s, struct sample *smp, uint opt, int type, const char *data)
+{
+	(void)memset(smp, 0, sizeof(*smp));
+	(void)smp_set_owner(smp, s->be, s->sess, s, opt | SMP_OPT_FINAL);
+
+	if (data != NULL) {
+		smp->data.type = type;
+
+		chunk_initstr(&(smp->data.u.str), data);
+	}
+}
+
+
 /***
  * NAME
  *   flt_ot_get_vars -
@@ -300,11 +330,7 @@ int flt_ot_var_set(struct stream *s, const char *scope, const char *prefix, cons
 	if (retval == -1)
 		FLT_OT_RETURN(retval);
 
-	(void)memset(&smp, 0, sizeof(smp));
-	(void)smp_set_owner(&smp, s->be, s->sess, s, opt | SMP_OPT_FINAL);
-	smp.data.type       = SMP_T_STR;
-	smp.data.u.str.area = (char *)value;
-	smp.data.u.str.data = strlen(value);
+	flt_ot_smp_init(s, &smp, opt, SMP_T_STR, value);
 
 	vars_set_by_name_ifexist(var_name, retval, &smp);
 
@@ -364,10 +390,8 @@ int flt_ot_vars_unset(struct stream *s, const char *scope, const char *prefix, u
 
 			FLT_OT_DBG(2, "- '%s' -> '%.*s'", var_name, (int)var->data.u.str.data, var->data.u.str.area);
 
-			(void)memset(&smp, 0, sizeof(smp));
-			(void)smp_set_owner(&smp, s->be, s->sess, s, opt | SMP_OPT_FINAL);
-
-			size = var_clear(var);
+			size = var_clear(var, 1);
+			flt_ot_smp_init(s, &smp, opt, 0, NULL);
 			var_accounting_diff(vars, smp.sess, smp.strm, -size);
 
 			retval++;
-- 
2.30.2

Reply via email to