0

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 in vcl_hit
  • obj.entered is not available in vcl_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:

  1. Getting (null) in the first call - is there a way to not prepend the header if it is null?
  2. 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?
Vojtěch
  • 11,312
  • 31
  • 103
  • 173
  • Do you need this or would using Fastly-Debug be sufficient? https://www.integralist.co.uk/posts/fastly-varnish/#fastly-debug – Integralist Apr 20 '21 at 08:32
  • 1
    It might be useful to look into how Fastly's request flow works (https://developer.fastly.com/learning/vcl/using/), as it would help explain why it wouldn't make sense to set beresp within vcl_hit. Also I recommend reading up on Fastly's clustering model (https://developer.fastly.com/learning/vcl/clustering/) as that would help understand why HTTP headers set within certain vcl subroutines can disappear or not persist data correctly. I've written about clustering and adding a 'breadcrumb' header (and lots more) here: https://www.integralist.co.uk/posts/fastly-varnish/#breadcrumb-trail) – Integralist Apr 20 '21 at 08:38
  • I'd also recommend using https://fiddle.fastlydemo.net/ for creating a reduced test case to validate things more easily. – Integralist Apr 20 '21 at 08:41
  • 1
    re: avoiding null you can use the `if()` ternary syntax: `set resp.http.X-Fastly-Age = if(resp.http.X-Fastly-Age, resp.http.X-Fastly-Age, "") " " server.datacenter ":" obj.entered;` Alternatively you could use it with a variable declaration, which is more verbose, if you prefer avoiding inlining a conditional: `declare local var.xfa STRING; set var.xfa = if(resp.http.X-Fastly-Age, resp.http.X-Fastly-Age, ""); set resp.http.X-Fastly-Age = var.xfa " " server.datacenter ":" obj.entered;` – Integralist Apr 20 '21 at 08:51

1 Answers1

1

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 an if/else statement to solve that problem.

Here's some VCL for you:

sub vcl_deliver {
    if(resp.http.X-Fastly-Age) {
        set resp.http.X-Fastly-Age = resp.http.X-Fastly-Age + " " + server.datacenter + ":" + resp.http.Age;
    } else {
        set resp.http.X-Fastly-Age = server.datacenter + ":" + resp.http.Age;
    }
}
Thijs Feryn
  • 3,982
  • 1
  • 5
  • 10
  • `For uncached objects, the value of the Age response header is always zero.` this is in fact not true. If there is a cache before Varnish, which already sets the `Age` header, it will only extend it, so it will never be zero. Which is a reason I wanted to use internal Fastly `obj.entered` header to get actual Age in Fastly varnish. – Vojtěch Apr 19 '21 at 18:10
  • @Vojtěch You're right. I forgot about the fact that Fastly uses multiple caching tiers. In that case I can't help you because I'm not too familiar with Fastly. However, I you have more Varnish questions, don't hesitate to reach out to me. – Thijs Feryn Apr 20 '21 at 06:59