-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
59 lines (48 loc) · 1.09 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
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/metal-toolbox/trivy-extractor/internal/trivy"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
handleMetrics(ctx)
eg, groupCtx := errgroup.WithContext(ctx)
eg.Go(func() error {
return trivy.Report(
&trivy.MetricsServicer{},
trivy.NewPrometheusMetricsService(),
groupCtx,
time.Second*15,
trivy.NewNamespaceTeam("/data/namespaces.csv"),
)
})
eg.Wait()
}
func handleMetrics(ctx context.Context) {
server := &http.Server{
Addr: ":2112",
ReadTimeout: 3 * time.Second,
ReadHeaderTimeout: 3 * time.Second,
}
http.Handle("/metrics", promhttp.Handler())
go func() {
log.Printf("starting HTTP server on address '%s'...", server.Addr)
if err := server.ListenAndServe(); err != nil {
return
}
}()
go func() {
<-ctx.Done()
log.Println("stopping HTTP server...")
server.Shutdown(ctx)
}()
}