This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
hook_test.go
109 lines (90 loc) · 2.36 KB
/
hook_test.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
package hook
import (
"testing"
"github.com/sirupsen/logrus"
"fmt"
"time"
"io/ioutil"
)
const (
clickHouseHost = "clickhouse-server"
)
func TestHook(t *testing.T) {
log := logrus.New()
log.Out = ioutil.Discard
clickhouse := makeClickHouseConfig()
hook, err := NewHook(clickhouse)
if err != nil {
t.Errorf("Error when initialization hook: %s", err)
}
if err == nil {
log.AddHook(hook)
}
for i := 0; i < 100; i++ {
currentTime := time.Now()
log.WithFields(logrus.Fields{
"remote_addr": fmt.Sprint("sync_test", i),
"upstream_addr": fmt.Sprint("sync_test", i),
"request_time": fmt.Sprint("sync_test", i),
"upstream_response_time": fmt.Sprint("sync_test", i),
"request": fmt.Sprint("sync_test", i),
"status": fmt.Sprint("sync_test", i),
"bytes_sent": i,
"time": currentTime.Unix(),
"event_date": currentTime.Format("2006-01-02"),
}).Info("Sync message for clickhouse")
}
}
func TestAsyncHook(t *testing.T) {
log := logrus.New()
log.Out = ioutil.Discard
clickhouse := makeClickHouseConfig()
hook, err := NewAsyncHook(clickhouse)
if err != nil {
t.Errorf("Error when initialization hook: %s", err)
}
if err == nil {
log.AddHook(hook)
}
for i := 0; i < 100000; i++ {
currentTime := time.Now()
log.WithFields(logrus.Fields{
"remote_addr": fmt.Sprint("async_test", i),
"upstream_addr": fmt.Sprint("async_test", i),
"request_time": fmt.Sprint("async_test", i),
"upstream_response_time": fmt.Sprint("async_test", i),
"request": fmt.Sprint("async_test", i),
"status": fmt.Sprint("async_test", i),
"bytes_sent": i,
"time": currentTime.Unix(),
"event_date": currentTime.Format("2006-01-02"),
}).Info("Async message for clickhouse")
}
hook.Close()
}
func makeClickHouseConfig() *ClickHouse {
clickhouse := &ClickHouse{
Db: "logs",
Table: "nginx_logs",
Host: clickHouseHost,
Port: "8123",
Columns: []string{
"remote_addr",
"upstream_addr",
"request_time",
"upstream_response_time",
"request",
"status",
"bytes_sent",
"time",
"event_date",
},
Credentials: struct {
User string
Password string
}{
User: "default",
},
}
return clickhouse
}