Support using using dynamic values for regexes in VRL
Requested by user in discord: https://discord.com/channels/742820443487993987/746070591097798688/829794652852977695
They'd like to match one field against the value of another for in the filter transform:
$ !match(.tags.__name__, .tags.keep)
error[E110]: invalid argument type
┌─ :1:23
│
1 │ match(.tags.__name__, .tags.keep)
│ ^^^^^^^^^^
│ │
│ this expression resolves to the exact type "string"
│ but the parameter "pattern" expects the exact type "regex"
│
= learn more about error code 110 at https://errors.vrl.dev/110
= see language documentation at https://vrl.dev
Here VRL complains about .tags.keep as it wants a regexp literal there. We should permit dynamic string values to be interpreted as a regex.
I think replace has this same restriction. We should survey other functions for opportunities to broaden support for using dynamic values as the regexp.
IMHO providing a function that converts "string" type to "regex" type would suffice.
There are some functions in regex cargo for rust such as https://docs.rs/regex/1.4.5/regex/struct.Regex.html#method.new and https://docs.rs/regex/1.0.0/regex/fn.escape.html that may help with this.
It should be noted that we deliberately avoided allowing dynamic regexes since compiling regexes is an expensive operation. Having to do so for every event could get costly and we wanted to avoid the user accidentally damaging their throughput. Currently all regexes are compiled up front at boot time.
A good compromise could be to create this to_regex function and document it very clearly that this is a slow function and should be avoided unless absolutely necessary. At least that way a user will have to go to extra effort in order to create a slow config.
It should be noted that we deliberately avoided allowing dynamic regexes since compiling regexes is an expensive operation. Having to do so for every event could get costly and we wanted to avoid the user accidentally damaging their throughput. Currently all regexes are compiled up front at boot time.
@FungusHumungus could the regexes be cached since for our intended use case the dynamically obtained regex strings would be the same for large amounts of data we would like to process? We just want to have a way to dynamically obtain a regex string but still apply the same regex to match and process large amounts of metrics.
Hey @jszwedko, @Mercury2699.
I was trying to use to_regex to parse the dynamic regex retrieved from annotations. But it only seems to work for match but not for parse_regex function. Though the parameter itself have the same type in both of the functions.
I tried saving the result into a separate variable and pass it as an argument, or resolve it on the fly, but it raises an error as it expects the pattern argument to be regex and not group or variable reference.
Any ideas how can I overcome this issue?
Here is the code snippet:
container_log_regex = get!(.kubernetes.annotations, ["test.com/" + string!(.kubernetes.container_name) + ".logs.regex"])
.message = parse_regex!(.message, to_regex(container_log_regex) ?? r'')
Here is the error I am getting:
unexpected expression for argument "pattern"
expected: regex
received: group
The version that I am using is 0.27.0
Hi @Genry1 !
Yeah, it looks like the parse_regex function requires a regular expression literal. Unfortunately I don't think what you are doing will be possible in VRL right now. A workaround would be using Lua. Would you want to open an issue specifically for allowing parse_regex to take a dynamic regex? This issue is more about allowing string values to be interpreted as regular expressions.
Thanks for confirming that, and for the workaround hint! Sure, I will open a new issue.
Thanks! I'll look out for the new issue. We realized that this issue is actually covered by https://github.com/vectordotdev/vector/pull/7074 so I'll close this one.
Hey @jszwedko, I've submitted the issue. I've had a look at the code, and it seems to be a pretty simple change. I can submit a PR for this change.
Great, thanks!