forked from ImpactInsights/valuestream
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
239 lines (206 loc) · 5.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"context"
"contrib.go.opencensus.io/exporter/prometheus"
"fmt"
"github.com/ImpactInsights/valuestream/eventsources"
"github.com/ImpactInsights/valuestream/eventsources/github"
"github.com/ImpactInsights/valuestream/eventsources/gitlab"
customhttp "github.com/ImpactInsights/valuestream/eventsources/http"
"github.com/ImpactInsights/valuestream/eventsources/jenkins"
"github.com/ImpactInsights/valuestream/eventsources/jiracloud"
"github.com/ImpactInsights/valuestream/eventsources/webhooks"
"github.com/ImpactInsights/valuestream/tracers"
"github.com/ImpactInsights/valuestream/traces"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/mocktracer"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
"go.opencensus.io/plugin/ochttp"
"go.opencensus.io/plugin/ochttp/propagation/b3"
"go.opencensus.io/plugin/runmetrics"
"go.opencensus.io/stats/view"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
const envLogLevel string = "VS_LOG_LEVEL"
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
switch os.Getenv(envLogLevel) {
case "INFO":
log.SetLevel(log.InfoLevel)
case "WARN":
log.SetLevel(log.WarnLevel)
default:
log.SetLevel(log.DebugLevel)
}
}
func main() {
app := cli.NewApp()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "addr",
Value: "127.0.0.1:5000",
Usage: "address to start valuestream server",
EnvVar: "VS_ADDR",
},
cli.StringFlag{
Name: "tracer, t",
Value: "logging",
Usage: "tracer implementation to use: 'logger|jaeger|lightstep'",
EnvVar: "VS_TRACER_BACKEND",
},
cli.StringFlag{
Name: "tracer-access-token",
Value: "",
Usage: "Tracer access token",
EnvVar: "VS_TRACER_ACCESS_TOKEN",
},
}
app.Action = func(c *cli.Context) error {
ctx := context.Background()
// get the tracer
initialzeTracer := tracers.InitializerFromCLI(c, c.String("tracer"))
spans, err := traces.NewBufferedSpanStore(1000)
if err != nil {
return err
}
go spans.Monitor(ctx, time.Second*5, "spans")
sources := []struct {
urlPath string
name string
builderFn func(*cli.Context, opentracing.Tracer) (eventsources.EventSource, error)
}{
{
urlPath: "/github",
name: "github",
builderFn: github.NewFromCLI,
},
{
urlPath: "/gitlab",
name: "gitlab",
builderFn: gitlab.NewFromCLI,
},
{
urlPath: "/customhttp",
name: "customhttp",
builderFn: customhttp.NewFromCLI,
},
{
urlPath: "/jenkins",
name: "jenkins",
builderFn: jenkins.NewFromCLI,
},
{
urlPath: "/jira",
name: "jira",
builderFn: jiracloud.NewFromCLI,
},
}
r := mux.NewRouter()
for _, s := range sources {
log.Infof("initializing source: %q", s.name)
tracer, closer, err := initialzeTracer(ctx, s.name)
if err != nil {
return err
}
defer func() {
if err := closer.Close(); err != nil {
panic(err)
}
}()
source, err := s.builderFn(c, tracer)
if err != nil {
return err
}
webhook, err := webhooks.New(
source,
tracers.NewRequestScopedUsingSources(),
spans,
)
r.Handle(s.urlPath,
ochttp.WithRouteTag(
http.HandlerFunc(webhook.Handler),
s.urlPath,
),
)
}
if c.String("tracer") == "mock" {
tracer, _, err := initialzeTracer(ctx, "global")
if err != nil {
return err
}
if err := tracers.Register(tracer.(*mocktracer.MockTracer), spans, r); err != nil {
return err
}
}
exporter, err := prometheus.NewExporter(prometheus.Options{})
if err != nil {
return fmt.Errorf("failed to create the Prometheus exporter: %v", err)
}
r.Handle("/metrics", exporter)
if err := view.Register(
ochttp.ServerRequestCountView,
ochttp.ServerRequestBytesView,
ochttp.ServerResponseBytesView,
ochttp.ServerLatencyView,
ochttp.ServerRequestCountByMethod,
ochttp.ServerResponseCountByStatusCode,
webhooks.EventStartCountView,
webhooks.EventEndCountView,
webhooks.EventLatencyView,
); err != nil {
return fmt.Errorf("failed to register ochttp Server views: %v", err)
}
view.SetReportingPeriod(500 * time.Millisecond)
if err := runmetrics.Enable(runmetrics.RunMetricOptions{
EnableCPU: true,
EnableMemory: true,
}); err != nil {
return err
}
loggedRouter := handlers.LoggingHandler(os.Stdout, r)
srv := &http.Server{
Handler: &ochttp.Handler{
Handler: loggedRouter,
Propagation: &b3.HTTPFormat{},
},
Addr: c.String("addr"),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
go func() {
log.Infof("Starting Server: %q", c.String("addr"))
if err := srv.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
waitForShutdown(srv)
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func waitForShutdown(srv *http.Server) {
interruptChan := make(chan os.Signal, 1)
signal.Notify(interruptChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
// Block until we receive our signal.
<-interruptChan
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel()
srv.Shutdown(ctx)
log.Println("Shutting down")
os.Exit(0)
}