-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (142 loc) · 5.35 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"context"
"flag"
"fmt"
"net/http"
"net/http/pprof"
"os"
"strconv"
"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()
}
}()
}
flags := &collector.ExporterFlags{
FlagSlowQuery: *flagSlowQueryPtr,
FlagSlowQueryThreshold: *flagSlowQueryThresholdPtr,
FlagReplicationStatus: *flagReplicationStatusPtr,
FlagDataDiskUsage: *flagDataDiskUsagePtr,
FlagActiveTransactionPtr: *flagActiveTransactionPtr,
FlagSlowQueryExceptionHosts: slowQueryExceptionHosts,
FlagSlowQueryExceptionInfoPatterns: slowQueryExceptionInfoPatterns,
}
mux := http.NewServeMux()
mux.Handle(
"/metrics",
promhttp.InstrumentMetricHandler(
prometheus.DefaultRegisterer,
newHandler(
Version,
dsn,
flags,
),
),
)
// 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,
flags *collector.ExporterFlags,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
registry := prometheus.NewRegistry()
ctx := r.Context()
timeoutSeconds, err := getScrapeTimeoutSeconds(r)
if err != nil {
log.ErrorLogger.Infof("Error getting timeout from Prometheus header: err=%v", err)
} else if timeoutSeconds > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(timeoutSeconds*float64(time.Second)))
defer cancel()
r = r.WithContext(ctx)
}
registry.MustRegister(
collector.New(
ctx,
version,
dsn,
flags,
),
)
gatherers := prometheus.Gatherers{
prometheus.DefaultGatherer,
registry,
}
h := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}
}
// {"level":"info","msg":"Headers: map[Accept:[text/plain;version=0.0.4;q=1,*/*;q=0.1] Accept-Encoding:[gzip] User-Agent:[vm_promscrape] X-Prometheus-Scrape-Timeout-Seconds:[5.000]]","time":"2024-10-31T10:42:53+09:00"}
func getScrapeTimeoutSeconds(r *http.Request) (float64, error) {
var timeoutSeconds float64
if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" {
var err error
timeoutSeconds, err = strconv.ParseFloat(v, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse timeout from Prometheus header: key=X-Prometheus-Scrape-Timeout-Seconds v=%s err=%v", v, err)
}
}
if timeoutSeconds < 0 {
return 0, fmt.Errorf("timeout value from Prometheus header is invalid: %f", timeoutSeconds)
}
return timeoutSeconds, nil
}