/
postgresql_exporter.go
169 lines (140 loc) · 4 KB
/
postgresql_exporter.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
168
169
package main
import (
"database/sql"
"flag"
"net/http"
"os"
"strings"
"sync"
"time"
_ "github.com/lib/pq"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/log"
"github.com/mc2soft/postgresql_exporter/metrics"
)
const (
namespace = "postgresql"
)
var db *sql.DB
var (
listenAddress = flag.String("web.listen-address", ":9104", "Address to listen on for web interface and telemetry.")
metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
databases = flag.String("db.names", "", "Comma-separated list of monitored DB.")
slow = flag.Duration("db.consider-query-slow", 100*time.Millisecond, "Queries with execution time higher than this value will be considered as slow (e.g. 100ms, 1s).")
tables = flag.String("db.tables", "", "Comma-separated list of tables to track.")
queries = flag.String("queries.config-path", "", "Path to yaml files with custom queries")
)
type Exporter struct {
m sync.Mutex
dsn string
metrics []metrics.Collection
totalScrapes prometheus.Counter
duration, errors prometheus.Gauge
}
func NewPostgreSQLExporter(dsn string, cq []metrics.CustomQuery) *Exporter {
e := &Exporter{
dsn: dsn,
metrics: []metrics.Collection{
metrics.NewBufferMetrics(),
metrics.NewDBMetrics(strings.Split(*databases, ",")),
metrics.NewSlowQueryMetrics(*slow),
metrics.NewCustomQueryMetrics(cq),
},
totalScrapes: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "exporter_scrapes_total",
Help: "Current total postgresql scrapes.",
}),
duration: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "exporter_last_scrape_duration_seconds",
Help: "The last scrape duration.",
}),
errors: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: "exporter_last_scrape_error",
Help: "The last scrape error status.",
}),
}
if len(*tables) > 0 {
e.metrics = append(e.metrics, metrics.NewTableMetrics(strings.Split(*tables, ",")))
}
return e
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
for _, m := range e.metrics {
m.Describe(ch)
}
ch <- e.duration.Desc()
ch <- e.totalScrapes.Desc()
ch <- e.errors.Desc()
}
type metric struct {
section string
key string
val string
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.m.Lock()
defer e.m.Unlock()
e.scrape()
ch <- e.duration
ch <- e.totalScrapes
ch <- e.errors
for _, m := range e.metrics {
m.Collect(ch)
}
}
func (e *Exporter) scrape() {
now := time.Now().UnixNano()
e.totalScrapes.Inc()
for _, m := range e.metrics {
err := m.Scrape(db)
if err != nil {
log.Println(err)
e.errors.Set(1)
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
return
}
}
e.errors.Set(0)
e.duration.Set(float64(time.Now().UnixNano()-now) / 1000000000)
}
// check interface
var _ prometheus.Collector = new(Exporter)
func main() {
flag.Parse()
dsn := os.Getenv("DATA_SOURCE_NAME")
if len(dsn) == 0 {
log.Fatal("couldn't find environment variable DATA_SOURCE_NAME")
}
if *databases == "" {
log.Fatal("please specify at least one database")
}
var err error
db, err = sql.Open("postgres", dsn)
if err != nil {
log.Fatal("error opening connection to database: ", err)
}
defer db.Close()
if err := db.Ping(); err != nil {
log.Fatal("error opening connection to database: ", err)
}
db.SetMaxIdleConns(5)
db.SetMaxOpenConns(5)
exporter := NewPostgreSQLExporter(dsn, parseQueries(*queries))
prometheus.MustRegister(exporter)
http.Handle(*metricPath, prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>PostgreSQL exporter</title></head>
<body>
<h1>PostgreSQL exporter</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
`))
})
log.Infof("Starting Server: %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}