Access to beresp.http headers in Logging

Hi, I’m trying to set up a logging endpoint that’ll return some custom headers we’re setting, but I’m having trouble. When I added “prefab_api_key_id”: %{beresp.http.Prefab-KeyId}V" to the json log format I got the error message “Variable beresp.http.Prefab-KeyId not accessible in method vcl_log

As I understand it, the beresp.http.* variable(s) aren’t available in the vcl_log phase but the docs for vcl_fetch phase mention one of the uses of that phase as “* adding variables not available in vcl_log to response headers beresp.http.* so that they can be logged later”

That sounds like a workaround but I don’t know what its telling me to do.

Has anyone gotten back end response headers into the logs?

Thank you

Hi @jkebinger

Each subroutine (e.g. vcl_fetch, vcl_deliver, vcl_log etc) has access to different types of objects.

One of these objects is the ‘response’ object.

Access to the response is allowed in subroutines such as vcl_fetch, vcl_deliver and vcl_log but the way it is accessed changes slightly between subroutines.

To reference the response object from vcl_fetch you use beresp.

In the documentation for vcl_fetch you’ll see, under the “Tokens available in this subroutine” section, the token beresp.http.{NAME}.

For example, when you want to set a header on the origin response (so the content gets stored in the cache with the header modification), you would use something like the following inside of vcl_fetch:

set beresp.http.MyHeaderName = "example";

If you instead wanted to set a header on the origin response (but not have the content stored in the cache with the modified header), you would set the header inside of vcl_deliver instead. This is because by the time vcl_deliver is executed (after vcl_fetch) the content has already been stored in the cache. So the response object you’re modifying is sent to the client, but the header modification isn’t persisted into the cache.

But critically, setting a header on the response in vcl_deliver (and this is the same for vcl_log) requires you to switch from beresp to resp:

set resp.http.MyHeaderName = "example";

In the documentation for vcl_log (same for vcl_deliver) you’ll see, under the “Tokens available in this subroutine” section, the token resp.http.{NAME}.

NOTE: The reason for the difference in name (e.g. beresp vs resp) is to try and make it clearer what it is you’re modifying. In the case of beresp (i.e. “backend response”) you’re modifying the origin response, and any modifications to the origin response will be persisted into the cache. Where as resp is the final response that is sent to the client and so changes there are not persisted into cache. It’s a subtle but important distinction.

Hope this helps.

Thank you, that was helpful - copy beresp headers i want into response headers on the cached object, now they’re available at logging time

1 Like