-
Notifications
You must be signed in to change notification settings - Fork 0
/
logwriter.go
189 lines (165 loc) · 3.56 KB
/
logwriter.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"bytes"
"log"
"net"
"os"
"strconv"
"time"
"database/sql"
"net/http"
"github.com/json-iterator/go"
"github.com/kshvakov/clickhouse"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
func logger(logs chan *http.Request) {
// Make DB connection string
var connectParams = struct {
host,
port,
debug,
user,
password,
DB string
}{
"127.0.0.1",
":9000",
"?debug=false",
"",
"",
"",
}
if v, present := os.LookupEnv("CH_HOST"); present {
connectParams.host = v
}
if v, present := os.LookupEnv("CH_PORT"); present {
connectParams.port = ":" + v
}
if v, present := os.LookupEnv("CH_DEBUG"); present && v == "true" {
connectParams.debug = "?debug=true"
}
if v, present := os.LookupEnv("CH_USER"); present {
connectParams.user = "&username=" + v
}
if v, present := os.LookupEnv("CH_PASSWORD"); present {
connectParams.password = "&password=" + v
}
if v, present := os.LookupEnv("CH_DB"); present {
connectParams.DB = "&database=" + v
}
// Prepare DB connection
connect, err := sql.Open("clickhouse", "tcp://"+
connectParams.host+
connectParams.port+
connectParams.debug+
connectParams.user+
connectParams.password+
connectParams.DB+
"")
if err != nil {
log.Fatal(err)
}
connect.SetConnMaxLifetime(4 * time.Minute) // ClickHouse will drop connection after 5 minues of inactive
connect.SetMaxIdleConns(1)
connect.SetMaxOpenConns(4)
// Check DB connection
if err := connect.Ping(); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
log.Printf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
} else {
log.Println(err)
}
return
}
_, err = connect.Exec(`
CREATE TABLE IF NOT EXISTS access (
date Date DEFAULT today(),
dt DateTime DEFAULT now(),
method String,
uri String,
proto String,
header String,
body String,
host String,
remote_host String,
remote_port UInt16,
buffer_used UInt32,
buffer_size UInt32,
response String,
code UInt16
) ENGINE = MergeTree(date, (date), 8192)
`)
if err != nil {
log.Fatal(err)
}
// Insert data from channel
for {
var (
tx *sql.Tx
stmt *sql.Stmt
err error
)
// Wait (blocking) for first element in channel and iterate them
for i := range logs {
if tx == nil {
// Prepare query
tx, _ = connect.Begin()
stmt, err = tx.Prepare(`INSERT INTO access (
method,
uri,
proto,
header,
body,
host,
remote_host,
remote_port,
buffer_used,
buffer_size,
response,
code
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
if err != nil {
log.Fatal(err)
}
}
// Prepare values
// Header as JSON
headerJSON, _ := json.Marshal(&i.Header)
// Body as string
bodyBuf := new(bytes.Buffer)
bodyBuf.ReadFrom(i.Body)
bodyStr := bodyBuf.String()
// Remote host/port
remoteHost, remotePortStr, _ := net.SplitHostPort(i.RemoteAddr)
var remotePort uint16
if v, err := strconv.ParseUint(remotePortStr, 10, 16); err == nil {
remotePort = uint16(v)
}
// Make query
if _, err := stmt.Exec(
i.Method,
i.RequestURI,
i.Proto,
headerJSON,
bodyStr,
i.Host,
remoteHost,
remotePort,
len(logs),
bufferSize,
generateAnswer(i),
redirectCode,
); err != nil {
log.Println(err)
}
// If no items left - commit query and whait for next batch
if len(logs) == 0 {
break
}
}
if err := tx.Commit(); err != nil {
log.Println(err)
}
stmt.Close()
}
}