-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (122 loc) · 3.38 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
package main
import (
"fmt"
"net/http"
"os"
"path"
"runtime"
"strings"
"time"
"github.com/jessevdk/go-flags"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/webdevops/apprelease-exporter/config"
)
const (
Author = "webdevops.io"
)
var (
argparser *flags.Parser
opts config.Opts
collectorList map[string]*CollectorGeneral
AppConfig Config
// Git version information
gitCommit = "<unknown>"
gitTag = "<unknown>"
)
func main() {
initArgparser()
log.Infof("starting apprelease-exporter v%s (%s; %s; by %v)", gitTag, gitCommit, runtime.Version(), Author)
log.Info(string(opts.GetJson()))
log.Infof("loading config")
readConfig()
log.Infof("starting metrics collection")
if opts.Cache.Path != "" {
log.Infof("enable cache (path: %v, ttl: %v)", opts.Cache.Path, opts.Cache.Ttl.String())
}
if opts.Cve.Url != "" {
log.Infof("enable CVE fetching")
}
initMetricCollector()
log.Infof("starting http server on %s", opts.ServerBind)
startHttpServer()
}
// init argparser and parse/validate arguments
func initArgparser() {
argparser = flags.NewParser(&opts, flags.Default)
_, err := argparser.Parse()
// check if there is an parse error
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
fmt.Println(err)
fmt.Println()
argparser.WriteHelp(os.Stdout)
os.Exit(1)
}
}
// verbose level
if opts.Logger.Verbose {
log.SetLevel(log.DebugLevel)
}
// debug level
if opts.Logger.Debug {
log.SetReportCaller(true)
log.SetLevel(log.TraceLevel)
log.SetFormatter(&log.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
s := strings.Split(f.Function, ".")
funcName := s[len(s)-1]
return funcName, fmt.Sprintf("%s:%d", path.Base(f.File), f.Line)
},
})
}
// json log format
if opts.Logger.LogJson {
log.SetReportCaller(true)
log.SetFormatter(&log.JSONFormatter{
DisableTimestamp: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
s := strings.Split(f.Function, ".")
funcName := s[len(s)-1]
return funcName, fmt.Sprintf("%s:%d", path.Base(f.File), f.Line)
},
})
}
if opts.Scrape.TimeDocker == nil {
opts.Scrape.TimeDocker = &opts.Scrape.Time
}
if opts.Scrape.TimeGithub == nil {
opts.Scrape.TimeGithub = &opts.Scrape.Time
}
}
func readConfig() {
AppConfig = NewAppConfig(opts.Config.Path)
}
func initMetricCollector() {
var collectorName string
collectorList = map[string]*CollectorGeneral{}
collectorName = "docker"
if opts.Scrape.TimeDocker.Seconds() > 0 {
collectorList[collectorName] = NewCollectorGeneral(collectorName, &MetricsCollectorDocker{})
collectorList[collectorName].Setup(*opts.Scrape.TimeDocker)
}
collectorName = "github"
if opts.Scrape.TimeGithub.Seconds() > 0 {
collectorList[collectorName] = NewCollectorGeneral(collectorName, &MetricsCollectorGithub{})
collectorList[collectorName].Setup(*opts.Scrape.TimeGithub)
}
for _, collector := range collectorList {
collector.Run()
}
collector := NewCollectorGeneral("Collector", &MetricsCollectorCollector{})
collector.Setup(time.Duration(10 * time.Second))
collector.SetIsHidden(true)
collector.Run()
}
// start and handle prometheus handler
func startHttpServer() {
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(opts.ServerBind, nil))
}