forked from yunlzheng/prometheus-pusher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (74 loc) · 1.87 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
package main
import (
"flag"
"fmt"
"github.com/Andywugh/prometheus-pusher/scrape"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/retrieval"
"github.com/prometheus/prometheus/storage"
"strings"
)
var cfg = struct {
configFile string
customLabels string
customLabelValues string
}{}
var (
labels, values []string
)
func init() {
flag.StringVar(
&cfg.configFile, "config.file", "prometheus_pusher.yml",
"Prometheus configuration file name.",
)
flag.StringVar(
&cfg.customLabels, "config.customLabels", "", "custom metrics labels",
)
flag.StringVar(
&cfg.customLabelValues, "config.customLabelValues", "", "custom mertics label values",
)
}
func main() {
flag.Parse()
var (
sampleAppender = storage.Fanout{}
targetManager = retrieval.NewTargetManager(sampleAppender)
jobTargets = scrape.NewJobTargets(targetManager)
)
fmt.Println("Loading prometheus config file: " + cfg.configFile)
fmt.Println("Custom labels: " + cfg.customLabels + "\t Custom label values: " + cfg.customLabelValues)
if cfg.customLabels == "" {
labels = []string{}
values = []string{}
} else {
labels = strings.Split(cfg.customLabels, ",")
values = strings.Split(cfg.customLabelValues, ",")
}
var (
scrapeManager = scrape.NewExporterScrape(jobTargets, labels, values)
)
conf, err := config.LoadFile(cfg.configFile)
if err != nil {
fmt.Println(err.Error())
return
}
targetManager.ApplyConfig(conf)
go targetManager.Run()
defer targetManager.Stop()
scrapeManager.AppConfig(conf)
go scrapeManager.Run()
defer scrapeManager.Stop()
r := gin.Default()
pprof.Register(r, nil) // NOQA
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.GET("/targets", func(c *gin.Context) {
c.JSON(200, jobTargets.Targets())
})
r.Run()
}