Hello!

On Sun, Oct 26, 2025 at 04:50:35AM +0900, Hyeonggeun Oh wrote:
> Hello HAProxy Org.
> 
> I'm about to work on implementing dump_all_vars() from issue #1623 as my
> first contribution to HAProxy. (
> https://github.com/haproxy/haproxy/issues/1623)
> Since this is my first work and a "good first issue", I'd like to confirm
> the implementation approach before submitting patches.

Thanks for checking, indeed!

> Proposed Implementation:
> 
> Function signature:
>   dump_all_vars([scope],[prefix])
> 
> Arguments:
>   - scope (optional): variable scope(sess, txn, req, res, proc)
>   - prefix (optional): filter variables by name prefix
> 
> Output format:
>   varl=var1, var2=var2, ...
>   (string values will be quoted: name="value")

Yes, looks good. We'll also need to escape quotes from strings so that
quote characters embedded in strings do not break out of the variable,
e.g. a quote character (") will turn to backslash-quote (\") inside the
quoted string, and the backslash will also have to be escaped (\\). Please
look at the escape_string() function which already does this.

> Implementation Plan:
>   - Location: src/vars.c
>   - Start with basic types: int, str, bool

I think there won't be any reason for not dealing with all types at
once. In fact, there are cast mechanisms that make a variable appear
as a string (e.g. when used in expressions such as %[var(txn.foo)],
and all types are already convertible there. Look for example in
log.c:4081. We're processing a log-format expression such as above,
whose output is retrieved in "key" from sample_process(), and we
want to convert it to text. It could very well be the exact contents
of the variable, which is also a sample. Here the conversion to text
is performed in-place using sample_convert() a few lines below.
Logically, any type of variable can be passed into sample_convert()
and be turned into a string suitable for use, without having to check
the types since they're all supported.

>   - Iterate through variables list and filter by scope/prefix

Yes! However you won't be able to benefit from the tree here, because
while it used to index variables by their names (which could have been
looked up by prefix), it now only indexes the hash of their names. It
shouldn't be too much of a deal since there are not that many variables
in each context.

>   - Format output as comma-separated key=value pairs

yes, taking care of escaping " and \ for STR and placing quotes around.

>   - Leave room for future format extension (e.g., JSON)

Ah yes I forgot about that one. Indeed, once the conversion is done
like above, there's not much to change to get a JSON block on output.

> Example Usage:
>   # Dump all txn variables
>   http-request return string %[dump_all_vars(txn)]
> 
>   # Dump only variables starting with "counter"
>   http-request return string %[dump_all_vars(txn,counter)]
> 
> Any concerns or suggestions before I proceed?

No concern, that sems to perfectly match the use case. Please note
that a common use case will be to place them in a header block. In
this case a few other characters have to be encoded for strings:
(\r, \n, \b, \0).

Thanks!
Willy


Reply via email to