-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (87 loc) · 2.82 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
package main
import (
"github.com/implustech/prometheus_clickhouse_exporter/cmd"
"github.com/prometheus/client_golang/prometheus"
"github.com/roistat/go-clickhouse"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"net/http"
"os"
"regexp"
"strings"
"sync"
)
type ClickhouseMetrics struct {
Name string
Counter prometheus.Counter
LastValue float64
}
func NameToUnderscore(src string) string {
re := regexp.MustCompile(`(\w)([A-Z])`)
return strings.ToLower(re.ReplaceAllString(src, "${1}_${2}"))
}
func run(cmd *cobra.Command, args []string) {
clickhouseConnection := viper.GetString("clickhouse.connection")
logger.Info("clickhouse.connection ", clickhouseConnection)
logger.Info("production ", viper.GetBool("production"))
logger.Info("listen ", viper.GetString("listen"))
clickhouseConnectionErrorsTotal := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: viper.GetString("prometheus.namespace"),
Subsystem: viper.GetString("prometheus.subsystem"),
Name: "clickhouse_connection_errors_total",
Help: "clickhouse connection error counter",
})
prometheus.MustRegister(clickhouseConnectionErrorsTotal)
prometheusHandler := prometheus.Handler()
clickhouseMetrics := make(map[string]*ClickhouseMetrics)
mutex := sync.Mutex{}
mux := http.NewServeMux()
mux.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
mutex.Lock()
defer mutex.Unlock()
conn := clickhouse.NewConn(clickhouseConnection, clickhouse.NewHttpTransport())
query := clickhouse.NewQuery("select * from system.events")
iter := query.Iter(conn)
if iter.Error() != nil {
clickhouseConnectionErrorsTotal.Inc()
}
var name string
var value float64
for iter.Scan(&name, &value) {
underscoreName := NameToUnderscore(name)
metrics, ok := clickhouseMetrics[underscoreName]
if !ok {
metrics = &ClickhouseMetrics{}
metrics.Counter = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: viper.GetString("prometheus.namespace"),
Subsystem: viper.GetString("prometheus.subsystem"),
Name: underscoreName,
Help: name,
})
prometheus.MustRegister(metrics.Counter)
clickhouseMetrics[underscoreName] = metrics
}
metrics.Counter.Add(value - metrics.LastValue)
metrics.LastValue = value
}
prometheusHandler.ServeHTTP(w, req)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Clickhouse Exporter</title></head>
<body>
<h1>Clickhouse Exporter</h1>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>`))
})
logger.Fatalln(http.ListenAndServe(viper.GetString("listen"), mux))
}
func main() {
initLogger()
cmd.RootCmd.Run = run
if err := cmd.RootCmd.Execute(); err != nil {
logger.Println(err)
os.Exit(-1)
}
}