BUG/MEDIUM: opentracing: initialization before establishing daemon and/or chroot mode
This patch solves the problem reported in github issue #1204, where the
OpenTracing filter cannot communicate with the selected tracer if HAProxy
is run in daemon mode.
This commit also solves github issue #1274, where the problem manifests
itself when using the 'chroot' keyword in the HAProxy configuration.
This is solved so that the initialization of the OpenTracing plugin is
split into two operations, first the plugin (dynamic library) is loaded
before switching the HAProxy to daemon mode (or chroot) and then the
tracer thread is started.
This means that nothing is retrieved from the file system in runtime.
After applying this commit, opentracing C wrapper version 1.1.0 should be
used because the earlier version does not have separated initialization
functions.
This resolves GitHub issues #1204 and #1274.
(cherry picked from commit 9425ed488f4d31db07f0c9b1e8b2f0fb65b18c7b)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/addons/ot/src/conf.c b/addons/ot/src/conf.c
index 71db967..d07be2c 100644
--- a/addons/ot/src/conf.c
+++ b/addons/ot/src/conf.c
@@ -671,6 +671,7 @@
FLT_OT_FREE((*ptr)->id);
FLT_OT_FREE((*ptr)->config);
+ FLT_OT_FREE((*ptr)->cfgbuf);
FLT_OT_FREE((*ptr)->plugin);
FLT_OT_DBG(2, "- deleting acls list %s", flt_ot_list_debug(&((*ptr)->acls)));
list_for_each_entry_safe(acl, aclback, &((*ptr)->acls), list) {
diff --git a/addons/ot/src/filter.c b/addons/ot/src/filter.c
index 6699d46..04c4a67 100644
--- a/addons/ot/src/filter.c
+++ b/addons/ot/src/filter.c
@@ -168,11 +168,10 @@
/*
* Initialize the OpenTracing library.
- * Enable HTX streams filtering.
*/
- retval = ot_init(&(conf->tracer->tracer), conf->tracer->config, conf->tracer->plugin, &err);
+ retval = ot_init(&(conf->tracer->tracer), conf->tracer->plugin, &err);
if (retval != FLT_OT_RET_ERROR)
- fconf->flags |= FLT_CFG_FL_HTX;
+ /* Do nothing. */;
else if (err != NULL) {
FLT_OT_ALERT("%s", err);
@@ -426,8 +425,6 @@
}
-#ifdef DEBUG_OT
-
/***
* NAME
* flt_ot_init_per_thread -
@@ -446,14 +443,38 @@
*/
static int flt_ot_init_per_thread(struct proxy *p, struct flt_conf *fconf)
{
- int retval = FLT_OT_RET_OK;
+ struct flt_ot_conf *conf = FLT_OT_DEREF(fconf, conf, NULL);
+ char *err = NULL;
+ int retval = FLT_OT_RET_ERROR;
FLT_OT_FUNC("%p, %p", p, fconf);
+ if (conf == NULL)
+ FLT_OT_RETURN(retval);
+
+ /*
+ * Start the OpenTracing library tracer thread.
+ * Enable HTX streams filtering.
+ */
+ if (!(fconf->flags & FLT_CFG_FL_HTX)) {
+ retval = ot_start(conf->tracer->tracer, conf->tracer->cfgbuf, &err);
+ if (retval != FLT_OT_RET_ERROR)
+ fconf->flags |= FLT_CFG_FL_HTX;
+ else if (err != NULL) {
+ FLT_OT_ALERT("%s", err);
+
+ FLT_OT_ERR_FREE(err);
+ }
+ } else {
+ retval = FLT_OT_RET_OK;
+ }
+
FLT_OT_RETURN(retval);
}
+#ifdef DEBUG_OT
+
/***
* NAME
* flt_ot_deinit_per_thread -
@@ -1112,7 +1133,7 @@
.init = flt_ot_init,
.deinit = flt_ot_deinit,
.check = flt_ot_check,
- .init_per_thread = FLT_OT_DBG_IFDEF(flt_ot_init_per_thread, NULL),
+ .init_per_thread = flt_ot_init_per_thread,
.deinit_per_thread = FLT_OT_DBG_IFDEF(flt_ot_deinit_per_thread, NULL),
/* Stream callbacks. */
diff --git a/addons/ot/src/opentracing.c b/addons/ot/src/opentracing.c
index 58936d1..b79ba29 100644
--- a/addons/ot/src/opentracing.c
+++ b/addons/ot/src/opentracing.c
@@ -138,7 +138,6 @@
*
* ARGUMENTS
* tracer -
- * config -
* plugin -
* err -
*
@@ -148,12 +147,12 @@
* RETURN VALUE
* -
*/
-int ot_init(struct otc_tracer **tracer, const char *config, const char *plugin, char **err)
+int ot_init(struct otc_tracer **tracer, const char *plugin, char **err)
{
char cwd[PATH_MAX], path[PATH_MAX], errbuf[BUFSIZ] = "";
int rc, retval = -1;
- FLT_OT_FUNC("%p:%p \"%s\", \"%s\", %p:%p", FLT_OT_DPTR_ARGS(tracer), config, plugin, FLT_OT_DPTR_ARGS(err));
+ FLT_OT_FUNC("%p:%p, \"%s\", %p:%p", FLT_OT_DPTR_ARGS(tracer), plugin, FLT_OT_DPTR_ARGS(err));
flt_ot_pools_info();
#ifdef USE_POOL_OT_SPAN_CONTEXT
@@ -172,7 +171,7 @@
FLT_OT_RETURN(retval);
}
- *tracer = otc_tracer_init(path, config, NULL, errbuf, sizeof(errbuf));
+ *tracer = otc_tracer_load(path, errbuf, sizeof(errbuf));
if (*tracer == NULL) {
FLT_OT_ERR("%s", (*errbuf == '\0') ? "failed to initialize tracing library" : errbuf);
} else {
@@ -187,6 +186,39 @@
/***
* NAME
+ * ot_start -
+ *
+ * ARGUMENTS
+ * tracer -
+ * cfgbuf -
+ * err -
+ *
+ * DESCRIPTION
+ * -
+ *
+ * RETURN VALUE
+ * This function does not return a value.
+ */
+int ot_start(struct otc_tracer *tracer, const char *cfgbuf, char **err)
+{
+ char errbuf[BUFSIZ] = "";
+ int retval = -1;
+
+ FLT_OT_FUNC("%p, %p, %p:%p", tracer, cfgbuf, FLT_OT_DPTR_ARGS(err));
+
+ if (cfgbuf == NULL)
+ FLT_OT_RETURN(retval);
+
+ retval = otc_tracer_start(NULL, cfgbuf, errbuf, sizeof(errbuf));
+ if (retval == -1)
+ FLT_OT_ERR("%s", (*errbuf == '\0') ? "failed to start tracer" : errbuf);
+
+ FLT_OT_RETURN(retval);
+}
+
+
+/***
+ * NAME
* ot_close -
*
* ARGUMENTS
diff --git a/addons/ot/src/parser.c b/addons/ot/src/parser.c
index 5dec862..e26ca18 100644
--- a/addons/ot/src/parser.c
+++ b/addons/ot/src/parser.c
@@ -574,7 +574,8 @@
*/
static int flt_ot_post_parse_cfg_tracer(void)
{
- int retval = ERR_NONE;
+ char errbuf[BUFSIZ] = "";
+ int retval = ERR_NONE;
FLT_OT_FUNC("");
@@ -586,8 +587,13 @@
if (flt_ot_current_tracer->id == NULL)
FLT_OT_RETURN(retval);
- if (flt_ot_current_tracer->config == NULL)
+ if (flt_ot_current_tracer->config == NULL) {
FLT_OT_POST_PARSE_ALERT("tracer '%s' has no configuration file specified", flt_ot_current_tracer->cfg_line, flt_ot_current_tracer->id);
+ } else {
+ flt_ot_current_tracer->cfgbuf = otc_file_read(flt_ot_current_tracer->config, "#", errbuf, sizeof(errbuf));
+ if (flt_ot_current_tracer->cfgbuf == NULL)
+ FLT_OT_POST_PARSE_ALERT("tracer '%s' %s", flt_ot_current_tracer->cfg_line, flt_ot_current_tracer->id, (*errbuf == '\0') ? "cannot load configuration file" : errbuf);
+ }
if (flt_ot_current_tracer->plugin == NULL)
FLT_OT_POST_PARSE_ALERT("tracer '%s' has no plugin library specified", flt_ot_current_tracer->cfg_line, flt_ot_current_tracer->id);