-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture.go
More file actions
304 lines (255 loc) · 7.11 KB
/
capture.go
File metadata and controls
304 lines (255 loc) · 7.11 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"bufio"
"bytes"
"context"
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"io"
"log/slog"
"net"
"strings"
"time"
"unsafe"
"github.com/go-sql-driver/mysql"
)
type captureTask struct {
name string
fn CaptureFunc
}
type CaptureFunc func(ctx context.Context, db *sql.DB, writer Writer) error
var allTasks = []captureTask{
{"processlist", captureProcesslist()},
{"innodb", captureInnodb()},
{"status", captureStatus()},
}
func shouldRetry(err error) bool {
if errors.Is(err, context.DeadlineExceeded) {
return true
}
var netErr net.Error
if errors.As(err, &netErr) {
return true
}
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
return true
}
if errors.Is(err, driver.ErrBadConn) {
return true
}
if errors.Is(err, mysql.ErrInvalidConn) {
return true
}
var mysqlErr *mysql.MySQLError
if errors.As(err, &mysqlErr) {
if mysqlErr.Number == 1040 || mysqlErr.Number == 1053 || mysqlErr.Number == 1105 {
return true
}
return false
}
return false
}
func capture(ctx context.Context, db *sql.DB, name string, fn CaptureFunc) error {
writer := NewRotatingLogWriter(options.Path, name)
defer writer.Close()
timer := time.NewTimer(0)
for {
select {
case <- ctx.Done():
slog.Info("Capture stopped", "type", name)
return nil
case now := <- timer.C:
timer.Reset(options.Interval)
if err := writer.EnsureRotated(); err != nil {
slog.Error("Failed to rotate log file", "type", name, "error", err)
continue
}
fmt.Fprintf(writer, "---------+ TS %s ---------------------------------------------\n", now.Format(time.RFC3339))
if err := fn(ctx, db, writer); err != nil {
if errors.Is(err, context.Canceled) {
return nil
}
fmt.Fprintln(writer, "Capture error:", err.Error())
if !shouldRetry(err) {
slog.Error("Fatal capture error", "type", name, "error", err)
return err
}
slog.Error("Capture error", "type", name, "error", err)
}
fmt.Fprintf(writer, "---------+ --------------------------------------------------------------------------\n\n")
writer.Flush()
}
}
}
func captureInnodb() func (ctx context.Context, db *sql.DB, writer Writer) error {
return func(ctx context.Context, db *sql.DB, writer Writer) error {
var (
engineType string
engineName string
engineStatus string
)
results, err := db.QueryContext(ctx, "SHOW ENGINE INNODB STATUS")
if err != nil {
return err
}
now := time.Now()
for results.Next() {
err := results.Scan(&engineType, &engineName, &engineStatus)
if err != nil {
return err
}
reader := strings.NewReader(engineStatus)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
fmt.Fprintf(writer, "%s | %s\n", now.Format("15:04:05"), scanner.Text())
}
}
return nil
}
}
func writeInSingleLineUnsafe(w io.Writer, s string) {
b := unsafe.Slice(unsafe.StringData(s), len(s))
p := 0
for {
i := bytes.IndexByte(b[p:], '\n')
if i == -1 {
w.Write(b[p:])
break
}
w.Write(b[p : p+i])
w.Write([]byte{' '})
p += i + 1
}
}
func captureProcesslist() func(ctx context.Context,db *sql.DB, writer Writer) error {
return func(ctx context.Context, db *sql.DB, writer Writer) error {
type ProcessList struct {
Id uint64
User sql.NullString
Host sql.NullString
Db sql.NullString
Command sql.NullString
Time int32
State sql.NullString
Info sql.NullString
TimeMs int64
RowsSent int64
RowsExamined int64
}
results, err := db.QueryContext(ctx, "SHOW FULL PROCESSLIST")
if err != nil {
return err
}
columns, err := results.Columns()
if err != nil {
return err
}
record := make([]interface{}, len(columns))
record[0] = new(uint64) // Id
record[1] = new(sql.NullString) // User
record[2] = new(sql.NullString) // Host
record[3] = new(sql.NullString) // Db
record[4] = new(sql.NullString) // Command
record[5] = new(int32) // Time
record[6] = new(sql.NullString) // State
record[7] = new(sql.NullString) // Info
switch len(columns) {
case 8:
// nothing to do
case 10:
record[8] = new(int64) // RowsSent
record[9] = new(int64) // RowsExamined
case 11:
record[8] = new(int64) // TimeMs
record[9] = new(int64) // RowsSent
record[10] = new(int64) // RowsExamined
default:
return errors.New("Unexpected number of columns in processlist")
}
now := time.Now()
for results.Next() {
err := results.Scan(record...)
if err != nil {
return err
}
pl := &ProcessList{
Id: *record[0].(*uint64),
User: *record[1].(*sql.NullString),
Host: *record[2].(*sql.NullString),
Db: *record[3].(*sql.NullString),
Command: *record[4].(*sql.NullString),
Time: *record[5].(*int32),
State: *record[6].(*sql.NullString),
Info: *record[7].(*sql.NullString),
}
switch len(record) {
case 8:
// nothing to do
case 10:
pl.RowsSent = *record[8].(*int64)
pl.RowsExamined = *record[9].(*int64)
case 11:
pl.TimeMs = *record[8].(*int64)
pl.RowsSent = *record[9].(*int64)
pl.RowsExamined = *record[10].(*int64)
default:
return errors.New("Unexpected number of columns in processlist")
}
timestamp := now.Format("15:04:05")
switch len(record) {
case 8:
fmt.Fprintf(writer, "%s | %d\t%-12s\t%-32s\t%-12s\t%-10s\t%d\t\t%-10s\t",
timestamp, pl.Id, pl.User.String, pl.Host.String, pl.Db.String,
pl.Command.String, pl.Time, pl.State.String)
writeInSingleLineUnsafe(writer, pl.Info.String)
fmt.Fprintln(writer)
case 10:
fmt.Fprintf(writer, "%s | %d\t%-12s\t%-32s\t%-12s\t%-10s\t%d\t\t%-10s\t%d\t%d\t",
timestamp, pl.Id, pl.User.String, pl.Host.String, pl.Db.String,
pl.Command.String, pl.Time, pl.State.String, pl.RowsSent, pl.RowsExamined)
writeInSingleLineUnsafe(writer, pl.Info.String)
fmt.Fprintln(writer)
case 11:
fmt.Fprintf(writer, "%s | %d\t%-12s\t%-32s\t%-12s\t%-10s\t%d\t%d\t\t%-10s\t%d\t%d\t",
timestamp, pl.Id, pl.User.String, pl.Host.String, pl.Db.String,
pl.Command.String, pl.Time, pl.TimeMs, pl.State.String, pl.RowsSent, pl.RowsExamined)
writeInSingleLineUnsafe(writer, pl.Info.String)
fmt.Fprintln(writer)
default:
return errors.New("Unexpected number of columns in processlist")
}
}
return nil
}
}
func captureStatus() func(ctx context.Context, db *sql.DB, writer Writer) error {
return func(ctx context.Context, db *sql.DB, writer Writer) error {
type Variable struct {
Name sql.NullString
Value sql.NullString
}
record := []interface{} {
new(sql.NullString),
new(sql.NullString),
}
results, err := db.QueryContext(ctx, "SHOW GLOBAL STATUS")
if err != nil {
return err
}
now := time.Now()
for results.Next() {
err := results.Scan(record...)
if err != nil {
return err
}
v := &Variable{
Name: *record[0].(*sql.NullString),
Value: *record[1].(*sql.NullString),
}
fmt.Fprintf(writer, "%s | %s = %s\n", now.Format("15:04:05"), v.Name.String, v.Value.String)
}
return nil
}
}