-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
188 lines (167 loc) · 4.94 KB
/
main.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const version = "1.9"
const scanSize = 24
func main() {
fs := flag.NewFlagSet("fancy", flag.ExitOnError)
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Use fancy %s like: %s [option]\n", version, os.Args[0])
fs.PrintDefaults()
}
var (
cmd = fs.String("cmd", "", "Send input msg to external command and use it's output as new msg")
lokiURL = fs.String("loki-url", "http://localhost:3100", "Loki Server URL")
lokiChanSize = fs.Int("loki-chan-size", 10000, "Loki buffered channel capacity")
lokiBatchSize = fs.Int("loki-batch-size", 1024*1024, "Loki will batch these bytes before sending them")
lokiBatchWait = fs.Int("loki-batch-wait", 4, "Loki will send logs after these seconds")
promOnly = fs.Bool("prom-only", false, "Only metrics for Prometheus will be exposed")
promAddr = fs.String("prom-addr", ":9090", "Prometheus scrape endpoint address")
staticTag = fs.String("static-tag", "", "Will be used as a static label value with the name static_tag")
staticTagFilter = fs.String("static-tag-filter", "", "Set static-tag only when msg contains this string")
)
fs.Parse(os.Args[1:])
t := time.Now()
defer fmt.Fprintf(os.Stderr, "%v end fancy with flags %s\n", t, os.Args[1:])
input := &Input{
cmd: strings.Fields(*cmd),
promOnly: *promOnly,
staticTag: *staticTag,
staticTagFilter: []byte(*staticTagFilter),
scanChan: make(chan [scanSize][]byte, 1000),
}
if *promOnly {
go func() {
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(*promAddr, nil)
if err != nil {
fmt.Fprintf(os.Stderr, "%v ERROR: %v\n", t, err)
os.Exit(1)
}
}()
} else if len(*lokiURL) > 3 {
input.useLoki = true
input.lineChan = make(chan *LogLine, *lokiChanSize)
defer close(input.lineChan)
l, err := NewLoki(input.lineChan, *lokiURL, *lokiBatchSize, *lokiBatchWait)
if err != nil {
fmt.Fprintf(os.Stderr, "%v ERROR: %v\n", t, err)
os.Exit(1)
}
go l.Run()
}
fmt.Fprintf(os.Stderr, "%v run fancy v.%s with flags %s\n", time.Now(), version, os.Args[1:])
for i := 0; i < 8; i++ {
go input.process()
}
input.scan(os.Stderr, os.Stdin)
}
var (
logScanNumber = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "fancy_input_scan_total",
Help: "Total number of logs received from rsyslog fancy template"},
[]string{"hostname", "program", "level", "static_tag"})
logScanSize = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "fancy_input_raw_bytes_total",
Help: "Total number of bytes received from rsyslog fancy template"},
[]string{"hostname", "program"})
)
type Input struct {
cmd []string
cache Cache
useLoki bool
scanChan chan [scanSize][]byte
lineChan chan *LogLine
promOnly bool
staticTag string
staticTagFilter []byte
}
type Cache struct {
buf [scanSize][]byte
pos int
}
func batchScan(c chan [scanSize][]byte, cache *Cache, value []byte) {
cache.buf[cache.pos] = value
cache.pos++
if cache.pos == scanSize {
c <- cache.buf
cache.pos = 0
}
}
func (in *Input) scan(stderr io.Writer, stdin io.Reader) {
var err error
r := bufio.NewReader(stdin)
line := make([]byte, 0, 8192)
defer close(in.scanChan)
for {
line, err = r.ReadBytes('\n')
if err != nil {
if err == io.EOF {
fmt.Fprintf(stderr, "%v INFO: %v\n", time.Now(), err)
break
}
fmt.Fprintf(stderr, "%v ERROR: %v\n", time.Now(), err)
break
}
batchScan(in.scanChan, &in.cache, line)
}
}
func (in *Input) process() {
t := time.Now()
staticTag := in.staticTag
for s := range in.scanChan {
for i := 0; i < len(s); i++ {
ll, err := parseLine(s[i], in.promOnly)
if err != nil {
fmt.Fprintf(os.Stderr, "%v ERROR: %v\n", time.Now(), err)
continue
}
if len(in.staticTagFilter) > 0 {
staticTag = ""
if bytes.Contains(ll.Raw[ll.MsgPos:], in.staticTagFilter) {
staticTag = in.staticTag
}
}
ll.StaticTag = staticTag
if in.promOnly {
rawSize := float64(len(ll.Raw))
logScanNumber.WithLabelValues(ll.Hostname, ll.Program, ll.Severity, staticTag).Inc()
logScanSize.WithLabelValues(ll.Hostname, ll.Program).Add(rawSize)
continue
}
if len(in.cmd) > 0 && in.useLoki {
c := exec.Command(in.cmd[0], in.cmd[1:]...)
c.Stdin = bytes.NewReader(ll.Raw[ll.MsgPos:])
out, err := c.Output()
if err != nil {
fmt.Fprintf(os.Stderr, "%v ERROR: %v\n", time.Now(), err)
continue
}
ll.Msg = string(out)
}
if in.useLoki {
select {
case in.lineChan <- ll:
default:
if time.Since(t) > 1e9 {
fmt.Fprintf(os.Stderr, "%v ERROR: overflowing Loki buffered channel capacity\n", t)
}
t = time.Now()
}
}
}
}
}