nyov
nyov

Reputation: 1462

How to log backend in varnishncsa client mode log format?

I'm looking for a way to include the backend name (as configured by the backend vcl option), that a request was sent to (or response received from; either way) in varnishncsa log output.

When running varnishncsa in client mode, i.e. logging the frontend requests/responses.

The log format is here: https://varnish-cache.org/docs/trunk/reference/varnishncsa.html#format

I've tried to find an "Extended variable" (%{X}x) that would satisfy this option, without luck so far. Varnish version is 6.x.

Do I need to set a custom request or response header, to match on, for this first??

Upvotes: 0

Views: 1828

Answers (1)

Thijs Feryn
Thijs Feryn

Reputation: 4808

You can use an extended variable to get the job done.

varnishncsa from a backend request

Here's an example where I retrieve the backend name for backend requests:

varnishncsa -b -F "%{VSL:BackendOpen[2]}x"

The VSL tag you need is the BackendOpen tag that would return the following output in varnishlog:

-   BackendOpen    32 boot.bla 192.168.224.2 8080 192.168.224.3 53908

As you can see the backend name is the second field, hence the BackendOpen[2] expression.

varnishncsa from a client request

In the client thread the backend is hinted, but not opened.

If you want to access the hinted backend, you'll need to have access to the req.backend_hint variable. You could use std.log() to log this value as a VCL_Log tag.

Here's the VCL example:

vcl 4.0;
import std;

backend default {
  .host="1.2.3.4";
  .port="80";
}

sub vcl_recv {
  std.log("Backend: "+ req.backend_hint);
}

And here's the varnishncsa command the would look for this value:

varnishncsa -c -F "%{VSL:VCL_Log:Backend}x"

Conclusion

Of course you still need to tune your varnishncsa command a bit to include the right fields and filter on the right parameters, but this one should hopefully answer your question.

It's up to you to look for the backend name at client request side or at the backend request side.

Upvotes: 4

Related Questions