Search code examples
envoyproxy

Can I get the time when a request was received by Envoy?


I have a python flask web service. It uses Gunicorn so that it can serve up to 4 request in parallel, and uses Envoy as a proxy.

The system contains a number of nodes, and the node's clocks must be relatively synced (at most 2 seconds apart). So when a new node requests to join the system, one of the arguments it sends is the "request_time" (i.e. the time on the node's clock when it made the request). The node receiving this request makes sure "request_time" is within 2 seconds of it's own clock's time.

The problem is that on a busy system, a request can end up waiting for some time before any service-worker is free to handle it. If the request is stuck in the queue for more then 2 seconds, the "request_time" will be more then 2 seconds behind the current time, and the request will fail even though the nodes' clocks are synced.

Looking in Envoy's log, I see that Envoy received the request on time (i.e. no delay). This means that if I could compare the "request_time" to the time that Envoy received the request instead of the time my flask service received it, I'll be okay. Is there any way I can either know when Envoy received the request, or have Envoy add that detail to the request?


Solution

  • After much investigating, I found that there's a fairly simple way to do this. All I needed to do is add a filter to the the relevant route that adds the time the request was received by Envoy as a header. Envoy even has an out of the box variable for that (%START_TIME%).

    The filter should look something like this:

                - header:
                   key: "some-key-name"
                   value: "%START_TIME(%s)%"
    

    Sources: