CONTRIB: opentracing: add the OpenTracing filter

This commit adds the OpenTracing filter (hereinafter we will use the
abbreviated name 'the OT filter') to the contrib tree.

The OT filter adds native support for using distributed tracing in HAProxy.
This is enabled by sending an OpenTracing compliant request to one of the
supported tracers; such as Datadog, Jaeger, Lightstep and Zipkin tracers.
Please note: tracers are not listed by any preference, but alphabetically.

The OT filter is a standard HAProxy filter, so what applies to others also
applies to this one (of course, by that I mean what is described in the
documentation, more precisely in the doc/internals/filters.txt file).

The OT filter activation is done explicitly by specifying it in the HAProxy
configuration.  If this is not done, the OT filter in no way participates
in the work of HAProxy.

As for the impact on HAProxy speed, this is documented with several tests
located in the test directory, and the result is found in the README-speed-*
files.  In short, the speed of operation depends on the way it is used and
the complexity of the configuration, from an almost immeasurable impact to
a significant deceleration (5x and more).  I think that in some normal use
the speed of HAProxy with the filter on will be quite satisfactory with a
slowdown of less than 4%.

The OT filter allows intensive use of ACLs, which can be defined anywhere in
the configuration.  Thus, it is possible to use the filter only for those
connections that are of interest to us.

More detailed documentation related to the operation, configuration and use
of the filter can be found in the contrib/opentracing directory.

To make the OpenTracing filter easier to configure and compile, several
entries have been added to the Makefile.  When running the make utility,
it is possible to use several new arguments:

  USE_OT=1     : enable the OpenTracing filter
  OT_DEBUG=1   : compile the OpenTracing filter in debug mode
  OT_INC=path  : force the include path to libopentracing-c-wrapper
  OT_LIB=path  : force the lib path to libopentracing-c-wrapper
  OT_RUNPATH=1 : add libopentracing-c-wrapper RUNPATH to haproxy executable

