tsweb: rename AccessLogRecord's When to Time

This change makes our access log record more consistent with the
new log/tslog package formatting of "time". Note that we can
change slog itself to call "time" "when" but we're chosing
to make this breaking change to be consistent with the std lib's
defaults.

Updates tailscale/corp#17071

Signed-off-by: Marwan Sulaiman <marwan@tailscale.com>
pull/12305/head
Marwan Sulaiman 4 months ago committed by Marwan Sulaiman
parent 0380cbc90d
commit 7e357e1636

@ -12,7 +12,7 @@ import (
// AccessLogRecord is a record of one HTTP request served. // AccessLogRecord is a record of one HTTP request served.
type AccessLogRecord struct { type AccessLogRecord struct {
// Timestamp at which request processing started. // Timestamp at which request processing started.
When time.Time `json:"when"` Time time.Time `json:"time"`
// Time it took to finish processing the request. It does not // Time it took to finish processing the request. It does not
// include the entire lifetime of the underlying connection in // include the entire lifetime of the underlying connection in
// cases like connection hijacking, only the lifetime of the HTTP // cases like connection hijacking, only the lifetime of the HTTP
@ -55,8 +55,8 @@ type AccessLogRecord struct {
// String returns m as a JSON string. // String returns m as a JSON string.
func (m AccessLogRecord) String() string { func (m AccessLogRecord) String() string {
if m.When.IsZero() { if m.Time.IsZero() {
m.When = time.Now() m.Time = time.Now()
} }
var buf strings.Builder var buf strings.Builder
json.NewEncoder(&buf).Encode(m) json.NewEncoder(&buf).Encode(m)

@ -299,7 +299,7 @@ type retHandler struct {
// ServeHTTP implements the http.Handler interface. // ServeHTTP implements the http.Handler interface.
func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
msg := AccessLogRecord{ msg := AccessLogRecord{
When: h.opts.Now(), Time: h.opts.Now(),
RemoteAddr: r.RemoteAddr, RemoteAddr: r.RemoteAddr,
Proto: r.Proto, Proto: r.Proto,
TLS: r.TLS != nil, TLS: r.TLS != nil,
@ -371,7 +371,7 @@ func (h retHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
lw.code = 200 lw.code = 200
} }
msg.Seconds = h.opts.Now().Sub(msg.When).Seconds() msg.Seconds = h.opts.Now().Sub(msg.Time).Seconds()
msg.Code = lw.code msg.Code = lw.code
msg.Bytes = lw.bytes msg.Bytes = lw.bytes

@ -87,7 +87,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/"), r: req(bgCtx, "http://example.com/"),
wantCode: 200, wantCode: 200,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
TLS: false, TLS: false,
@ -104,7 +104,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/"), r: req(bgCtx, "http://example.com/"),
wantCode: 200, wantCode: 200,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
TLS: false, TLS: false,
@ -121,7 +121,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 404, wantCode: 404,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -137,7 +137,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 404, wantCode: 404,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -153,7 +153,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 404, wantCode: 404,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -171,7 +171,7 @@ func TestStdHandler(t *testing.T) {
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"), r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
wantCode: 404, wantCode: 404,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -190,7 +190,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 404, wantCode: 404,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -208,7 +208,7 @@ func TestStdHandler(t *testing.T) {
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"), r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
wantCode: 404, wantCode: 404,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -227,7 +227,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 500, wantCode: 500,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -245,7 +245,7 @@ func TestStdHandler(t *testing.T) {
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"), r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
wantCode: 500, wantCode: 500,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -264,7 +264,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 500, wantCode: 500,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -282,7 +282,7 @@ func TestStdHandler(t *testing.T) {
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"), r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
wantCode: 500, wantCode: 500,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -301,7 +301,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 500, wantCode: 500,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -319,7 +319,7 @@ func TestStdHandler(t *testing.T) {
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"), r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
wantCode: 500, wantCode: 500,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -338,7 +338,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 200, wantCode: 200,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -355,7 +355,7 @@ func TestStdHandler(t *testing.T) {
r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"), r: req(RequestIDKey.WithValue(bgCtx, exampleRequestID), "http://example.com/foo"),
wantCode: 200, wantCode: 200,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -373,7 +373,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 200, wantCode: 200,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -390,7 +390,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 200, wantCode: 200,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
Host: "example.com", Host: "example.com",
@ -412,7 +412,7 @@ func TestStdHandler(t *testing.T) {
r: req(bgCtx, "http://example.com/foo"), r: req(bgCtx, "http://example.com/foo"),
wantCode: 200, wantCode: 200,
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
@ -432,7 +432,7 @@ func TestStdHandler(t *testing.T) {
http.Error(w, e.Msg, 200) http.Error(w, e.Msg, 200)
}, },
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
TLS: false, TLS: false,
@ -455,7 +455,7 @@ func TestStdHandler(t *testing.T) {
http.Error(w, fmt.Sprintf("%s with request ID %s", e.Msg, requestID), 200) http.Error(w, fmt.Sprintf("%s with request ID %s", e.Msg, requestID), 200)
}, },
wantLog: AccessLogRecord{ wantLog: AccessLogRecord{
When: startTime, Time: startTime,
Seconds: 1.0, Seconds: 1.0,
Proto: "HTTP/1.1", Proto: "HTTP/1.1",
TLS: false, TLS: false,

Loading…
Cancel
Save