I am trying to create a Varnish response header which would help me debug the state of the caches. I would like to create a header which would display the datacenters the request went through and age of the object. Something like:
X-Fastly-Age: VIE:2311s FRA:0s
Which would mean the object was cached in VIE
data center (shield) and in the FRA
edge it had to be fetched from VIE
.
The second request would return something like:
X-Fastly-Age: VIE:2311s FRA:133s
I tried to set this:
set beresp.http.X-Fastly-Age = beresp.http.X-Fastly-Age " " req.http.edge-geo-datacenter ":" obj.entered;
This would work, but the problem is that:
beresp.http
cannot be written invcl_hit
obj.entered
is not available invcl_fetch
So basically it seems I don't have a place where to generate this header.
How can this be achieved?
EDIT:
I managed to run this in vcl_deliver
:
set resp.http.X-Fastly-Age = resp.http.X-Fastly-Age " " server.datacenter ":" obj.entered;
getting:
> x-fastly-age: (null) DCA:0.001 FRA:0.001
It suffers from two things:
- Getting
(null)
in the first call - is there a way to not prepend the header if it is null? - The edge time (FRA) never changes and stays on
0.001
. So it seems it is cached even with the obj.entered and never changes?
I'm going to approach this purely from a Varnish point of view, since I have no experience with Fastly.
Varnish uses an
Age
header, which it sets automatically, to determine the age of an object in cache.For uncached objects, the value of the
Age
response header is always zero. Non-zero values represent the age of an object accordingly.And for the
null
values you're getting: we can use anif/else
statement to solve that problem.Here's some VCL for you: