forked from 42wim/rl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
104 lines (96 loc) · 2.13 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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"github.com/juju/ratelimit"
"os"
"regexp"
"strconv"
"time"
)
var (
drops int
flagRate int64
flagKeep bool
flagFile string
)
func init() {
rate, _ := strconv.Atoi(os.Getenv("LOG_RATELIMIT"))
if rate == 0 {
rate = 1000
}
flag.Int64Var(&flagRate, "r", int64(rate), "limit to r messages per second (drops those exceeding the limit)")
flag.BoolVar(&flagKeep, "k", false, "keep the messages instead of dropping them")
flag.StringVar(&flagFile, "f", "", "define a file as input")
flag.Parse()
}
func reset() {
if drops > 0 {
fmt.Fprintf(os.Stderr, "Rate-limiting to %d loglines/second. Suppressed %d.\n", flagRate, drops)
drops = 0
}
}
func parseMendixLogline(line string) string {
// Match ISO 8601 timestamp string - 2022-10-13 12:26:07.623
re := regexp.MustCompile(`^\d{4}(.\d{2}){2}(\s|T)(\d{2}.){2}\d{2}.\d+`)
return re.ReplaceAllString(line, "")
}
func openFile(file string) (*os.File, error) {
if _, err := os.Stat(file); err != nil {
return nil, errors.New(fmt.Sprintf("Failed to open \"%s\" (file does not exist)", file))
}
input, err := os.Open(file)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to open \"%s\" (%s)", file, err))
}
return input, nil
}
func main() {
var input *os.File
if flagFile != "" {
var err error
input, err = openFile(flagFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
} else {
input = os.Stdin
}
scanner := bufio.NewScanner(input)
c := make(chan string)
done := make(chan bool)
go func(c chan string, done chan bool) {
for scanner.Scan() {
c <- scanner.Text()
}
close(done)
}(c, done)
l := ratelimit.NewBucket(time.Second/time.Duration(flagRate), flagRate)
timer := time.NewTicker(time.Second)
for {
select {
case line := <-c:
if flagKeep {
l.Wait(1)
fmt.Println(parseMendixLogline(line))
} else {
if l.TakeAvailable(1) > 0 {
// Replace the timestamp from the logs
fmt.Println(parseMendixLogline(line))
reset()
} else {
drops++
}
}
case <-timer.C:
if l.Available() == flagRate {
reset()
}
case <-done:
reset()
return
}
}
}