-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
93 lines (79 loc) · 2.78 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
package main
import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
dtrack "github.com/DependencyTrack/client-go"
"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log/level"
"github.com/jetstack/dependency-track-exporter/internal/exporter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/promlog"
"github.com/prometheus/common/promlog/flag"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
webflag "github.com/prometheus/exporter-toolkit/web/kingpinflag"
)
const (
envAddress string = "DEPENDENCY_TRACK_ADDR"
envAPIKey string = "DEPENDENCY_TRACK_API_KEY"
)
func init() {
prometheus.MustRegister(version.NewCollector(exporter.Namespace + "_exporter"))
}
func main() {
var (
webConfig = webflag.AddFlags(kingpin.CommandLine, ":9916")
metricsPath = kingpin.Flag("web.metrics-path", "Path under which to expose metrics").Default("/metrics").String()
dtAddress = kingpin.Flag("dtrack.address", fmt.Sprintf("Dependency-Track server address (can also be set with $%s)", envAddress)).Default("http://localhost:8080").Envar(envAddress).String()
dtAPIKey = kingpin.Flag("dtrack.api-key", fmt.Sprintf("Dependency-Track API key (can also be set with $%s)", envAPIKey)).Envar(envAPIKey).Required().String()
promlogConfig = promlog.Config{}
)
flag.AddFlags(kingpin.CommandLine, &promlogConfig)
kingpin.Version(version.Print(exporter.Namespace + "_exporter"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
logger := promlog.New(&promlogConfig)
level.Info(logger).Log("msg", fmt.Sprintf("Starting %s_exporter %s", exporter.Namespace, version.Info()))
level.Info(logger).Log("msg", fmt.Sprintf("Build context %s", version.BuildContext()))
c, err := dtrack.NewClient(*dtAddress, dtrack.WithAPIKey(*dtAPIKey))
if err != nil {
level.Error(logger).Log("msg", "Error creating client", "err", err)
os.Exit(1)
}
e := exporter.Exporter{
Client: c,
Logger: logger,
}
http.HandleFunc(*metricsPath, e.HandlerFunc())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Dependency-Track Exporter</title></head>
<body>
<h1>Dependency-Track Exporter</h1>
<p><a href='` + *metricsPath + `'>Metrics</a></p>
</body>
</html>`))
})
srvc := make(chan struct{})
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
go func() {
srv := &http.Server{}
if err := web.ListenAndServe(srv, webConfig, logger); err != http.ErrServerClosed {
level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
close(srvc)
}
}()
for {
select {
case <-term:
level.Info(logger).Log("msg", "Received SIGTERM, exiting gracefully...")
os.Exit(0)
case <-srvc:
os.Exit(1)
}
}
}