This repository was archived by the owner on Aug 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
102 lines (84 loc) · 2.95 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
package main
import (
"context"
"fmt"
"os"
"github.com/coinbase/watchdog/config"
"github.com/coinbase/watchdog/controller"
"github.com/coinbase/watchdog/primitives/datadog/client"
"github.com/coinbase/watchdog/server"
"github.com/sirupsen/logrus"
)
func main() {
version, err := NewVersion()
if err != nil {
logrus.Errorf("version was not set. Please use Makefile to set build SHA and time.")
}
if len(os.Args) > 1 && os.Args[1] == "version" {
fmt.Println(version.String())
return
}
cfg, err := config.NewConfig(context.Background(), nil, nil)
if err != nil {
logrus.Fatalf("unable to initialize a config: %s", err)
}
// enable the json logging if value is set
if cfg.GetLoggingJSON() {
logrus.SetFormatter(&logrus.JSONFormatter{})
}
// redirect all logging output to stdout
logrus.SetOutput(os.Stdout)
// set the requested logging level
loggingLevel := cfg.GetLoggingLevel()
if loggingLevel != "" {
level, err := logrus.ParseLevel(loggingLevel)
if err != nil {
logrus.Fatalf("unable to parse logging level %s: %s", loggingLevel, err)
}
logrus.SetLevel(level)
}
// setup default fields to be remove from dashboard/monitor/screen board response.
clientOptions := []client.Option{
client.WithRemoveDashboardFields([]string{"dash.modified"}),
client.WithRemoveMonitorFields([]string{"modified", "overall_state", "overall_state_modified"}, []string{"state"}),
client.WithRemoveScreenBoardFields([]string{"modified"}),
}
// construct the controller options
options := []controller.Option{
controller.WithDatadog(cfg.GetDatadogAPIKey(), cfg.GetDatadogAPPKey(), clientOptions...),
controller.WithGithub(cfg.GetGithubProjectOwner(), cfg.GetGithubRepo(), cfg.GithubAPIURL(),
cfg.GetGithubIntegrationID(), cfg.GetGithubAppInstallationID(), cfg.GithubAppPrivateKeyBytes()),
controller.WithSSHGit(cfg.GitURL(), cfg.GitUser(), cfg.GitEmail(), cfg.GithubAppPrivateKeyBytes(), cfg.GetIgnoreKnownHosts()),
}
// use polling scheduler based on config
// TODO: refactor this part
switch cfg.GetDatadogPollingScheduler() {
case "simple":
options = append(options, controller.WithSimplePollster(cfg.GetDatadogPollingInterval(), cfg))
default:
logrus.Fatalf("invalid datadog polling scheduler %s", cfg.GetDatadogPollingScheduler())
}
// initialize a new watchdog controller
c, err := controller.New(cfg, options...)
if err != nil {
logrus.Fatalf("unable to initialize controller %s", err)
}
// Start the polling scheduler in the background
go c.PollDatadog(context.Background())
// setup http router
routerOpts := []server.Option{
server.WithController(c),
server.WithGithubWebhook(cfg.GetGithubWebhookSecret()),
}
if version != nil {
routerOpts = append(routerOpts, server.WithVersion(version))
}
router, err := server.New(cfg, routerOpts...)
if err != nil {
logrus.Fatalf("unable to create a new HTTP server %s", err)
}
err = router.Start()
if err != nil {
logrus.Fatalf("unable to start an HTTP server %s", err)
}
}