-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
150 lines (128 loc) · 3.27 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
port int
scrapeTimeout time.Duration
sonarURL string
sonarUser string
sonarPassword string
labelSeparator string
)
var (
gitRevision = "HEAD"
buildDate = "unknown"
version = "unknown"
versionCmd bool
helpCmd bool
)
// nolint:gochecknoinits
func init() {
flag.IntVar(&port, "port", 8080, "Exporter port")
flag.DurationVar(&scrapeTimeout, "scrape-timeout", 1*time.Minute, "Metrics scraper timeout")
flag.StringVar(&sonarURL, "url", "", "Required. Sonarqube URL")
flag.StringVar(&sonarUser, "user", "", "Required. Sonarqube User")
flag.StringVar(&sonarPassword, "password", "", "Required. Sonarqube Password")
flag.StringVar(&labelSeparator, "label-separator", "#", "Label Separator. For instance, "+
"for Sonar with Label 'key#value', Prometheus attribute {project=\"my-project-name\"}")
flag.BoolVar(&versionCmd, "version", false, "Show version")
flag.BoolVar(&helpCmd, "help", false, "Show help")
flag.Parse()
if versionCmd {
fmt.Printf("Version: %s\n", version)
fmt.Printf("Git Revision: %s\n", gitRevision)
fmt.Printf("UTC Build Date: %s\n", buildDate)
os.Exit(0)
}
if helpCmd {
flag.Usage()
os.Exit(0)
}
if sonarURL == "" || sonarUser == "" || sonarPassword == "" {
flag.Usage()
log.Fatal("make sure all required flags are provided")
}
}
func main() {
// Setting up signal capturing
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
done := make(chan struct{}, 1)
go func() {
<-stop
close(done)
}()
m := http.NewServeMux()
m.Handle("/metrics", promhttp.Handler())
server := &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: m}
go func() {
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}()
go initMetrics(done)
// Waiting for SIGINT (pkill -2)
<-done
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Println(err)
}
}
func initMetrics(done <-chan struct{}) {
sonar := NewSonarClient(sonarURL, sonarUser, sonarPassword)
components, err := sonar.GetComponents()
if err != nil {
log.Fatal(err)
}
for _, cInfo := range components {
componentKey := cInfo.Key
component, err := sonar.GetComponent(componentKey)
if err != nil {
log.Fatal(err)
}
allMetrics, err := sonar.GetMetrics()
if err != nil {
log.Fatal(err)
}
exp := NewPrometheusExporter()
metrics, err := exp.Init(component, allMetrics)
if err != nil {
log.Fatal(err)
}
schedule(done, 0, scrapeTimeout, func() error {
measures, err := sonar.GetMeasures(componentKey, metrics)
if err != nil {
log.Fatal(err)
}
return exp.Run(measures)
})
}
}
// schedule executes action with defined timeout until receives timeout signal
func schedule(done <-chan struct{}, initialDelay, timeout time.Duration, callback func() error) {
var err error
attemptTimer := time.After(initialDelay)
for {
select {
case <-done:
return
case <-attemptTimer:
err = callback()
if err != nil {
log.Printf("Scheduler error: %v\n", err)
}
attemptTimer = time.After(timeout)
log.Println("Scheduler job run successfully")
}
}
}