Traffic mirroring with dynamic percentage from map

HAProxy version 2.1

Following the documentation in this blog post: https://www.haproxy.com/blog/haproxy-traffic-mirroring-for-real-world-testing/

We are looking to have a map file which contains the percentage of traffic to be mirrored so it would be always ‘on’ but configurable at runtime. Is something like this supported?

acl mirroring_on rand(100) lt str(percentage),map_str_int(/usr/local/etc/haproxy/mirror.map)

I couldn’t get it to work as a match value either, and rand() doesn’t accept it (even though map_str_int should produce an int type) on the other side of the match
I thought I might be able to hack it by setting option force-set-var and using variables to at least setup some predefined levels, like so:

# We can't set the rand value directly with str(mirror.every),map_str_int(mirror-runtime.conf)
acl mirror_high_threshold rand(100) -m int 0
acl mirroring_high_on str(mirror.level),map(mirror-runtime.conf) -m str high
acl mirror_low_threshold rand(10000) -m int 0
acl mirroring_low_on str(mirror.level),map(mirror-runtime.conf) -m str low
# Can't do (A and B) or (C and D), so do it using intermediate variables
http-request set-var(txn.mirror_high) bool(true) if mirror_high_threshold mirroring_high_on
http-request set-var(txn.mirror_low) bool(true) if mirror_low_threshold mirroring_low_on
http-request event on-frontend-http-request if { var(txn.mirror_high) -m bool } || { var(txn.mirror_low) -m bool }

But unfortunately even that doesn’t work, you can only set one level which is not much more useful that the tutorial example (except at least not mirroring every request):

acl mirror_threshold rand(100) -m int 0
acl mirroring_on str(mirror.active),map(mirror-runtime.conf) -m str on
event on-frontend-http-request if mirror_threshold mirroring_on

and then you also seem to need to reload HAProxy (or maybe change the value through the runtime API which I didn’t try), so it looks easier to do anything complex here with the data plane API unless there is some hidden magic in haproxy/doc/SPOE.txt at master · haproxy/haproxy · GitHub