If USE_OT is set, then an additional Makefile from the contrib/opentracing
directory is included in the compilation process.
diff --git a/contrib/opentracing/src/cli.c b/contrib/opentracing/src/cli.c
new file mode 100644
index 0000000..529c687
--- /dev/null
+++ b/contrib/opentracing/src/cli.c
@@ -0,0 +1,395 @@
+/***
+ * Copyright 2020 HAProxy Technologies
+ *
+ * This file is part of the HAProxy OpenTracing filter.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+#include "include.h"
+
+
+/***
+ * NAME
+ *   flt_ot_cli_set_msg -
+ *
+ * ARGUMENTS
+ *   appctx    -
+ *   err       -
+ *   msg       -
+ *   cli_state -
+ *
+ * DESCRIPTION
+ *   -
+ *
+ * RETURN VALUE
+ *   This function does not return a value.
+ */
+static void cmn_cli_set_msg(struct appctx *appctx, char *err, char *msg, int cli_state)
+{
+	FLT_OT_FUNC("%p, %p, %p, %d", appctx, err, msg, cli_state);
+
+	if ((appctx == NULL) || ((err == NULL) && (msg == NULL)))
+		FLT_OT_RETURN();
+
+	appctx->ctx.cli.err = (err == NULL) ? msg : err;
+	appctx->st0         = (appctx->ctx.cli.err == NULL) ? CLI_ST_PROMPT : cli_state;
+
+	FLT_OT_DBG(1, "err(%d): \"%s\"", appctx->st0, appctx->ctx.cli.err);
+
+	FLT_OT_RETURN();
+}
+
+
+#ifdef DEBUG_OT
+
+/***
+ * NAME
+ *   flt_ot_cli_parse_debug -
+ *
+ * ARGUMENTS
+ *   args    -
+ *   payload -
+ *   appctx  -
+ *   private -
+ *
+ * DESCRIPTION
+ *   -
+ *
+ * RETURN VALUE
+ *   -
+ */
+static int flt_ot_cli_parse_debug(char **args, char *payload, struct appctx *appctx, void *private)
+{
+	char    *err = NULL, *msg = NULL;
+	uint8_t  value;
+	int      retval = 0;
+
+	FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
+
+	FLT_OT_ARGS_DUMP();
+
+	if (FLT_OT_ARG_ISVALID(2)) {
+		value = flt_ot_strtoll(args[2], 0, 255, &err);
+		if (err == NULL) {
+			_HA_ATOMIC_STORE(&(flt_ot_debug.level), value);
+
+			(void)memprintf(&msg, FLT_OT_CLI_CMD " : debug level set to %hhu", value);
+		} else {
+			retval = 1;
+		}
+	} else {
+		value = _HA_ATOMIC_LOAD(&(flt_ot_debug.level));
+
+		(void)memprintf(&msg, FLT_OT_CLI_CMD " : current debug level is %hhu", value);
+	}
+
+	cmn_cli_set_msg(appctx, err, msg, CLI_ST_PRINT_FREE);
+
+	FLT_OT_RETURN(retval);
+}
+
+#endif /* DEBUG_OT */
+
+
+/***
+ * NAME
+ *   flt_ot_cli_parse_disabled -
+ *
+ * ARGUMENTS
+ *   args    -
+ *   payload -
+ *   appctx  -
+ *   private -
+ *
+ * DESCRIPTION
+ *   -
+ *
+ * RETURN VALUE
+ *   -
+ */
+static int flt_ot_cli_parse_disabled(char **args, char *payload, struct appctx *appctx, void *private)
+{
+	char *msg = NULL;
+	bool  value = (uintptr_t)private;
+	int   retval = 0;
+
+	FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
+
+	FLT_OT_ARGS_DUMP();
+
+	FLT_OT_PROXIES_LIST_START() {
+		_HA_ATOMIC_STORE(&(conf->tracer->flag_disabled), value);
+
+		(void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : filter %sabled", FLT_OT_CLI_MSG_CAT(msg), value ? "dis" : "en");
+	} FLT_OT_PROXIES_LIST_END();
+
+	cmn_cli_set_msg(appctx, NULL, msg, CLI_ST_PRINT_FREE);
+
+	FLT_OT_RETURN(retval);
+}
+
+
+/***
+ * NAME
+ *   flt_ot_cli_parse_option -
+ *
+ * ARGUMENTS
+ *   args    -
+ *   payload -
+ *   appctx  -
+ *   private -
+ *
+ * DESCRIPTION
+ *   -
+ *
+ * RETURN VALUE
+ *   -
+ */
+static int flt_ot_cli_parse_option(char **args, char *payload, struct appctx *appctx, void *private)
+{
+	char *msg = NULL;
+	bool  value = (uintptr_t)private;
+	int   retval = 0;
+
+	FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
+
+	FLT_OT_ARGS_DUMP();
+
+	FLT_OT_PROXIES_LIST_START() {
+		_HA_ATOMIC_STORE(&(conf->tracer->flag_harderr), value);
+
+		(void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : filter set %s-errors", FLT_OT_CLI_MSG_CAT(msg), value ? "hard" : "soft");
+	} FLT_OT_PROXIES_LIST_END();
+
+	cmn_cli_set_msg(appctx, NULL, msg, CLI_ST_PRINT_FREE);
+
+	FLT_OT_RETURN(retval);
+}
+
+
+/***
+ * NAME
+ *   flt_ot_cli_parse_logging -
+ *
+ * ARGUMENTS
+ *   args    -
+ *   payload -
+ *   appctx  -
+ *   private -
+ *
+ * DESCRIPTION
+ *   -
+ *
+ * RETURN VALUE
+ *   -
+ */
+static int flt_ot_cli_parse_logging(char **args, char *payload, struct appctx *appctx, void *private)
+{
+	char    *err = NULL, *msg = NULL;
+	uint8_t  value;
+	int      retval = 0;
+
+	FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
+
+	FLT_OT_ARGS_DUMP();
+
+	if (FLT_OT_ARG_ISVALID(2)) {
+		if (strcasecmp(args[2], FLT_OT_CLI_LOGGING_OFF) == 0) {
+			value = FLT_OT_LOGGING_OFF;
+		}
+		else if (strcasecmp(args[2], FLT_OT_CLI_LOGGING_ON) == 0) {
+			value = FLT_OT_LOGGING_ON;
+		}
+		else if (strcasecmp(args[2], FLT_OT_CLI_LOGGING_NOLOGNORM) == 0) {
+			value = FLT_OT_LOGGING_ON | FLT_OT_LOGGING_NOLOGNORM;
+		}
+		else {
+			(void)memprintf(&err, "'%s' : invalid value, use <" FLT_OT_CLI_LOGGING_OFF " | " FLT_OT_CLI_LOGGING_ON " | " FLT_OT_CLI_LOGGING_NOLOGNORM ">", args[2]);
+
+			retval = 1;
+		}
+
+		if (retval == 0) {
+			FLT_OT_PROXIES_LIST_START() {
+				_HA_ATOMIC_STORE(&(conf->tracer->logging), value);
+
+				(void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : logging is %s", FLT_OT_CLI_MSG_CAT(msg), FLT_OT_CLI_LOGGING_STATE(value));
+			} FLT_OT_PROXIES_LIST_END();
+		}
+	} else {
+		FLT_OT_PROXIES_LIST_START() {
+			value = _HA_ATOMIC_LOAD(&(conf->tracer->logging));
+
+			(void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : logging is currently %s", FLT_OT_CLI_MSG_CAT(msg), FLT_OT_CLI_LOGGING_STATE(value));
+		} FLT_OT_PROXIES_LIST_END();
+	}
+
+	cmn_cli_set_msg(appctx, err, msg, CLI_ST_PRINT_FREE);
+
+	FLT_OT_RETURN(retval);
+}
+
+
+/***
+ * NAME
+ *   flt_ot_cli_parse_rate -
+ *
+ * ARGUMENTS
+ *   args    -
+ *   payload -
+ *   appctx  -
+ *   private -
+ *
+ * DESCRIPTION
+ *   -
+ *
+ * RETURN VALUE
+ *   -
+ */
+static int flt_ot_cli_parse_rate(char **args, char *payload, struct appctx *appctx, void *private)
+{
+	char     *err = NULL, *msg = NULL;
+	uint32_t  value;
+	int       retval = 0;
+
+	FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
+
+	FLT_OT_ARGS_DUMP();
+
+	if (FLT_OT_ARG_ISVALID(2)) {
+		value = FLT_OT_FLOAT_U32(flt_ot_strtod(args[2], 0.0, FLT_OT_RATE_LIMIT_MAX, &err), FLT_OT_RATE_LIMIT_MAX);
+		if (err == NULL) {
+			FLT_OT_PROXIES_LIST_START() {
+				_HA_ATOMIC_STORE(&(conf->tracer->rate_limit), value);
+
+				(void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : rate limit set to %.2f", FLT_OT_CLI_MSG_CAT(msg), FLT_OT_U32_FLOAT(value, FLT_OT_RATE_LIMIT_MAX));
+			} FLT_OT_PROXIES_LIST_END();
+		} else {
+			retval = 1;
+		}
+	} else {
+		FLT_OT_PROXIES_LIST_START() {
+			value = _HA_ATOMIC_LOAD(&(conf->tracer->rate_limit));
+
+			(void)memprintf(&msg, "%s%s" FLT_OT_CLI_CMD " : current rate limit is %.2f", FLT_OT_CLI_MSG_CAT(msg), FLT_OT_U32_FLOAT(value, FLT_OT_RATE_LIMIT_MAX));
+		} FLT_OT_PROXIES_LIST_END();
+	}
+
+	cmn_cli_set_msg(appctx, err, msg, CLI_ST_PRINT_FREE);
+
+	FLT_OT_RETURN(retval);
+}
+
+
+/***
+ * NAME
+ *   flt_ot_cli_parse_status -
+ *
+ * ARGUMENTS
+ *   args    -
+ *   payload -
+ *   appctx  -
+ *   private -
+ *
+ * DESCRIPTION
+ *   -
+ *
+ * RETURN VALUE
+ *   -
+ */
+static int flt_ot_cli_parse_status(char **args, char *payload, struct appctx *appctx, void *private)
+{
+	const char *nl = "";
+	char       *msg = NULL;
+	int         retval = 0;
+
+	FLT_OT_FUNC("%p, \"%s\", %p, %p", args, payload, appctx, private);
+
+	FLT_OT_ARGS_DUMP();
+	flt_ot_filters_dump();
+
+	(void)memprintf(&msg, " " FLT_OT_OPT_NAME " filter status\n" FLT_OT_STR_DASH_78);
+#ifdef DEBUG_OT
+	(void)memprintf(&msg, "%s\n   debug level: 0x%02hhx\n", msg, flt_ot_debug.level);
+#endif
+
+	FLT_OT_PROXIES_LIST_START() {
+		(void)memprintf(&msg, "%s\n%s   filter %s\n", msg, nl, conf->id);
+		(void)memprintf(&msg, "%s     configuration: %s\n", msg, conf->cfg_file);
+		(void)memprintf(&msg, "%s     disable count: %" PRIu64 " %" PRIu64 "\n\n", msg, conf->cnt.disabled[0], conf->cnt.disabled[1]);
+		(void)memprintf(&msg, "%s     tracer %s\n", msg, conf->tracer->id);
+		(void)memprintf(&msg, "%s       configuration: %s\n", msg, conf->tracer->config);
+		(void)memprintf(&msg, "%s       plugin:        %s\n", msg, conf->tracer->plugin);
+		(void)memprintf(&msg, "%s       rate limit:    %.2f %%\n", msg, FLT_OT_U32_FLOAT(conf->tracer->rate_limit, FLT_OT_RATE_LIMIT_MAX));
+		(void)memprintf(&msg, "%s       hard errors:   %s\n", msg, FLT_OT_STR_FLAG_YN(conf->tracer->flag_harderr));
+		(void)memprintf(&msg, "%s       disabled:      %s\n", msg, FLT_OT_STR_FLAG_YN(conf->tracer->flag_disabled));
+		(void)memprintf(&msg, "%s       logging:       %s\n", msg, FLT_OT_CLI_LOGGING_STATE(conf->tracer->logging));
+		(void)memprintf(&msg, "%s       analyzers:     %08x", msg, conf->tracer->analyzers);
+
+		nl = "\n";
+	} FLT_OT_PROXIES_LIST_END();
+
+	cmn_cli_set_msg(appctx, NULL, msg, CLI_ST_PRINT_FREE);
+
+	FLT_OT_RETURN(retval);
+}
+
+
+static struct cli_kw_list cli_kws = { { }, {
+#ifdef DEBUG_OT
+	{ { FLT_OT_CLI_CMD, "debug", NULL }, FLT_OT_CLI_CMD " debug [level]   : set the OT filter debug level (default: get current debug level)", flt_ot_cli_parse_debug, NULL, NULL, NULL, 0 },
+#endif
+	{ { FLT_OT_CLI_CMD, "disable", NULL }, FLT_OT_CLI_CMD " disable         : disable the OT filter", flt_ot_cli_parse_disabled, NULL, NULL, (void *)1, 0 },
+	{ { FLT_OT_CLI_CMD, "enable", NULL }, FLT_OT_CLI_CMD " enable          : enable the OT filter", flt_ot_cli_parse_disabled, NULL, NULL, (void *)0, 0 },
+	{ { FLT_OT_CLI_CMD, "soft-errors", NULL }, FLT_OT_CLI_CMD " soft-errors     : turning off hard-errors mode", flt_ot_cli_parse_option, NULL, NULL, (void *)0, 0 },
+	{ { FLT_OT_CLI_CMD, "hard-errors", NULL }, FLT_OT_CLI_CMD " hard-errors     : enabling hard-errors mode", flt_ot_cli_parse_option, NULL, NULL, (void *)1, 0 },
+	{ { FLT_OT_CLI_CMD, "logging",  NULL }, FLT_OT_CLI_CMD " logging [state] : set logging state (default: get current logging state)", flt_ot_cli_parse_logging, NULL, NULL, NULL, 0 },
+	{ { FLT_OT_CLI_CMD, "rate", NULL }, FLT_OT_CLI_CMD " rate [value]    : set the rate limit (default: get current rate value)", flt_ot_cli_parse_rate, NULL, NULL, NULL, 0 },
+	{ { FLT_OT_CLI_CMD, "status", NULL }, FLT_OT_CLI_CMD " status          : show the OT filter status", flt_ot_cli_parse_status, NULL, NULL, NULL, 0 },
+	{ /* END */ }
+}};
+
+
+/***
+ * NAME
+ *   flt_ot_cli_init -
+ *
+ * ARGUMENTS
+ *   This function takes no arguments.
+ *
+ * DESCRIPTION
+ *   -
+ *
+ * RETURN VALUE
+ *   This function does not return a value.
+ */
+void flt_ot_cli_init(void)
+{
+	FLT_OT_FUNC("");
+
+	/* Register CLI keywords. */
+	cli_register_kw(&cli_kws);
+
+	FLT_OT_RETURN();
+}
+
+/*
+ * Local variables:
+ *  c-indent-level: 8
+ *  c-basic-offset: 8
+ * End:
+ *
+ * vi: noexpandtab shiftwidth=8 tabstop=8
+ */