cancel
Showing results for 
Search instead for 
Did you mean: 

Generate a unique ID with the help of an iRule

Dan44
Altostratus
Altostratus

Hello everyone

 

I would like to generate a unique ID with the help of an iRule and place it in a header. The goal is to identify the HTTP request by the ID. Has anyone done something like this before?

 

What I have tried:

when HTTP_REQUEST priority 3 { ###Generate URL Tracing for { set i 0 } {$i < 8 } { incr i } { append rand_hex [format {%x} [expr {int(rand()*16)}]] } #log local0. "X-Request-ID: $rand_hex" ###Add X-Request-ID Header if {not([HTTP::header exists X-Request-ID ])}{ HTTP::header insert X-Request-ID $rand_hex     } } when HTTP_RESPONSE priority 3 {   HTTP::header insert X-Request-ID $rand_hex }

 

The problem is that I sometimes see multiple requests with the same ID. Does anyone have a better idea how to solve it?

 

 

 

 

3 REPLIES 3

Rob_Stonham
Cirrus
Cirrus

Dan44,

 

I'm not sure about generating the unique ID, I'd look at this article https://devcentral.f5.com/s/articles/random-string-generators or keep it simple and generate an id like Kai Wilke suggested in https://devcentral.f5.com/s/question/0D51T00006i7W29/how-random-is-rand

 

set uniqueID [TMM::cmp_unit][clock clicks]

Your code also allows a client to supply the header X-Request-ID, is this intended behaviour?

 

Rob

 

 

Hi Rob

Thanks for your reply, the links look interesting. I will have a look at them and test the examples.

 

Regarding your question. It is actually planned that only F5 LTM does the header insert and not the client.

if that is the case you should remove the HTTP_RESPONSE part