-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
73 lines (66 loc) · 2.07 KB
/
utils.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 ginhttplogger
import (
"net/http"
"strings"
)
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
// Compute the size of request headers and flatten the header values
func normalizeHeaderMap(headerMap http.Header) (normalizedHeaderMap map[string]string, headerMapSize int) {
headerMapSize = 0
normalizedHeaderMap = make(map[string]string)
for name, value := range headerMap {
headerMapSize += len(name)
for _, v := range value {
headerMapSize += len(v)
}
normalizedHeaderMap[strings.ToLower(strings.Replace(name, "-", "_", -1))] = strings.Join(value, ", ")
}
return
}
// Formats a given payload as
func buildPayload(logEntry *Log) (logPayload AccessLog) {
// Let's normalize our headers to match Kong's format as well as our Django logger's
requestHeaders, requestHeaderSize := normalizeHeaderMap(logEntry.context.Request.Header)
responseHeaders, responseHeaderSize := normalizeHeaderMap(logEntry.responseHeaders)
// Let's parse the request and response objects and put that in a JSON-friendly map
logPayload = AccessLog{
TimeStarted: logEntry.startDate.Format("2006-01-02T15:04:05.999+0100"),
ClientAddress: logEntry.context.ClientIP(),
Time: int64(logEntry.latency.Nanoseconds() / 1000),
Request: RequestLogEntry{
Method: logEntry.context.Request.Method,
Path: logEntry.context.Request.URL.Path,
HTTPVersion: logEntry.context.Request.Proto,
Headers: requestHeaders,
HeaderSize: requestHeaderSize,
Content: HTTPContent{
Size: logEntry.context.Request.ContentLength,
MimeType: logEntry.context.ContentType(),
Content: logEntry.requestBody,
},
},
Errors: logEntry.context.Errors.String(),
Response: ResponseLogEntry{
Status: logEntry.context.Writer.Status(),
Headers: responseHeaders,
HeaderSize: int(responseHeaderSize),
Content: HTTPContent{
Size: logEntry.responseContentLength,
MimeType: responseHeaders["content_type"],
Content: logEntry.responseBody,
},
},
}
return logPayload
}