forked from chaspy/circleci-insights-prometheus-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (65 loc) · 1.62 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"time"
"github.com/chaspy/circleci-insights-prometheus-exporter/pkg/api/v2/insights/summary/jobs"
"github.com/chaspy/circleci-insights-prometheus-exporter/pkg/api/v2/insights/summary/workflows"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
interval, err := getInterval()
if err != nil {
log.Fatal(err)
}
workflows.Register()
jobs.Register()
http.Handle("/metrics", promhttp.Handler())
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
ticker := time.NewTicker(time.Duration(interval) * time.Second)
defer ticker.Stop()
go func() {
for {
err := snapshot()
if err != nil {
log.Fatal(err)
}
select {
case <-ticker.C:
continue
case <-interrupt:
ticker.Stop()
return
}
}
}()
log.Fatal(http.ListenAndServe(":8080", nil))
}
func snapshot() error {
workflowsWithRepo, err := workflows.Export()
if err != nil {
return fmt.Errorf("failed to export workflow insights metrics: %w", err)
}
err = jobs.Export(workflowsWithRepo)
if err != nil {
return fmt.Errorf("failed to export job insights metrics: %w", err)
}
return nil
}
func getInterval() (int, error) {
const defaultCircleCIAPIIntervalSecond = 300
circleciAPIInterval := os.Getenv("CIRCLECI_API_INTERVAL")
if len(circleciAPIInterval) == 0 {
return defaultCircleCIAPIIntervalSecond, nil
}
integerCircleCIAPIInterval, err := strconv.Atoi(circleciAPIInterval)
if err != nil {
return 0, fmt.Errorf("failed to read CircleCI Config: %w", err)
}
return integerCircleCIAPIInterval, nil
}