-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
183 lines (164 loc) · 4.41 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
package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/pkg/profile"
"github.com/urfave/cli/v2"
"github.com/probe-lab/thunderdome/pkg/loki"
"github.com/probe-lab/thunderdome/pkg/prom"
"github.com/probe-lab/thunderdome/pkg/run"
)
const appName = "skyfish"
var app = &cli.App{
Name: appName,
Action: Run,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "loki-uri",
Usage: "URI of the loki server when using loki as a request source.",
Value: "",
Destination: &flags.lokiURI,
EnvVars: []string{"SKYFISH_LOKI_URI"},
},
&cli.StringFlag{
Name: "loki-username",
Usage: "Username to use when using loki as a request source.",
Value: "",
Destination: &flags.lokiUsername,
EnvVars: []string{"SKYFISH_LOKI_USERNAME"},
},
&cli.StringFlag{
Name: "loki-password",
Usage: "Password to use when using loki as a request source.",
Value: "",
Destination: &flags.lokiPassword,
EnvVars: []string{"SKYFISH_LOKI_PASSWORD"},
},
&cli.StringFlag{
Name: "loki-query",
Usage: "Query to use when using loki as a request source.",
Value: "",
Destination: &flags.lokiQuery,
EnvVars: []string{"SKYFISH_LOKI_QUERY"},
},
&cli.StringFlag{
Name: "sns-topic",
Usage: "ARN of sns topic to publish to.",
Value: "",
Destination: &flags.topicArn,
EnvVars: []string{"SKYFISH_TOPIC"},
},
&cli.StringFlag{
Name: "sns-region",
Usage: "AWS region to use when connecting to sns.",
Value: "eu-west-1",
Destination: &flags.snsRegion,
EnvVars: []string{"SKYFISH_SNS_REGION"},
},
&cli.StringFlag{
Name: "prometheus-addr",
Usage: "Network address to start a prometheus metric exporter server on (example: :9991)",
Value: "",
Destination: &flags.prometheusAddr,
EnvVars: []string{"SKYFISH_PROMETHEUS_ADDR"},
},
&cli.StringFlag{
Name: "cpuprofile",
Usage: "Write a CPU profile to the specified file before exiting.",
Value: "",
Destination: &flags.cpuprofile,
EnvVars: []string{"SKYFISH_CPUPROFILE"},
},
&cli.StringFlag{
Name: "memprofile",
Usage: "Write an allocation profile to the file before exiting.",
Value: "",
Destination: &flags.memprofile,
EnvVars: []string{"SKYFISH_MEMPROFILE"},
},
},
}
var flags struct {
prometheusAddr string
cpuprofile string
memprofile string
lokiURI string
lokiUsername string
lokiPassword string
lokiQuery string
topicArn string
snsRegion string
}
func main() {
log.SetFlags(log.LstdFlags | log.LUTC | log.Lshortfile)
ctx := context.Background()
if err := app.RunContext(ctx, os.Args); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}
func Run(cc *cli.Context) error {
ctx := cc.Context
cfg := &loki.LokiConfig{
AppName: appName,
URI: flags.lokiURI,
Username: flags.lokiUsername,
Password: flags.lokiPassword,
Query: flags.lokiQuery,
}
rg := new(run.Group)
source, err := loki.NewLokiTailer(cfg)
if err != nil {
return fmt.Errorf("loki source: %w", err)
}
rg.Add(source)
if flags.prometheusAddr != "" {
ps, err := prom.NewPrometheusServer(flags.prometheusAddr, "/metrics", appName)
if err != nil {
return fmt.Errorf("start prometheus: %w", err)
}
rg.Add(Restartable{ps})
}
awscfg := aws.NewConfig()
awscfg.Region = aws.String(flags.snsRegion)
awscfg.WithHTTPClient(&http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
Timeout: 10 * time.Second,
})
publisher, err := NewPublisher(awscfg, flags.topicArn, source.Chan())
if err != nil {
return fmt.Errorf("new publisher: %w", err)
}
rg.Add(publisher)
rg.Add(new(Health))
if flags.cpuprofile != "" {
defer profile.Start(profile.CPUProfile, profile.ProfileFilename(flags.cpuprofile)).Stop()
}
if flags.memprofile != "" {
defer profile.Start(profile.MemProfile, profile.ProfileFilename(flags.memprofile)).Stop()
}
return rg.RunAndWait(ctx)
}
type Restartable struct {
run.Runnable
}
func (r Restartable) Run(ctx context.Context) error {
for {
if ctx.Err() != nil {
return ctx.Err()
}
if err := r.Runnable.Run(ctx); err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return err
}
}
}
}