-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
145 lines (123 loc) · 4.26 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
package main
import (
"flag"
"fmt"
"net/http"
"net/http/pprof"
"os"
"strings"
"time"
"singlestore_exporter/collector"
"singlestore_exporter/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var Version string
var versionFlag = flag.Bool("version", false, "print the version")
func main() {
flagListenAddress := flag.String("net.listen_address", "0.0.0.0:9105", "network address on which the exporter listens")
flagPprof := flag.Bool("debug.pprof", false, "enable pprof")
flagReplicationStatusPtr := flag.Bool("collect.replication_status", false, "collect replication status")
flagSlowQueryPtr := flag.Bool("collect.slow_query", false, "collect slow query")
flagSlowQueryThresholdPtr := flag.Int("collect.slow_query.threshold", 10, "slow query threshold in seconds")
flagSlowQueryLogPathPtr := flag.String("collect.slow_query.log_path", "", "slow query log path")
flagSlowQueryExceptionHostsPtr := flag.String("collect.slow_query.exception.hosts", "", "slow query exception patterns host")
flagSlowQueryExceptionInfoPatternsPtr := flag.String("collect.slow_query.exception.info.patterns", "", "slow query exception patterns info")
flagDataDiskUsagePtr := flag.Bool("collect.data_disk_usage", false, "collect data disk usage")
flagDataDiskUsageScrapeIntervalPtr := flag.Int("collect.data_disk_usage.scrape_interval", 30, "data disk usage scrape interval in seconds")
flagActiveTransactionPtr := flag.Bool("collect.active_transaction", false, "collect active transaction")
flagLogPathPtr := flag.String("log.log_path", "", "singlestore_exporter log path")
flagLogLevel := flag.String("log.level", "info", "log level (default: info)")
flag.Parse()
if *versionFlag {
fmt.Printf("singlestore_exporter version %s\n", Version)
return
}
slowQueryExceptionHosts := make([]string, 0)
if *flagSlowQueryExceptionHostsPtr != "" {
slowQueryExceptionHosts = strings.Split(*flagSlowQueryExceptionHostsPtr, ",")
}
slowQueryExceptionInfoPatterns := make([]string, 0)
if *flagSlowQueryExceptionInfoPatternsPtr != "" {
slowQueryExceptionInfoPatterns = strings.Split(*flagSlowQueryExceptionInfoPatternsPtr, ",")
}
if err := log.InitLoggers(*flagLogPathPtr, *flagLogLevel, *flagSlowQueryLogPathPtr); err != nil {
fmt.Println(err)
os.Exit(1)
}
// only aggregator node need DSN
dsn := os.Getenv("DATA_SOURCE_NAME")
// scrape data_disk_usage in separate goroutine, because query execution time is too long (> 1s)
if *flagDataDiskUsagePtr {
ticker := time.Tick(time.Duration(*flagDataDiskUsageScrapeIntervalPtr) * time.Second)
go func() {
for range ticker {
collector.ScrapeDataDiskUsages()
}
}()
}
mux := http.NewServeMux()
mux.Handle(
"/metrics",
promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer,
newHandler(
Version,
dsn,
*flagSlowQueryPtr,
*flagSlowQueryThresholdPtr,
*flagReplicationStatusPtr,
*flagDataDiskUsagePtr,
*flagActiveTransactionPtr,
slowQueryExceptionHosts,
slowQueryExceptionInfoPatterns,
),
),
)
// pprof
if *flagPprof {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
log.ErrorLogger.Infof("listening on %s", *flagListenAddress)
if err := http.ListenAndServe(*flagListenAddress, mux); err != nil {
panic(err)
}
}
func newHandler(
version string,
dsn string,
flagSlowQuery bool,
flagSlowQueryThreshold int,
flagReplicationStatus bool,
flagDataDiskUsage bool,
flagActiveTransaction bool,
slowQueryExceptionHosts []string,
slowQueryExceptionInfoPatterns []string,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
registry := prometheus.NewRegistry()
registry.MustRegister(
collector.New(
version,
dsn,
flagSlowQuery,
flagSlowQueryThreshold,
flagReplicationStatus,
flagDataDiskUsage,
flagActiveTransaction,
slowQueryExceptionHosts,
slowQueryExceptionInfoPatterns,
),
)
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
h := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
}