tsweb: use a ResponseWriter wrapper to log more accurately.

Also adds tests, because the logging handler is acquiring a fair
number of branches.

Signed-off-by: David Anderson <dave@natulte.net>
This commit is contained in:
David Anderson
2020-03-18 17:37:40 -07:00
committed by Dave Anderson
parent 2e43cd3f95
commit 12a6626a94
3 changed files with 426 additions and 60 deletions

View File

@@ -10,30 +10,46 @@ import (
"time"
)
// Msg is a structured event log entry.
type Msg struct {
Where string `json:"where"`
When time.Time `json:"when"`
Duration time.Duration `json:"duration,omitempty"`
Domain string `json:"domain,omitempty"`
Msg string `json:"msg,omitempty"`
Err error `json:"err,omitempty"`
HTTP *MsgHTTP `json:"http,omitempty"`
Data interface{} `json:"data,omitempty"`
}
// AccessLogRecord is a record of one HTTP request served.
type AccessLogRecord struct {
// Timestamp at which request processing started.
When time.Time `json:"when"`
// Time it took to finish processing the request. It does not
// include the entire lifetime of the underlying connection in
// cases like connection hijacking, only the lifetime of the HTTP
// request handler.
Seconds float64 `json:"duration"`
// MsgHTTP contains information about the processing of one HTTP
// request.
type MsgHTTP struct {
Code int `json:"code"`
Path string `json:"path"`
// The client's ip:port.
RemoteAddr string `json:"remote_addr"`
UserAgent string `json:"user_agent"`
Referer string `json:"referer"`
// The HTTP protocol version, usually "HTTP/1.1 or HTTP/2".
Proto string `json:"proto"`
// Whether the request was received over TLS.
TLS bool `json:"tls"`
// The target hostname in the request.
Host string `json:"host"`
// The HTTP method invoked.
Method string `json:"method"`
// The unescaped request URI, including query parameters.
RequestURI string `json:"request_uri"`
// The client's user-agent
UserAgent string `json:"user_agent"`
// Where the client was before making this request.
Referer string `json:"referer"`
// The HTTP response code sent to the client.
Code int `json:"code"`
// Number of bytes sent in response body to client. If the request
// was hijacked, only includes bytes sent up to the point of
// hijacking.
Bytes int `json:"bytes"`
// Error encountered during request processing.
Err string `json:"err"`
}
// String returns m as a JSON string.
func (m Msg) String() string {
func (m AccessLogRecord) String() string {
if m.When.IsZero() {
m.When = time.Now()
}