-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
78 lines (60 loc) · 1.5 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
package main
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
"time"
log "github.com/sirupsen/logrus"
"github.com/camptocamp/prometheus-puppetdb-sd/internal/config"
"github.com/camptocamp/prometheus-puppetdb-sd/internal/outputs"
"github.com/camptocamp/prometheus-puppetdb-sd/internal/puppetdb"
)
var version = "undefined"
func main() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
wg := sync.WaitGroup{}
defer wg.Wait()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cfg := config.LoadConfig(version)
o, err := outputs.Setup(&cfg.Output)
if err != nil {
log.Fatalf("Failed to setup output: %s", err)
return
}
puppetDBClient, err := puppetdb.NewClient(&cfg.PuppetDB)
if err != nil {
log.Fatalf("Failed to build a PuppetDB client: %s", err)
return
}
wg.Add(1)
go func() {
defer wg.Done()
for ctx.Err() == nil {
scrapeConfigs, err := puppetDBClient.GetScrapeConfigs(ctx, &cfg.PrometheusSD)
if err != nil {
log.Errorf("Failed to generate scrape_configs: %s", err)
} else {
err = o.Write(ctx, scrapeConfigs)
if err != nil {
log.Errorf("Failed to write output: %s", err)
}
}
log.Infof("Sleeping for %v", cfg.Sleep)
select {
case <-time.After(cfg.Sleep):
case <-ctx.Done():
}
}
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
err := o.Close(ctx)
if err != nil {
log.Errorf("Failed to close output: %s", err)
}
}()
<-sigChan
}