-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (104 loc) · 2.81 KB
/
main.go
File metadata and controls
131 lines (104 loc) · 2.81 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
129
130
131
package main
import (
"context"
"flag"
"fmt"
"net/url"
"os"
"os/signal"
"time"
"github.com/prometheus/client_golang/prometheus/collectors"
"golang.org/x/sync/errgroup"
"go.ntppool.org/common/apitls"
"go.ntppool.org/common/logger"
"go.ntppool.org/common/metricsserver"
"go.ntppool.org/common/tracing"
"go.ntppool.org/common/version"
)
func main() {
log := logger.Setup()
ctx := context.Background()
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt, os.Kill)
go func() {
<-ctx.Done()
log.Info("shutting down")
}()
err := run(ctx)
if err != nil {
log.Error("runtime error", "err", err)
}
cancel()
}
func run(ctx context.Context) error {
log := logger.Setup()
keyFile := flag.String("key", "", "client key")
certFile := flag.String("cert", "", "client certificate")
path := flag.String("path", "", "path with avro files")
upstreamAPI := flag.String("upstream", "", "log receiver URL")
jsondump := flag.Bool("jsondump", false, "dump specified files to json")
versionFlag := flag.Bool("version", false, "show version")
flag.Parse()
if *versionFlag {
fmt.Printf("logferry %s\n", version.Version())
os.Exit(0)
}
if *jsondump {
dumpJSON(flag.Args())
os.Exit(0)
}
log.Info("logferry", "version", version.Version())
// todo: unless upstream API isn't https
if len(*keyFile) == 0 || len(*certFile) == 0 {
return fmt.Errorf("-cert and -key are required")
}
cm, err := apitls.GetCertman(*certFile, *keyFile)
if err != nil {
return fmt.Errorf("certman setup: %w", err)
}
capool, err := apitls.CAPool()
if err != nil {
return err
}
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
tpShutdown, err := tracing.InitTracer(ctx, &tracing.TracerConfig{
ServiceName: "logferry",
Environment: "prod",
RootCAs: capool,
CertificateProvider: cm.GetClientCertificate,
})
if err != nil {
return err
}
<-ctx.Done()
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return tpShutdown(shutdownCtx)
})
metricssrv := metricsserver.New()
g.Go(func() error {
metricssrv.Registry().MustRegister(
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
version.RegisterMetric("logferry", metricssrv.Registry())
return metricssrv.ListenAndServe(ctx, 9097)
})
g.Go(func() error {
return directoryCleaner(ctx, *path)
})
g.Go(func() error {
upstreamURL, err := url.Parse(*upstreamAPI)
if err != nil {
return err
}
upstreamURL.Path = "/"
log.Info("upstream", "url", upstreamURL.String())
clientManager, err := NewHTTPClientManager(cm, upstreamURL.String(), metricssrv.Registry())
if err != nil {
return fmt.Errorf("http setup: %w", err)
}
defer clientManager.Stop()
return uploader(ctx, clientManager, *path, metricssrv.Registry())
})
return g.Wait()
}