haproxy/addons/otel/include/util.h

105 lines
4.1 KiB
C
Raw Permalink Normal View History

MEDIUM: otel: added configuration and utility layer Added the configuration structures that model the OTel filter's instrumentation hierarchy and the utility functions that support the configuration parser. The configuration is organized as a tree rooted at flt_otel_conf, which holds the proxy reference, filter identity, and lists of groups and scopes. Below it, flt_otel_conf_instr carries the instrumentation settings: tracer handle, rate limiting, hard-error mode, logging state, channel analyzers, and placeholder references to groups and scopes. Groups (flt_otel_conf_group) aggregate scopes by name. Scopes (flt_otel_conf_scope) bind an event to its ACL condition, span context declarations, span definitions and a list of spans scheduled for finishing. Spans (flt_otel_conf_span) carry attributes, events, baggages and status entries, each represented as flt_otel_conf_sample structures that pair a key with concatenated sample-expression arguments. All configuration types share a common header macro (FLT_OTEL_CONF_HDR) that embeds an identifier string, its length, a configuration line number, and a list link. Their init and free functions are generated by the FLT_OTEL_CONF_FUNC_INIT and FLT_OTEL_CONF_FUNC_FREE macros in conf_funcs.h, with per-type custom initialization and cleanup bodies. The utility layer in util.c provides argument counting and concatenation for the configuration parser, sample data to string conversion covering boolean, integer, IPv4, IPv6, string and HTTP method types, and debug helpers for dumping argument arrays and linked list state.
2026-04-12 01:30:57 -04:00
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#ifndef _OTEL_UTIL_H_
#define _OTEL_UTIL_H_
#define FLT_OTEL_HTTP_METH_DEFINES \
FLT_OTEL_HTTP_METH_DEF(OPTIONS) \
FLT_OTEL_HTTP_METH_DEF(GET) \
FLT_OTEL_HTTP_METH_DEF(HEAD) \
FLT_OTEL_HTTP_METH_DEF(POST) \
FLT_OTEL_HTTP_METH_DEF(PUT) \
FLT_OTEL_HTTP_METH_DEF(DELETE) \
FLT_OTEL_HTTP_METH_DEF(TRACE) \
FLT_OTEL_HTTP_METH_DEF(CONNECT)
MEDIUM: otel: added CLI commands for runtime filter management Added HAProxy CLI commands that allow runtime inspection and modification of OTel filter settings without requiring a configuration reload. The new cli.c module registers CLI keywords under the "otel" prefix and implements the following commands: flt_otel_cli_parse_status() displays a comprehensive status report of all OTel filter instances including filter ID, proxy, disabled state, hard-error mode, logging state, rate limit, analyzer bits, and SDK diagnostic message count; flt_otel_cli_parse_disabled() enables or disables filtering across all instances; flt_otel_cli_parse_option() toggles the hard-error mode that controls whether errors disable the filter for a stream or are silently ignored; flt_otel_cli_parse_logging() manages the logging state with support for off, on, and dontlog-normal modes; flt_otel_cli_parse_rate() adjusts the sampling rate limit as a floating-point percentage; and flt_otel_cli_parse_debug() sets the debug verbosity level in debug builds. All modifications are applied atomically across every OTel filter instance in every proxy. The CLI initialization is called from flt_otel_ops_init() during filter startup via flt_otel_cli_init(), which registers the keyword table through cli_register_kw(). Supporting changes include the FLT_OTEL_U32_FLOAT macro for converting the internal uint32_t rate representation to a human-readable percentage, the FLT_OTEL_PROXIES_LIST_START/END iteration macros for traversing all OTel filter instances across the proxy list, and flt_otel_filters_dump() for debug logging of filter instances.
2026-04-12 05:31:30 -04:00
/* Iterate over all OTel filter configurations across all proxies. */
#define FLT_OTEL_PROXIES_LIST_START() \
do { \
struct flt_conf *fconf; \
struct proxy *px; \
\
for (px = proxies_list; px != NULL; px = px->next) \
list_for_each_entry(fconf, &(px->filter_configs), list) \
if (fconf->id == otel_flt_id) { \
struct flt_otel_conf *conf = fconf->conf;
#define FLT_OTEL_PROXIES_LIST_END() \
} \
} while (0)
MEDIUM: otel: added configuration and utility layer Added the configuration structures that model the OTel filter's instrumentation hierarchy and the utility functions that support the configuration parser. The configuration is organized as a tree rooted at flt_otel_conf, which holds the proxy reference, filter identity, and lists of groups and scopes. Below it, flt_otel_conf_instr carries the instrumentation settings: tracer handle, rate limiting, hard-error mode, logging state, channel analyzers, and placeholder references to groups and scopes. Groups (flt_otel_conf_group) aggregate scopes by name. Scopes (flt_otel_conf_scope) bind an event to its ACL condition, span context declarations, span definitions and a list of spans scheduled for finishing. Spans (flt_otel_conf_span) carry attributes, events, baggages and status entries, each represented as flt_otel_conf_sample structures that pair a key with concatenated sample-expression arguments. All configuration types share a common header macro (FLT_OTEL_CONF_HDR) that embeds an identifier string, its length, a configuration line number, and a list link. Their init and free functions are generated by the FLT_OTEL_CONF_FUNC_INIT and FLT_OTEL_CONF_FUNC_FREE macros in conf_funcs.h, with per-type custom initialization and cleanup bodies. The utility layer in util.c provides argument counting and concatenation for the configuration parser, sample data to string conversion covering boolean, integer, IPv4, IPv6, string and HTTP method types, and debug helpers for dumping argument arrays and linked list state.
2026-04-12 01:30:57 -04:00
#ifdef DEBUG_OTEL
# define FLT_OTEL_ARGS_DUMP() do { if (otelc_dbg_level & (1 << OTELC_DBG_LEVEL_LOG)) flt_otel_args_dump((const char **)args); } while (0)
#else
# define FLT_OTEL_ARGS_DUMP() while (0)
#endif
MEDIUM: otel: added CLI commands for runtime filter management Added HAProxy CLI commands that allow runtime inspection and modification of OTel filter settings without requiring a configuration reload. The new cli.c module registers CLI keywords under the "otel" prefix and implements the following commands: flt_otel_cli_parse_status() displays a comprehensive status report of all OTel filter instances including filter ID, proxy, disabled state, hard-error mode, logging state, rate limit, analyzer bits, and SDK diagnostic message count; flt_otel_cli_parse_disabled() enables or disables filtering across all instances; flt_otel_cli_parse_option() toggles the hard-error mode that controls whether errors disable the filter for a stream or are silently ignored; flt_otel_cli_parse_logging() manages the logging state with support for off, on, and dontlog-normal modes; flt_otel_cli_parse_rate() adjusts the sampling rate limit as a floating-point percentage; and flt_otel_cli_parse_debug() sets the debug verbosity level in debug builds. All modifications are applied atomically across every OTel filter instance in every proxy. The CLI initialization is called from flt_otel_ops_init() during filter startup via flt_otel_cli_init(), which registers the keyword table through cli_register_kw(). Supporting changes include the FLT_OTEL_U32_FLOAT macro for converting the internal uint32_t rate representation to a human-readable percentage, the FLT_OTEL_PROXIES_LIST_START/END iteration macros for traversing all OTel filter instances across the proxy list, and flt_otel_filters_dump() for debug logging of filter instances.
2026-04-12 05:31:30 -04:00
#ifndef DEBUG_OTEL
# define flt_otel_filters_dump() while (0)
#else
MEDIUM: otel: added configuration and utility layer Added the configuration structures that model the OTel filter's instrumentation hierarchy and the utility functions that support the configuration parser. The configuration is organized as a tree rooted at flt_otel_conf, which holds the proxy reference, filter identity, and lists of groups and scopes. Below it, flt_otel_conf_instr carries the instrumentation settings: tracer handle, rate limiting, hard-error mode, logging state, channel analyzers, and placeholder references to groups and scopes. Groups (flt_otel_conf_group) aggregate scopes by name. Scopes (flt_otel_conf_scope) bind an event to its ACL condition, span context declarations, span definitions and a list of spans scheduled for finishing. Spans (flt_otel_conf_span) carry attributes, events, baggages and status entries, each represented as flt_otel_conf_sample structures that pair a key with concatenated sample-expression arguments. All configuration types share a common header macro (FLT_OTEL_CONF_HDR) that embeds an identifier string, its length, a configuration line number, and a list link. Their init and free functions are generated by the FLT_OTEL_CONF_FUNC_INIT and FLT_OTEL_CONF_FUNC_FREE macros in conf_funcs.h, with per-type custom initialization and cleanup bodies. The utility layer in util.c provides argument counting and concatenation for the configuration parser, sample data to string conversion covering boolean, integer, IPv4, IPv6, string and HTTP method types, and debug helpers for dumping argument arrays and linked list state.
2026-04-12 01:30:57 -04:00
/* Dump configuration arguments for debugging. */
void flt_otel_args_dump(const char **args);
MEDIUM: otel: added CLI commands for runtime filter management Added HAProxy CLI commands that allow runtime inspection and modification of OTel filter settings without requiring a configuration reload. The new cli.c module registers CLI keywords under the "otel" prefix and implements the following commands: flt_otel_cli_parse_status() displays a comprehensive status report of all OTel filter instances including filter ID, proxy, disabled state, hard-error mode, logging state, rate limit, analyzer bits, and SDK diagnostic message count; flt_otel_cli_parse_disabled() enables or disables filtering across all instances; flt_otel_cli_parse_option() toggles the hard-error mode that controls whether errors disable the filter for a stream or are silently ignored; flt_otel_cli_parse_logging() manages the logging state with support for off, on, and dontlog-normal modes; flt_otel_cli_parse_rate() adjusts the sampling rate limit as a floating-point percentage; and flt_otel_cli_parse_debug() sets the debug verbosity level in debug builds. All modifications are applied atomically across every OTel filter instance in every proxy. The CLI initialization is called from flt_otel_ops_init() during filter startup via flt_otel_cli_init(), which registers the keyword table through cli_register_kw(). Supporting changes include the FLT_OTEL_U32_FLOAT macro for converting the internal uint32_t rate representation to a human-readable percentage, the FLT_OTEL_PROXIES_LIST_START/END iteration macros for traversing all OTel filter instances across the proxy list, and flt_otel_filters_dump() for debug logging of filter instances.
2026-04-12 05:31:30 -04:00
/* Dump all OTel filter configurations across all proxies. */
void flt_otel_filters_dump(void);
MEDIUM: otel: implemented filter callbacks and event dispatcher Replaced the stub filter callbacks with full implementations that dispatch OTel events through the scope execution engine, and added the supporting debug, error handling and utility infrastructure. The filter lifecycle callbacks (init, deinit, init_per_thread) now initialize the OpenTelemetry C wrapper library, create the tracer from the instrumentation configuration file, enable HTX stream filtering, and clean up the configuration and memory pools on shutdown. The stream callbacks (attach, stream_start, stream_set_backend, stream_stop, detach, check_timeouts) create the per-stream runtime context on attach with rate-limit based sampling, fire the corresponding OTel events (on-stream-start, on-backend-set, on-stream-stop), manage the idle timeout timer with reschedule logic in detach, and free the runtime context in check_timeouts. The attach callback also registers the required pre and post channel analyzers from the instrumentation configuration. The channel callbacks (start_analyze, pre_analyze, post_analyze, end_analyze) register per-channel analyzers, map analyzer bits to event indices via flt_otel_get_event(), and dispatch the matching events. The end_analyze callback also fires the on-server-unavailable event when response analyzers were configured but never executed. The HTTP callbacks (http_headers, http_end, http_reply, and the debug-only http_payload and http_reset) dispatch their respective request/response events based on the channel direction. The event dispatcher flt_otel_event_run() in event.c iterates over all scopes matching a given event index and calls flt_otel_scope_run() for each, sharing a common monotonic and wall-clock timestamp across all spans within a single event. Error handling is centralized in flt_otel_return_int() and flt_otel_return_void(), which implement the hard-error/soft-error policy: hard errors disable the filter for the stream, soft errors are silently cleared. The new debug.h header provides conditional debug macros (FLT_OTEL_DBG_ARGS, FLT_OTEL_DBG_BUF) and the FLT_OTEL_LOG macro for structured logging through the instrumentation's log server list. The utility layer gained debug-only label functions for channel direction, proxy mode, stream position, filter type, and analyzer bit name lookups.
2026-04-12 04:30:03 -04:00
/* Return a label string identifying a channel direction. */
const char *flt_otel_chn_label(const struct channel *chn);
/* Return the proxy mode string for a stream. */
const char *flt_otel_pr_mode(const struct stream *s);
/* Return the stream processing position as a string. */
const char *flt_otel_stream_pos(const struct stream *s);
/* Return the filter type string for a filter instance. */
const char *flt_otel_type(const struct filter *f);
/* Return the analyzer name string for an analyzer bit. */
const char *flt_otel_analyzer(uint an_bit);
MEDIUM: otel: added configuration and utility layer Added the configuration structures that model the OTel filter's instrumentation hierarchy and the utility functions that support the configuration parser. The configuration is organized as a tree rooted at flt_otel_conf, which holds the proxy reference, filter identity, and lists of groups and scopes. Below it, flt_otel_conf_instr carries the instrumentation settings: tracer handle, rate limiting, hard-error mode, logging state, channel analyzers, and placeholder references to groups and scopes. Groups (flt_otel_conf_group) aggregate scopes by name. Scopes (flt_otel_conf_scope) bind an event to its ACL condition, span context declarations, span definitions and a list of spans scheduled for finishing. Spans (flt_otel_conf_span) carry attributes, events, baggages and status entries, each represented as flt_otel_conf_sample structures that pair a key with concatenated sample-expression arguments. All configuration types share a common header macro (FLT_OTEL_CONF_HDR) that embeds an identifier string, its length, a configuration line number, and a list link. Their init and free functions are generated by the FLT_OTEL_CONF_FUNC_INIT and FLT_OTEL_CONF_FUNC_FREE macros in conf_funcs.h, with per-type custom initialization and cleanup bodies. The utility layer in util.c provides argument counting and concatenation for the configuration parser, sample data to string conversion covering boolean, integer, IPv4, IPv6, string and HTTP method types, and debug helpers for dumping argument arrays and linked list state.
2026-04-12 01:30:57 -04:00
/* Dump a linked list of configuration items as a string. */
const char *flt_otel_list_dump(const struct list *head);
#endif
/* Count the number of non-NULL arguments in an argument array. */
int flt_otel_args_count(const char **args);
/* Concatenate argument array elements into a single string. */
int flt_otel_args_concat(const char **args, int idx, int n, char **str);
/* Comparator for qsort: ascending order of doubles with epsilon tolerance. */
int flt_otel_qsort_compar_double(const void *a, const void *b);
MEDIUM: otel: added configuration parser and event model Added the full configuration parser that reads the OTel filter's external configuration file and the event model that maps filter events to HAProxy channel analyzers. The event model in event.h defines an X-macro table (FLT_OTEL_EVENT_DEFINES) that maps each filter event to its HAProxy channel analyzer bit, sample fetch direction, and event name. Events cover stream lifecycle (start, stop, backend-set, idle-timeout), client and server sessions, request analyzers (frontend and backend TCP and HTTP inspection, switching rules, sticking rules, RDP cookie), response analyzers (TCP inspection, HTTP response processing), and HTTP headers, end, and reply callbacks. The event names are partially compatible with the SPOE filter. The flt_otel_event_data[] table in event.c is generated from the same X-macro and provides per-event metadata at runtime. The parser in parser.c implements section parsers for the three OTel configuration blocks: otel-instrumentation (tracer identity, log server, config file path, groups, scopes, ACLs, rate-limit, options for disabled/hard-errors/nolognorm, and debug-level), otel-group (group identity and scope list), and otel-scope (scope identity, span definitions with optional root/parent modifiers, attributes, events, baggages, status codes, inject/extract context operations, finish lists, idle-timeout, ACLs, and otel-event binding with optional if/unless ACL conditions). Each section has a post-parse callback that validates the parsed state. The top-level flt_otel_parse_cfg() temporarily registers these section parsers, loads the external configuration file via parse_cfg(), and handles deferred resolution of sample fetch arguments by saving them in conf->smp_args for later resolution in flt_otel_check() when full frontend and backend capabilities are available. The main flt_otel_parse() entry point was extended to parse the filter ID and config file keywords, verify that insecure-fork-wanted is enabled, and wire the parsed configuration into the flt_conf structure. The utility layer gained flt_otel_strtod() and flt_otel_strtoll() for validated string-to-number conversion used by rate-limit and debug-level parsing.
2026-04-12 02:41:03 -04:00
/* Parse a string to double with range validation. */
bool flt_otel_strtod(const char *nptr, double *value, double limit_min, double limit_max, char **err);
/* Parse a string to int64_t with range validation. */
bool flt_otel_strtoll(const char *nptr, int64_t *value, int64_t limit_min, int64_t limit_max, char **err);
MEDIUM: otel: added configuration and utility layer Added the configuration structures that model the OTel filter's instrumentation hierarchy and the utility functions that support the configuration parser. The configuration is organized as a tree rooted at flt_otel_conf, which holds the proxy reference, filter identity, and lists of groups and scopes. Below it, flt_otel_conf_instr carries the instrumentation settings: tracer handle, rate limiting, hard-error mode, logging state, channel analyzers, and placeholder references to groups and scopes. Groups (flt_otel_conf_group) aggregate scopes by name. Scopes (flt_otel_conf_scope) bind an event to its ACL condition, span context declarations, span definitions and a list of spans scheduled for finishing. Spans (flt_otel_conf_span) carry attributes, events, baggages and status entries, each represented as flt_otel_conf_sample structures that pair a key with concatenated sample-expression arguments. All configuration types share a common header macro (FLT_OTEL_CONF_HDR) that embeds an identifier string, its length, a configuration line number, and a list link. Their init and free functions are generated by the FLT_OTEL_CONF_FUNC_INIT and FLT_OTEL_CONF_FUNC_FREE macros in conf_funcs.h, with per-type custom initialization and cleanup bodies. The utility layer in util.c provides argument counting and concatenation for the configuration parser, sample data to string conversion covering boolean, integer, IPv4, IPv6, string and HTTP method types, and debug helpers for dumping argument arrays and linked list state.
2026-04-12 01:30:57 -04:00
/* Convert sample data to a string representation. */
int flt_otel_sample_to_str(const struct sample_data *data, char *value, size_t size, char **err);
MEDIUM: otel: implemented scope execution and span management Implemented the scope execution engine that creates OTel spans, evaluates sample expressions to collect telemetry data, and manages span lifecycle during request and response processing. The scope runner flt_otel_scope_run() was expanded from a stub into a complete implementation that evaluates ACL conditions on the scope, extracts span contexts from HTTP headers when configured, iterates over the scope's span definitions calling flt_otel_scope_run_span() for each, marks and finishes completed spans, and cleans up unused runtime resources. The span runner flt_otel_scope_run_span() creates OTel spans via the tracer with optional parent references (from other spans or extracted contexts), collects telemetry by calling flt_otel_sample_add() for each configured attribute, event, baggage and status entry, then applies the collected data to the span (attributes, events with their own key-value arrays, baggage items, and status code with description) and injects the span context into HTTP headers when configured. The sample evaluation layer converts HAProxy sample expressions into OTel telemetry data. flt_otel_sample_add() evaluates each sample expression against the stream, converts the result via flt_otel_sample_to_value() which preserves native types (booleans as OTELC_VALUE_BOOL, integers as OTELC_VALUE_INT64, all others as strings), and routes the key-value pair to the appropriate collector based on the sample type (attribute, event, baggage, or status). The key-value arrays grow dynamically using the FLT_OTEL_ATTR_INIT_SIZE and FLT_OTEL_ATTR_INC_SIZE constants. Span finishing is handled in two phases: flt_otel_scope_finish_mark() marks spans and contexts for completion using exact name matching or wildcards ("*" for all, "*req*" for request-direction, "*res*" for response-direction), and flt_otel_scope_finish_marked() ends all marked spans with a common monotonic timestamp and destroys their contexts.
2026-04-12 04:58:38 -04:00
/* Convert sample data to an OTel value. */
int flt_otel_sample_to_value(const char *key, const struct sample_data *data, struct otelc_value *value, char **err);
/* Add a key-value pair to a growable key-value array. */
int flt_otel_sample_add_kv(struct flt_otel_scope_data_kv *kv, const char *key, const struct otelc_value *value);
/* Evaluate a sample definition into an OTel value. */
int flt_otel_sample_eval(struct stream *s, uint dir, struct flt_otel_conf_sample *sample, bool flag_native, struct otelc_value *value, char **err);
MEDIUM: otel: implemented scope execution and span management Implemented the scope execution engine that creates OTel spans, evaluates sample expressions to collect telemetry data, and manages span lifecycle during request and response processing. The scope runner flt_otel_scope_run() was expanded from a stub into a complete implementation that evaluates ACL conditions on the scope, extracts span contexts from HTTP headers when configured, iterates over the scope's span definitions calling flt_otel_scope_run_span() for each, marks and finishes completed spans, and cleans up unused runtime resources. The span runner flt_otel_scope_run_span() creates OTel spans via the tracer with optional parent references (from other spans or extracted contexts), collects telemetry by calling flt_otel_sample_add() for each configured attribute, event, baggage and status entry, then applies the collected data to the span (attributes, events with their own key-value arrays, baggage items, and status code with description) and injects the span context into HTTP headers when configured. The sample evaluation layer converts HAProxy sample expressions into OTel telemetry data. flt_otel_sample_add() evaluates each sample expression against the stream, converts the result via flt_otel_sample_to_value() which preserves native types (booleans as OTELC_VALUE_BOOL, integers as OTELC_VALUE_INT64, all others as strings), and routes the key-value pair to the appropriate collector based on the sample type (attribute, event, baggage, or status). The key-value arrays grow dynamically using the FLT_OTEL_ATTR_INIT_SIZE and FLT_OTEL_ATTR_INC_SIZE constants. Span finishing is handled in two phases: flt_otel_scope_finish_mark() marks spans and contexts for completion using exact name matching or wildcards ("*" for all, "*req*" for request-direction, "*res*" for response-direction), and flt_otel_scope_finish_marked() ends all marked spans with a common monotonic timestamp and destroys their contexts.
2026-04-12 04:58:38 -04:00
/* Evaluate a sample expression and add the result to scope data. */
int flt_otel_sample_add(struct stream *s, uint dir, struct flt_otel_conf_sample *sample, struct flt_otel_scope_data *data, int type, char **err);
MEDIUM: otel: added configuration and utility layer Added the configuration structures that model the OTel filter's instrumentation hierarchy and the utility functions that support the configuration parser. The configuration is organized as a tree rooted at flt_otel_conf, which holds the proxy reference, filter identity, and lists of groups and scopes. Below it, flt_otel_conf_instr carries the instrumentation settings: tracer handle, rate limiting, hard-error mode, logging state, channel analyzers, and placeholder references to groups and scopes. Groups (flt_otel_conf_group) aggregate scopes by name. Scopes (flt_otel_conf_scope) bind an event to its ACL condition, span context declarations, span definitions and a list of spans scheduled for finishing. Spans (flt_otel_conf_span) carry attributes, events, baggages and status entries, each represented as flt_otel_conf_sample structures that pair a key with concatenated sample-expression arguments. All configuration types share a common header macro (FLT_OTEL_CONF_HDR) that embeds an identifier string, its length, a configuration line number, and a list link. Their init and free functions are generated by the FLT_OTEL_CONF_FUNC_INIT and FLT_OTEL_CONF_FUNC_FREE macros in conf_funcs.h, with per-type custom initialization and cleanup bodies. The utility layer in util.c provides argument counting and concatenation for the configuration parser, sample data to string conversion covering boolean, integer, IPv4, IPv6, string and HTTP method types, and debug helpers for dumping argument arrays and linked list state.
2026-04-12 01:30:57 -04:00
#endif /* _OTEL_UTIL_H_ */
/*
* Local variables:
* c-indent-level: 8
* c-basic-offset: 8
* End:
*
* vi: noexpandtab shiftwidth=8 tabstop=8
*/