9

I would like to add a unique id to each request done through apache which will be used in the access log and is forwarded to underlying systems in a header to be used in their logs.

Request id

What is the best solution to accomplish this?

David Berg
  • 1,958
  • 1
  • 21
  • 37

2 Answers2

14

mod_unique_id will provide an environment variable UNIQUE_ID with a unique identifier for each request. You can add it to request headers with:

RequestHeader set uniqueid %{UNIQUE_ID}e

If you add that header to apache logs, for example:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{uniqueid}i\"" combined 

you willl get something like:

10.0.2.2 - - [01/Nov/2016:23:12:40 +0000] "GET /index.html HTTP/1.1" 404 208 "WBkhaJRMNmj7U9aiFl2pzQAAAAA"
Dusan Bajic
  • 10,249
  • 3
  • 33
  • 43
  • This doesn't answer the question, which is about forwarding this to all subsequent requests. – Martin Wickman Nov 02 '18 at 12:47
  • 8
    @MartinWickman Headers added by apache will be available to all systems further downstream. – Rudolf Meijering Dec 07 '18 at 10:55
  • `mod_unique_id` generates IDs that are unique within the cluster of Apache servers. But they may not be unique if those logs are forwarded to a log aggregation service (ELK, Spunk, etc.) where they are comingled with logs from many clusters. Going the other way, all the tracing frameworks emit compound trace IDs that are cannot be matched with a simple equality test. Perhaps I am missing something ... – Charlie Reitzel Jun 15 '22 at 19:23
0

It seems that the best answer provides a solution in which the related systems pick up the unique identifier from the header of the request. There are some more details about this here: Accessing Apache's unique_id from java code

AlexN
  • 29
  • 4