forked from grafana/detect-angular-dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
262 lines (224 loc) · 6.83 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"context"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/grafana/detect-angular-dashboards/api"
"github.com/grafana/detect-angular-dashboards/api/gcom"
"github.com/grafana/detect-angular-dashboards/api/grafana"
"github.com/grafana/detect-angular-dashboards/build"
"github.com/grafana/detect-angular-dashboards/detector"
"github.com/grafana/detect-angular-dashboards/flags"
"github.com/grafana/detect-angular-dashboards/logger"
"github.com/grafana/detect-angular-dashboards/output"
)
const envGrafana = "GRAFANA_TOKEN"
type Output struct {
mu sync.Mutex
data []output.Dashboard
}
func main() {
f := flags.Parse()
if f.Version {
fmt.Printf("%s %s (%s)\n", os.Args[0], build.LinkerVersion, build.LinkerCommitSHA)
os.Exit(0)
}
log := newLogger(f.Verbose, f.JSONOutput)
token, err := getToken()
if err != nil {
log.Errorf("Failed to retrieve Grafana token: %s\n", err.Error())
os.Exit(1)
}
client := initializeClient(token, &f)
d := detector.NewDetector(log, client, gcom.NewAPIClient(), f.MaxConcurrency)
if f.Server != "" {
if err := runServerMode(&f, log, d); err != nil {
log.Errorf("%s\n", err)
os.Exit(1)
}
return
}
if err := runCLIMode(&f, log, d); err != nil {
log.Errorf("%s\n", err)
os.Exit(1)
}
}
// runServerMode runs the program in server (HTTP) mode.
func runServerMode(flags *flags.Flags, log *logger.LeveledLogger, d *detector.Detector) error {
// Readiness flag using atomic boolean
var ready atomic.Bool
var once sync.Once
ticker := time.NewTicker(flags.Interval)
defer ticker.Stop()
log.Log("Running detection every %s", flags.Interval)
var out Output
go func() {
// Trigger for the first time
run := make(chan struct{}, 1)
run <- struct{}{}
for {
select {
case <-run:
case <-ticker.C:
}
// Run detection periodically
log.Log("Detecting Angular dashboards")
data, err := d.Run(context.Background())
if err != nil {
log.Errorf("%s\n", err)
continue
}
// Run detection periodically
log.Log("Updating Output Data")
out.mu.Lock()
out.data = data
out.mu.Unlock()
// Use sync.Once to set readiness only once
once.Do(func() {
ready.Store(true)
log.Log("Updating readiness probe to ready")
})
}
}()
http.HandleFunc("/detections", func(w http.ResponseWriter, r *http.Request) {
handleDetectionsRequest(w, r, &out, log)
})
http.HandleFunc("/ready", func(w http.ResponseWriter, r *http.Request) {
handleReadyRequest(w, r, &ready)
})
if err := runServer(flags, log); err != nil {
log.Error("runServer Failed with the following err: %v", err)
return err
}
return nil
}
func runServer(flags *flags.Flags, log *logger.LeveledLogger) error {
server := &http.Server{Addr: flags.Server}
// Channel to listen for OS signals
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
// Start the server in a goroutine
go func() {
log.Log("Listening on %s", flags.Server)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error("ListenAndServe(): %s", err)
}
}()
// Wait for a signal interrupt
sig := <-sigChan
log.Log("Received signal: %s. Shutting down server...", sig)
// Gracefully shut down the server
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Error("Server Shutdown Failed:%+v", err)
return err
}
log.Log("Server gracefully stopped")
return nil
}
// runCLIMode runs the program in CLI mode.
func runCLIMode(flags *flags.Flags, log *logger.LeveledLogger, d *detector.Detector) error {
log.Log("Detecting Angular dashboards")
var out output.Outputter
if flags.JSONOutput {
out = output.NewJSONOutputter(os.Stdout)
} else {
out = output.NewLoggerReadableOutput(log)
}
data, err := d.Run(context.Background())
if err != nil {
return fmt.Errorf("run detector: %w", err)
}
if err := out.Output(data); err != nil {
return fmt.Errorf("output: %w", err)
}
return nil
}
// initializeClient initializes the Grafana API client.
func initializeClient(token string, flags *flags.Flags) grafana.APIClient {
grafanaURL := grafana.DefaultBaseURL
if flag.NArg() >= 1 {
grafanaURL = flag.Arg(0)
}
opts := []api.ClientOption{api.WithAuthentication(token)}
if flags.SkipTLS {
opts = append(opts, api.WithHTTPClient(&http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}))
}
return grafana.NewAPIClient(api.NewClient(grafanaURL, opts...))
}
// handleDetectionsRequest handles the /output HTTP endpoint.
func handleDetectionsRequest(w http.ResponseWriter, r *http.Request, output *Output, log *logger.LeveledLogger) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
output.mu.Lock()
defer output.mu.Unlock()
w.Header().Set("Content-Type", "application/json")
// Have to do this because the JSONOutputter.Output method modifies the slice in place
// which results in werid bug where the slice gets duplicate entries. The number of duplicate entries
// continues to grow with each request to /output. Something is leaky
angularDashboards := filterAngularDashboards(output.data)
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(angularDashboards); err != nil {
log.Errorf("http server: %s\n", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// handleReadyRequest handles the /ready HTTP endpoint.
func handleReadyRequest(w http.ResponseWriter, r *http.Request, ready *atomic.Bool) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
if ready.Load() {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Ready"))
} else {
w.WriteHeader(http.StatusServiceUnavailable)
w.Write([]byte("Not Ready"))
}
}
// filterAngularDashboards filters dashboards to include only those with detections.
func filterAngularDashboards(dashboards []output.Dashboard) []output.Dashboard {
var angularDashboards []output.Dashboard
for _, dashboard := range dashboards {
if len(dashboard.Detections) > 0 {
angularDashboards = append(angularDashboards, dashboard)
}
}
return angularDashboards
}
// newLogger initializes a new leveled logger.
func newLogger(verbose, jsonOutputFlag bool) *logger.LeveledLogger {
log := logger.NewLeveledLogger(verbose)
if jsonOutputFlag {
// Redirect everything to stderr to avoid mixing with json output
log.Logger.SetOutput(os.Stderr)
log.WarnLogger.SetOutput(os.Stderr)
}
return log
}
// getToken retrieves the Grafana token from the environment.
func getToken() (string, error) {
token := os.Getenv(envGrafana)
if token == "" {
return "", fmt.Errorf("environment variable %s is not set", envGrafana)
}
return token, nil
}