/
http.go
73 lines (63 loc) · 1.98 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package log
import (
"net/http"
"strconv"
"time"
)
// EventHTTP is the data structure used for logging a HTTP event.
//
// It isn't very useful to export, other than for documenting the
// data structure it outputs.
type EventHTTP struct {
StatusCode *int `json:"status_code,omitempty"`
Method string `json:"method,omitempty"`
// URL data
Scheme string `json:"scheme,omitempty"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
Path string `json:"path,omitempty"`
Query string `json:"query,omitempty"`
// Timing data
StartedAt *time.Time `json:"started_at,omitempty"`
EndedAt *time.Time `json:"ended_at,omitempty"`
Duration *time.Duration `json:"duration,omitempty"`
ResponseContentLength int64 `json:"response_content_length,omitempty"`
}
func (l *EventHTTP) attach(le *EventData) {
le.HTTP = l
}
// HTTP returns an option you can pass to Event to log HTTP
// request data with a log event.
//
// It converts the port number to a integer if possible, otherwise
// the port number is 0.
//
// It splits the URL into its component parts, and stores the scheme,
// host, port, path and query string individually.
//
// It also calculates the duration if both startedAt and endedAt are
// passed in, for example when wrapping a http.Handler.
func HTTP(req *http.Request, statusCode int, responseContentLength int64, startedAt, endedAt *time.Time) option {
port := 0
if p := req.URL.Port(); len(p) > 0 {
port, _ = strconv.Atoi(p)
}
var duration *time.Duration
if startedAt != nil && endedAt != nil {
d := endedAt.Sub(*startedAt)
duration = &d
}
return &EventHTTP{
StatusCode: &statusCode,
Method: req.Method,
Scheme: req.URL.Scheme,
Host: req.URL.Hostname(),
Port: port,
Path: req.URL.Path,
Query: req.URL.RawQuery,
StartedAt: startedAt,
EndedAt: endedAt,
Duration: duration,
ResponseContentLength: responseContentLength,
}
}