Is Varnish fast? Of course it is! I know that, you know that, but I want my friends, and my friends’ friends to also know that. But of course, they aren’t just going to believe me, because, as one wise man said: “bragging rights aren’t given, they have to be demonstrably earned”.
This is where monitoring and observability come into play. We need them to collect data about how Varnish is doing, and send that information somewhere™ it can be aggregated, crunched, and correlated with various events on the platform. In short: we need to make it useful. How do we do that?
Well, I’m an engineer, so of course, my go-to answer will be: “it depends”. But because I really want you to brag about Varnish performing very well, I have something for you today.
Metrics, all of them
If you know Varnish even at all, you know that we take great pride in exposing as much information as possible (and in the most efficient way possible), to the point that the only word that comes to (my) mind is “brutal”.
Let’s start with metrics: our tool of choice here is varnishstat. This deceptively simple tool that’ll happily dump all the internal counters that Varnish constantly updates. And I literally mean ALL of them, we hide nothing, no sir!
For example, here’s what I get on my laptop, with a fairly vanilla configuration:
The total output contains 356 counters (again, on my vanilla laptop setup) including currently used connections and cache usage, down to the number of times a specific mutex had to wait to get locked. It’s a treasure trove of information, and while you will probably never need ALL of it, what you do need is most certainly in it.
varnishstat makes it easy to sift through a wealth of information. You can query individual counters, or glob entire categories. All of that is just one filter away on the command line. We’ve got you covered in terms of visualization options, too.
We’ve already seen the -1 argument to dump the stats and exits, but we also have the default interactive view:
But if you want to export the data, you’ll probably want varnishstat -j for some JSON goodness:
Note: this is the output that is internally used by the great Varnish Prometheus Exporter.
You’re feeling old school and want some XML? We have that too, and I promise I won’t judge you:
What about logs?
Oh boy, I thought you’d never ask! Logs in Varnish are handled a tiny bit differently than in other applications, in the sense that Varnish itself doesn’t write log files. Instead, it just dumps the maximal set of data into memory and lets external loggers read that memory and format it the way YOU want and need it. In essence, Varnish is constantly logging everything in memory, and you can access that memory through a couple of tools.
First up is my favorite debugging tool of all time: varnishlog. If varnishstat was brutal, varnishlog is just bonkers, have a look:
That’s for one (ONE!) client request that then goes to the origin. You get timing information for each step of the way, you get caching decisions, connection data, header manipulation, YOU.GET.EVERYTHING.
You also get a powerful query system to filter slow requests, failed responses, transactions from a certain IP address, etc. Seriously, it’s insane, but it’s absolutely necessary when you need to find that one problematic request, to debug it.
All that data miiiight be a bit too much if you just need access logs, though. Access logs should be short and sweet, with just the information you need to create some reports, billing, and other light but necessary post-processing operations. For this, we have varnishncsa: which will output lines like:
As the name suggests, it supports the ncsa log format and allows you to tweak what goes into the line. Oh, and it supports the same excellent query system as varnishlog, just in case you want to filter requests per domain name, for example. It’s not uncommon to run multiple varnishncsa daemon, logging to different files to cover multiple properties/sites independently, for example.
Last but not least, there’s also JSON logging, which we announced a few weeks ago. It proposes a middle ground between the two previous tools. You get structured data, but you don’t necessarily need to handpick all the headers you want to know about:
And because it’s JSON, it’s highly processable, since the language is ubiquitous. And at the risk of repeating myself, this one too supports query filters (they are all built atop the same library, so those benefits are shared).
What to do with all that data?
This, my friends, is a question for another blog post, the next one, actually. Varnish follows the UNIX philosophy, so it makes sure that the data is easily accessible and exported, leaving the transport and utilization of this data to the care of more specialized tools.
In part 2, we’ll explore integrations and see how we can plug all of this into a larger ecosystem in just a few steps. Stay tuned, and see you in a week or two!