1package accesslog
2
3import (
4	"net/http"
5)
6
7const (
8	// StartUTC is the map key used for the time at which request processing started.
9	StartUTC = "StartUTC"
10	// StartLocal is the map key used for the local time at which request processing started.
11	StartLocal = "StartLocal"
12	// Duration is the map key used for the total time taken by processing the response, including the origin server's time but
13	// not the log writing time.
14	Duration = "Duration"
15
16	// RouterName is the map key used for the name of the Traefik router.
17	RouterName = "RouterName"
18	// ServiceName is the map key used for the name of the Traefik backend.
19	ServiceName = "ServiceName"
20	// ServiceURL is the map key used for the URL of the Traefik backend.
21	ServiceURL = "ServiceURL"
22	// ServiceAddr is the map key used for the IP:port of the Traefik backend (extracted from BackendURL).
23	ServiceAddr = "ServiceAddr"
24
25	// ClientAddr is the map key used for the remote address in its original form (usually IP:port).
26	ClientAddr = "ClientAddr"
27	// ClientHost is the map key used for the remote IP address from which the client request was received.
28	ClientHost = "ClientHost"
29	// ClientPort is the map key used for the remote TCP port from which the client request was received.
30	ClientPort = "ClientPort"
31	// ClientUsername is the map key used for the username provided in the URL, if present.
32	ClientUsername = "ClientUsername"
33	// RequestAddr is the map key used for the HTTP Host header (usually IP:port). This is treated as not a header by the Go API.
34	RequestAddr = "RequestAddr"
35	// RequestHost is the map key used for the HTTP Host server name (not including port).
36	RequestHost = "RequestHost"
37	// RequestPort is the map key used for the TCP port from the HTTP Host.
38	RequestPort = "RequestPort"
39	// RequestMethod is the map key used for the HTTP method.
40	RequestMethod = "RequestMethod"
41	// RequestPath is the map key used for the HTTP request URI, not including the scheme, host or port.
42	RequestPath = "RequestPath"
43	// RequestProtocol is the map key used for the version of HTTP requested.
44	RequestProtocol = "RequestProtocol"
45	// RequestScheme is the map key used for the HTTP request scheme.
46	RequestScheme = "RequestScheme"
47	// RequestContentSize is the map key used for the number of bytes in the request entity (a.k.a. body) sent by the client.
48	RequestContentSize = "RequestContentSize"
49	// RequestRefererHeader is the Referer header in the request.
50	RequestRefererHeader = "request_Referer"
51	// RequestUserAgentHeader is the User-Agent header in the request.
52	RequestUserAgentHeader = "request_User-Agent"
53	// OriginDuration is the map key used for the time taken by the origin server ('upstream') to return its response.
54	OriginDuration = "OriginDuration"
55	// OriginContentSize is the map key used for the content length specified by the origin server, or 0 if unspecified.
56	OriginContentSize = "OriginContentSize"
57	// OriginStatus is the map key used for the HTTP status code returned by the origin server.
58	// If the request was handled by this Traefik instance (e.g. with a redirect), then this value will be absent.
59	OriginStatus = "OriginStatus"
60	// DownstreamStatus is the map key used for the HTTP status code returned to the client.
61	DownstreamStatus = "DownstreamStatus"
62	// DownstreamContentSize is the map key used for the number of bytes in the response entity returned to the client.
63	// This is in addition to the "Content-Length" header, which may be present in the origin response.
64	DownstreamContentSize = "DownstreamContentSize"
65	// RequestCount is the map key used for the number of requests received since the Traefik instance started.
66	RequestCount = "RequestCount"
67	// GzipRatio is the map key used for the response body compression ratio achieved.
68	GzipRatio = "GzipRatio"
69	// Overhead is the map key used for the processing time overhead caused by Traefik.
70	Overhead = "Overhead"
71	// RetryAttempts is the map key used for the amount of attempts the request was retried.
72	RetryAttempts = "RetryAttempts"
73
74	// TLSVersion is the version of TLS used in the request.
75	TLSVersion = "TLSVersion"
76	// TLSCipher is the cipher used in the request.
77	TLSCipher = "TLSCipher"
78)
79
80// These are written out in the default case when no config is provided to specify keys of interest.
81var defaultCoreKeys = [...]string{
82	StartUTC,
83	Duration,
84	RouterName,
85	ServiceName,
86	ServiceURL,
87	ClientHost,
88	ClientPort,
89	ClientUsername,
90	RequestHost,
91	RequestPort,
92	RequestMethod,
93	RequestPath,
94	RequestProtocol,
95	RequestScheme,
96	RequestContentSize,
97	OriginDuration,
98	OriginContentSize,
99	OriginStatus,
100	DownstreamStatus,
101	DownstreamContentSize,
102	RequestCount,
103}
104
105// This contains the set of all keys, i.e. all the default keys plus all non-default keys.
106var allCoreKeys = make(map[string]struct{})
107
108func init() {
109	for _, k := range defaultCoreKeys {
110		allCoreKeys[k] = struct{}{}
111	}
112	allCoreKeys[ServiceAddr] = struct{}{}
113	allCoreKeys[ClientAddr] = struct{}{}
114	allCoreKeys[RequestAddr] = struct{}{}
115	allCoreKeys[GzipRatio] = struct{}{}
116	allCoreKeys[StartLocal] = struct{}{}
117	allCoreKeys[Overhead] = struct{}{}
118	allCoreKeys[RetryAttempts] = struct{}{}
119	allCoreKeys[TLSVersion] = struct{}{}
120	allCoreKeys[TLSCipher] = struct{}{}
121}
122
123// CoreLogData holds the fields computed from the request/response.
124type CoreLogData map[string]interface{}
125
126// LogData is the data captured by the middleware so that it can be logged.
127type LogData struct {
128	Core               CoreLogData
129	Request            request
130	OriginResponse     http.Header
131	DownstreamResponse downstreamResponse
132}
133
134type downstreamResponse struct {
135	headers http.Header
136	status  int
137	size    int64
138}
139
140type request struct {
141	headers http.Header
142	// Request body size
143	size int64
144}
145