-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.go
89 lines (66 loc) · 2.64 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
package main
import (
"greenplum-exporter/collector"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
logger "github.com/prometheus/common/log"
"gopkg.in/alecthomas/kingpin.v2"
)
/**
* 参考教程:https://www.cnblogs.com/momoyan/p/9943268.html
* 官方文档:https://godoc.org/github.com/prometheus/client_golang/prometheus
* 官方文档:https://gp-docs-cn.github.io/docs/admin_guide/monitoring/monitoring.html
*/
var (
listenAddress = kingpin.Flag("web.listen-address", "web endpoint").Default("0.0.0.0:9297").String()
metricPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
disableDefaultMetrics = kingpin.Flag("disableDefaultMetrics", "do not report default metrics(go metrics and process metrics)").Default("true").Bool()
)
var scrapers = map[collector.Scraper]bool{
collector.NewClusterStateScraper(): true,
collector.NewSegmentScraper(): true,
collector.NewDatabaseSizeScraper(): true,
collector.NewLocksScraper(): true,
collector.NewConnectionsScraper(): true,
collector.NewMaxConnScraper(): true,
collector.NewConnDetailScraper(): true,
collector.NewUsersScraper(): false,
collector.NewBgWriterStateScraper(): false,
collector.NewSystemScraper(): false,
collector.NewQueryScraper(): false,
collector.NewDynamicMemoryScraper(): false,
collector.NewDiskScraper(): false,
}
var gathers prometheus.Gatherers
func main() {
kingpin.Version("1.1.1")
kingpin.HelpFlag.Short('h')
logger.AddFlags(kingpin.CommandLine)
kingpin.Parse()
metricsHandleFunc := newHandler(*disableDefaultMetrics, scrapers)
mux := http.NewServeMux()
mux.HandleFunc(*metricPath, metricsHandleFunc)
logger.Warnf("Greenplum exporter is starting and will listening on : %s", *listenAddress)
logger.Error(http.ListenAndServe(*listenAddress, mux).Error())
}
func newHandler(disableDefaultMetrics bool, scrapers map[collector.Scraper]bool) http.HandlerFunc {
registry := prometheus.NewRegistry()
enabledScrapers := make([]collector.Scraper, 0, 16)
for scraper, enable := range scrapers {
if enable {
enabledScrapers = append(enabledScrapers, scraper)
}
}
greenPlumCollector := collector.NewCollector(enabledScrapers)
registry.MustRegister(greenPlumCollector)
if disableDefaultMetrics {
gathers = prometheus.Gatherers{registry}
} else {
gathers = prometheus.Gatherers{registry, prometheus.DefaultGatherer}
}
handler := promhttp.HandlerFor(gathers, promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
})
return handler.ServeHTTP
}