mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-30 05:25:35 +00:00
b819f66eb1
The recent addition of RequestID was only populated if the HTTP Request had returned an error. This meant that the underlying handler has no access to this request id and any logs it may have emitted were impossible to correlate to that request id. Therefore, this PR adds a middleware to generate request ids and pass them through the request context. The tsweb.StdHandler automatically populates this request id if the middleware is being used. Finally, inner handlers can use the context to retrieve that same request id and use it so that all logs and events can be correlated. Updates #2549 Signed-off-by: Marwan Sulaiman <marwan@tailscale.com>
65 lines
2.2 KiB
Go
65 lines
2.2 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
package tsweb
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// 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,omitempty"`
|
|
|
|
// The client's ip:port.
|
|
RemoteAddr string `json:"remote_addr,omitempty"`
|
|
// The HTTP protocol version, usually "HTTP/1.1 or HTTP/2".
|
|
Proto string `json:"proto,omitempty"`
|
|
// Whether the request was received over TLS.
|
|
TLS bool `json:"tls,omitempty"`
|
|
// The target hostname in the request.
|
|
Host string `json:"host,omitempty"`
|
|
// The HTTP method invoked.
|
|
Method string `json:"method,omitempty"`
|
|
// The unescaped request URI, including query parameters.
|
|
RequestURI string `json:"request_uri,omitempty"`
|
|
|
|
// The client's user-agent
|
|
UserAgent string `json:"user_agent,omitempty"`
|
|
// Where the client was before making this request.
|
|
Referer string `json:"referer,omitempty"`
|
|
|
|
// The HTTP response code sent to the client.
|
|
Code int `json:"code,omitempty"`
|
|
// 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,omitempty"`
|
|
// Error encountered during request processing.
|
|
Err string `json:"err,omitempty"`
|
|
// RequestID is a unique ID for this request. If the *http.Request context
|
|
// carries this value via SetRequestID, then it will be displayed to the
|
|
// client immediately after the error text, as well as logged here. This
|
|
// makes it easier to correlate support requests with server logs. If a
|
|
// RequestID generator is not configured, RequestID will be empty.
|
|
RequestID RequestID `json:"request_id,omitempty"`
|
|
}
|
|
|
|
// String returns m as a JSON string.
|
|
func (m AccessLogRecord) String() string {
|
|
if m.When.IsZero() {
|
|
m.When = time.Now()
|
|
}
|
|
var buf strings.Builder
|
|
json.NewEncoder(&buf).Encode(m)
|
|
return strings.TrimRight(buf.String(), "\n")
|
|
}
|