This repository was archived by the owner on Apr 7, 2024. It is now read-only.
forked from mintance/nginx-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
128 lines (114 loc) · 3.34 KB
/
Copy pathmain.go
File metadata and controls
128 lines (114 loc) · 3.34 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
package main
import (
"context"
"github.com/WinnerSoftLab/nginx-clickhouse/clickhouse"
configParser "github.com/WinnerSoftLab/nginx-clickhouse/config"
"github.com/WinnerSoftLab/nginx-clickhouse/nginx"
"github.com/papertrail/go-tail/follower"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/satyrius/gonx"
"github.com/sirupsen/logrus"
"io"
"net/http"
"strings"
"sync"
"time"
)
var (
linesProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "nginx_clickhouse_lines_processed_total",
Help: "The total number of processed log lines",
})
linesNotProcessed = promauto.NewCounter(prometheus.CounterOpts{
Name: "nginx_clickhouse_lines_not_processed_total",
Help: "The total number of log lines which was not processed",
})
linesReadFailed = promauto.NewCounter(prometheus.CounterOpts{
Name: "nginx_clickhouse_lines_read_failed_total",
Help: "The total number of log lines which was not readed",
})
)
const ChanSize = 4
const BuffSize = 500000
const BuffTimeout = time.Minute * 5
var pool = sync.Pool{New: func() interface{} { return make([]string, 0, BuffSize) }}
func main() {
// Read config & incoming flags
config := configParser.Read()
nginxParser, err := nginx.GetParser(config)
if err != nil {
logrus.Fatal("Can`t parse nginx log format: ", err)
}
storage, err := clickhouse.NewStorage(config, context.Background())
if err != nil {
logrus.Fatal("Can`t connect to clickhouse: ", err)
}
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}()
ch := make(chan []string, ChanSize)
go Writer(storage, nginxParser, ch)
go Reader(config, ch)
// Не стал пока реализовывать обработку сигналов
select {}
}
func Writer(storage *clickhouse.Storage, nginxParser *gonx.Parser, ch <-chan []string) {
for pack := range ch {
logrus.Debugf("Preparing to save %d new log entries.", len(pack))
parsed, err := nginx.ParseLogs(nginxParser, pack)
if err != nil {
logrus.Errorf("Can't parse pack: %s", err)
linesNotProcessed.Add(float64(len(pack)))
continue
}
if err := storage.Save(parsed); err != nil {
logrus.Error("Can't save pack: ", err)
linesNotProcessed.Add(float64(len(pack)))
} else {
logrus.Info("Saved ", len(pack), " new logs.")
linesProcessed.Add(float64(len(pack)))
}
pack = pack[:0]
pool.Put(pack)
}
}
func Reader(config *configParser.Config, ch chan<- []string) {
whenceSeek := io.SeekStart
if config.Settings.SeekFromEnd {
whenceSeek = io.SeekEnd
}
tail, err := follower.New(config.Settings.LogPath, follower.Config{
Whence: whenceSeek,
Offset: 0,
Reopen: true,
})
if err != nil {
logrus.Fatalf("Can't tail logfile: %s", err)
}
buff := pool.Get().([]string)
timer := time.NewTimer(BuffTimeout)
for {
select {
case line := <-tail.Lines():
if tail.Err() != nil {
linesReadFailed.Add(1)
logrus.Errorf("Tail failed, error: %s", tail.Err())
}
buff = append(buff, strings.TrimSpace(line.String()))
if len(buff) >= BuffSize {
ch <- buff
buff = pool.Get().([]string)
timer = time.NewTimer(BuffTimeout)
}
case <-timer.C:
if len(buff) > 0 {
ch <- buff
buff = pool.Get().([]string)
timer = time.NewTimer(BuffTimeout)
}
}
}
}