log.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package tsweb
  4. import (
  5. "encoding/json"
  6. "strings"
  7. "time"
  8. )
  9. // AccessLogRecord is a record of one HTTP request served.
  10. type AccessLogRecord struct {
  11. // Timestamp at which request processing started.
  12. Time time.Time `json:"time"`
  13. // Time it took to finish processing the request. It does not
  14. // include the entire lifetime of the underlying connection in
  15. // cases like connection hijacking, only the lifetime of the HTTP
  16. // request handler.
  17. Seconds float64 `json:"duration,omitempty"`
  18. // The client's ip:port.
  19. RemoteAddr string `json:"remote_addr,omitempty"`
  20. // The HTTP protocol version, usually "HTTP/1.1 or HTTP/2".
  21. Proto string `json:"proto,omitempty"`
  22. // Whether the request was received over TLS.
  23. TLS bool `json:"tls,omitempty"`
  24. // The target hostname in the request.
  25. Host string `json:"host,omitempty"`
  26. // The HTTP method invoked.
  27. Method string `json:"method,omitempty"`
  28. // The unescaped request URI, including query parameters.
  29. RequestURI string `json:"request_uri,omitempty"`
  30. // The client's user-agent
  31. UserAgent string `json:"user_agent,omitempty"`
  32. // Where the client was before making this request.
  33. Referer string `json:"referer,omitempty"`
  34. // The HTTP response code sent to the client.
  35. Code int `json:"code,omitempty"`
  36. // Number of bytes sent in response body to client. If the request
  37. // was hijacked, only includes bytes sent up to the point of
  38. // hijacking.
  39. Bytes int `json:"bytes,omitempty"`
  40. // Error encountered during request processing.
  41. Err string `json:"err,omitempty"`
  42. // RequestID is a unique ID for this request. If the *http.Request context
  43. // carries this value via SetRequestID, then it will be displayed to the
  44. // client immediately after the error text, as well as logged here. This
  45. // makes it easier to correlate support requests with server logs. If a
  46. // RequestID generator is not configured, RequestID will be empty.
  47. RequestID RequestID `json:"request_id,omitempty"`
  48. }
  49. // String returns m as a JSON string.
  50. func (m AccessLogRecord) String() string {
  51. if m.Time.IsZero() {
  52. m.Time = time.Now()
  53. }
  54. var buf strings.Builder
  55. json.NewEncoder(&buf).Encode(m)
  56. return strings.TrimRight(buf.String(), "\n")
  57. }