Varnishlog: measure your Varnish cache p·续1
As you can see, the log lines are no longer grouped in transactions. They are displayed as they are received. As soon as some production traffic hits your Varnish server, raw grouping becomes nearly impossible to use.
However, raw grouping still has its purpose and is also the only way to collect non-transactional logs, logs that aren’t tied to a session , request or backend request task. Non-transactional logs have the VXID 0 to reflect the absence of a transaction ID.
Here’s the output of varnishlog -g raw in a situation where there are no incoming requests:
0 CLI - Rd ping
0 CLI - Wr 200 19 PONG 1691745227 1.0
0 Backend_health - boot.default Still healthy 4---X-RH 8 3 8 0.004862 0.003716 HTTP/1.1 200 OK
This output features CLI tags that log CLI communication between the varnishd master and child process. You can see a ping being sent and a PONG being received.
The output also features a Backend_health tag that logs the result of a health probe that is defined in the VCL file. This probe sends an HTTP request to a pre-defined endpoint and expects a 200 OK status to be considered healthy.
Tags#
Each VSL transaction contains a number of log lines. Each line has a tag and that tag has a corresponding value.
In previous examples we’ve limited ourselves to the Begin tag to indicate the beginning of each transaction. But there are a lot more tags that can be displayed in the VSL output.
Transaction tags#
Here’s the list of transaction-related tags:
Begin: marks the start of an VXID transactionEnd: marks the end of an VXID transactionLink: links to a child VXID transaction
And here’s an example where these tags are used:
* << Request >> 2
- Begin req 1 rxreq
- Link bereq 3 fetch
- End
** << BeReq >> 3
-- Begin bereq 2 fetch
-- End
This is the format of the Begin tag:
%s %d %s
| | |
| | +- Reason
| +---- Parent vxid
+------- Type ("sess", "req" or "bereq")
As you can see it starts with either sess, req, or bereq to indicate the type of transaction. The reason field clearly indicates why or how this transaction is taking place.
The Link tag is also quite useful, as it shows what kind of child transaction is initiated from the current one.
Here’s the format:
%s %d %s
| | |
| | +- Reason
| +---- Child vxid
+------- Child type ("req" or "bereq")
A transaction can either trigger a (sub-)request , or a backend request. Hence the types req and bereq. The Link tag will also display the VXID of the child transaction. And finally, the reason why this happens is displayed.
Session tags#
SessOpen: displays the socket endpoints when a client connection has been openedSessClose: displays the reason for the socket closure and the duration of the session
Here’s an example where these two tags are used within a session transaction:
* << Session >> 1
- SessOpen 127.0.0.1 51726 a0 127.0.0.1 80 1606299467.161264 19
- SessClose REM_CLOSE 0.004
In order to understand what the values of SessOpen are, here’s the format for this tag:
%s %d %s %s %s %f %d
| | | | | | |
| | | | | | +- File descriptor number
| | | | | +----- Session start time (unix epoch)
| | | | +-------- Local TCP port
| | | +----------- Local IPv4/6 address
| | +-------------- Socket name (from -a argument)
| +----------------- Remote TCP port
+-------------------- Remote IPv4/6 address
So let’s explain the meaning of the values from the example:
127.0.0.1is the IP address of the client.51726is the port number of the client.a0is the name of our socket. Because we didn’t name the socket via the-astartup parameter invarnishd, the namea0was automatically assigned.127.0.0.1is also the IP address of the server.80is the port number of the server. This indicates regular HTTP traffic.1606299467.161264is the epoch equivalent ofWednesday, November 25, 2020 10:17:47.161 AM.19is the file descriptor number.
And from the SessClose tag we can determine that the session was closed normally and lasted four milliseconds.
Request tags#
The most commonly used VSL tags are request tags. They refer to the HTTP request that is made by the client.
Here’s an overview of these tags:
ReqStart: the start of request processingReqMethod: the HTTP method used to perform the requestReqURL: the URL that is addressed by the requestReqProtocol: the HTTP version used by the requestReqHeader: a set of request headers that were passedReqAcct: the byte counts for the request handling
Here’s an example that features these tags:
- ReqStart 127.0.0.1 51726 a0
- ReqMethod GET
- ReqURL /
- ReqProtocol HTTP/1.1
- ReqHeader Host: localhost
- ReqHeader User-Agent: curl/7.64.0
- ReqHeader Accept: */*
- ReqHeader X-Forwarded-For: 127.0.0.1
- ReqAcct 73 0 73 268 540 808
The ReqStart tag lets us know what the IP address and port number of the client is. The a0 refers to the socket that was used to connect. This socket is defined using the -a startup parameter.
The ReqMethod tag indicates a regular HTTP GET request, and the ReqURL shows that this request happened on the / URL.
The HTTP protocol version for this request was HTTP/1.1, as indicated by the ReqProtocol tag.
A set of ReqHeader tags are used to list the request headers that were passed with this HTTP request.
Finally, the ReqAcct tag is there to provide request accounting information. Here’s the format of this tag, which will allow us to explain the meaning of ReqAcct from our example:
%d %d %d %d %d %d
| | | | | |
| | | | | +- Total bytes transmitted
| | | | +---- Body bytes transmitted
| | | +------- Header bytes transmitted
| | +---------- Total bytes received
| +------------- Body bytes received
+---------------- Header bytes received
What this means in our example is that the incoming request contained 73 bytes of request headers , and no request body, which results in a total incoming byte count of 73 bytes.
The response contained 268 bytes of response header data , and 540 bytes of body payload. This body payload size will also be reflected in the Content-Length response header.
The total number of transmitted bytes is 808.
Response tags#
Whenever there’s an HTTP request, there must an HTTP response. The response tags are responsible for displaying information about the HTTP response that was generated by Varnish.
Here’s the list of response tags:
RespProtocol: the HTTP version that is used in the responseRespStatus: the HTTP response status code of the responseRespReason: the response-reason phrase that clarifies the statusRespHeader: a set of response headers that were returned
Here’s a short example of a response containing the response tags that were mentioned:
- RespProtocol HTTP/1.1
- RespStatus 200
- RespReason OK
- RespHeader Content-Type: text/html
- RespHeader Content-Encoding: gzip
- RespHeader X-Varnish: 2
- RespHeader Age: 1000
We know the response was made using the HTTP/1.1 protocol, as indicated in by the RespProtocol tag. The response was a regular 200 OK. The RespStatus and RespReason will return this information. And finally, there is a set of RespHeader tags that contains the HTTP response headers that were returned.
In this case two response headers were injected by Varnish:
Age: how long the object has been in cacheX-Varnish: the VXID of the transaction
Backend tags#
The backend tags are used when a miss or pass occurs, and a connection needs to be established with the origin server.
Here’s an example:
- BackendOpen 31 boot.default 172.22.0.2 8080 172.22.0.3 45378
- BackendStart 172.22.0.2 8080
- BackendClose 31 boot.default
The BackendOpen tag provides information about the TCP or UDS connection that is established with the backend. Here’s the format of the tag:
%d %s %s %s %s %s
| | | | | |
| | | | | +- Local port
| | | | +---- Local address
| | | +------- Remote port
| | +---------- Remote address
| +------------- Backend display name
+---------------- Connection file descriptor
In our example file descriptor 31 is used for the backend, and the actual backend that was selected came from a VCL configuration named boot. Inside that VCL configuration a backend named default was used.
You’ll agree that this is a very standard situation.
The connection with the origin was made via IP address 172.22.0.2 on port 8080. The connection originated from IP address 172.22.0.3, and the source port was 45378. The BackendStart tag provides similar information and doesn’t add that much value.
However, it is interesting to see that a BackendClose tag was found. This means the connection was not recycled for future requests to this backend.
If keep-alive was enabled, the origin would return a Connection: Keep-Alive header, and Varnish would reuse that connection. This would result in the BackendReuse tag to appear in VSL, as illustrated below:
- BackendReuse 31 boot.default
Backend request tags#
When a backend is opened, the goal is the send a backend request. The backend request tags are there to provide information about the backend request.
In most cases, the backend request will be identical to the client request. However, VCL does allow you to change request information, which might be reflected in the backend request.
These are the backend request tags:
BereqMethodBereqURLBereqProtocolBereqHeaderBereqAcct
This probably needs further explanation.
Backend response tags#
Just like regular response tags, there are also backend response tags. These are displayed when the response wasn’t served from cache but required a backend request.
These are the tags, and they look very similar to regular response tags:
BerespProtocolBerespStatusBerespReasonBerespHeader
Again, this needs further explanation.
Object tags#
Once a backend response is stored in the cache, it is considered an object and the response properties can be view in VSL using object tags. Here’s the list of object tags:
ObjProtocolObjStatusObjReasonObjHeader
VCL tags#
As the Varnish execution flow moves through the various stages of the finite state machine, VCL tags can be used to inspect the flow.
Here’s an overview of the various VCL tags in VSL:
VCL_Error: returns the error message in case of a VCL execution failureVCL_Log: custom log messages that were logged viastd.log()in VCLVCL_acl: evaluation of ACL s in VCLVCL_call: the name of the VCL state that is currently being executedVCL_return: the return statement that was used to transition to the next stateVCL_use: the name of the VCL configuration that is being used
Here’s an example containing some VCL tags:
* << Request >> 2
- VCL_call RECV
- VCL_return hash
- VCL_call HASH
- VCL_return lookup
- VCL_call MISS
- VCL_return fetch
- Link bereq 3 fetch
- VCL_call DELIVER
- VCL_return deliver
** << BeReq >> 3
-- VCL_use boot
-- VCL_call BACKEND_FETCH
-- VCL_return fetch
-- VCL_call BACKEND_RESPONSE
-- VCL_return deliver
This log extract represents a cache miss. The VCL_call tags go from RECV, to HASH, to MISS, to BACKEND_FETCH, to BACKEND_RESPONSE, to DELIVER. The VCL_return tags are responsible for this sequence of VCL subroutines being called.
When the request results in a cache hit , you’ll notice the following VCL tags in the VSL output:
* << Request >> 5
- VCL_call RECV
- VCL_return hash
- VCL_call HASH
- VCL_return lookup
- VCL_call HIT
- VCL_return deliver
- VCL_call DELIVER
- VCL_return deliver
