-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
108 lines (94 loc) · 2.44 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
package main
import (
"errors"
"flag"
"io"
"os"
"github.com/go-kit/log"
"github.com/grafana/go-test-runner/internal/cfg"
"github.com/grafana/go-test-runner/internal/console"
"github.com/grafana/go-test-runner/internal/loki"
"github.com/grafana/go-test-runner/internal/tests"
)
type eventHandler interface {
Handle(tests.Event) error
}
type stoppable interface {
Stop()
}
func main() {
conf := cfg.Config{}
fields := cfg.Tags{}
flag.Var(&fields, "t", "Add a key=value pair to the log output for each test")
file := flag.String("c", "", "Path to configuration file")
flag.Parse()
logger := log.NewLogfmtLogger(os.Stderr)
if *file != "" {
f, err := os.Open(*file)
if err != nil {
logger.Log("msg", "Failed to open configuration file", "filename", *file, "error", err)
os.Exit(-1)
}
conf, err = conf.Parse(*file, f)
if err != nil {
logger.Log("msg", "Failed to parse configuration file", "filename", *file, "error", err)
os.Exit(-1)
}
}
tracingOptions, traceErr := conf.Tracing()
lokiOptions, lokiErr := conf.Loki()
consoleOptions, consoleErr := conf.Console()
grafanaOptions, grafanaErr := conf.Grafana()
if err := errors.Join(traceErr, lokiErr, consoleErr, grafanaErr); err != nil {
logger.Log("msg", "Failed to parse configuration for services", "error", err)
os.Exit(-1)
}
r, err := tests.New(fields, tracingOptions)
if err != nil {
logger.Log("msg", "Failed to initialize test parser", "error", err)
os.Exit(-1)
}
r.CollectionDivider = "/"
logClient, err := loki.New(r, lokiOptions)
if err != nil {
logger.Log("msg", "Failed to initialize Loki sender", "error", err)
os.Exit(-1)
}
handlers := []eventHandler{
r,
logClient,
console.New(r.TraceID, consoleOptions, grafanaOptions),
}
failCount := 0
goJSON := tests.NewGoJSON(os.Stdin)
for {
es, err := goJSON.ReadLine()
if err != nil {
failCount++
if err == io.EOF {
break
}
logger.Log("msg", "Error parsing line from `go test -json`!", "error", err)
if failCount > 9 {
logger.Log("msg", "Too many subsequent parsing errors, stopping processing", "error", err)
os.Exit(-1)
}
continue
} else {
failCount = 0
}
for _, e := range es {
for _, handler := range handlers {
err := handler.Handle(e)
if err != nil {
logger.Log("msg", "Error from handler '%T': %v", handler, err)
}
}
}
}
for _, handler := range handlers {
if stopper, ok := handler.(stoppable); ok {
stopper.Stop()
}
}
}