HAProxy community

Unique-id: adding only if header not present


#1

We currently add a unique-id and use it to see a request flow through our backend servers.
Our backend servers will soon communicate also via the haproxy. All should use only one unique-id.
We tested and haproxy adds a new unique-id. This should be avoided.

Would it be possible to add this header only if not yet present?

Best regards,
richard


#2

Hi,

This is not possible.
That said, a dirty workaround may exist and worth a try.
Imagine the unique ID header already set is X-uniq-id.

frontend f_myapp
  unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%Ts_%rt:%pid
  unique-id-header TMP-X-uniq-id
  default_backend b_myapp

backend b_myapp
   acl existing-uniq-id req.hdr(X-uniq-id) -m found
   http-request set-header X-uniq-id %[TMP-X-uniq-id] unless existing-uniq-id
   http-request del-heder TMP-X-uniq-id

#3

More simple in fact, unique-id-format pre-dated the set-header rules. Now you don’t need it at all and “http-request set-header” is perfectly fine as a placement. Let’s simply consider that unique-id-format is unconditional while you have the choice with set-header.


#4

Yes, as you write it, it is so obvious.


#5

But does http-request set-header work with %ID? I have not been able to get that to work at all (1.6.11):

unique-id-format %{+X}o\ %Ts_%ci_%cp_%fi_%fp_%rt_%pid
http-request set-header X-Request-Id %ID
log-format %ci\ %r\ %ST\ %B\ %Tr\ %Tt\ %s\ %ID\ %hr

The id is logged, but the header is not set. If I replace %ID with a hardcoded string, like “HELLO” it works, I can also other log variables – like %Ts. I can also get rid of the unique-id-format parameter and just pass its parameters to my set-header directive. But then I run into odd logging issues. Is there a recommended way to set a distinct request ID via set-header?