-
Notifications
You must be signed in to change notification settings - Fork 7
/
exporter.go
205 lines (176 loc) · 5.12 KB
/
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package collector
import (
"context"
"fmt"
"strconv"
"strings"
"time"
kitlog "github.com/go-kit/log"
"github.com/go-kit/log/level"
pgx "github.com/jackc/pgx/v4"
"github.com/prometheus/client_golang/prometheus"
)
var (
upDesc = prometheus.NewDesc(
"postgres_up",
"Whether the Postgres server is up.",
nil,
nil,
)
infoDesc = prometheus.NewDesc(
"postgres_info",
"Postgres version",
[]string{"version"},
nil,
)
scrapeDurationDesc = prometheus.NewDesc(
"postgres_exporter_scraper_duration_seconds",
"Duration of a scrapers scrape.",
[]string{"scraper", "datname"},
nil,
)
scrapeSuccessDesc = prometheus.NewDesc(
"postgres_exporter_scraper_success",
"Whether a scraper succeeded.",
[]string{"scraper", "datname"},
nil,
)
)
const infoQuery = `SHOW server_version /*postgres_exporter*/`
const listDatnameQuery = `
SELECT datname FROM pg_database WHERE datallowconn = true AND datistemplate = false /*postgres_exporter*/`
// Scraper is the interface each scraper has to implement.
type Scraper interface {
Name() string
// Scrape new metrics and expose them via prometheus registry.
Scrape(ctx context.Context, db *pgx.Conn, version Version, ch chan<- prometheus.Metric) error
}
type Exporter struct {
ctx context.Context
logger kitlog.Logger
connConfig *pgx.ConnConfig
scrapers []Scraper
datnameScrapers []Scraper
}
// Postgres Version
type Version struct {
version float64
}
func NewVersion(v string) Version {
values := strings.Split(v, " ")
version, _ := strconv.ParseFloat(values[0], 64)
return Version{
version: version,
}
}
func (v Version) Gte(n float64) bool {
return v.version >= n
}
func (v Version) String() string {
return fmt.Sprintf("%g", v.version)
}
// Verify our Exporter satisfies the prometheus.Collector interface
var _ prometheus.Collector = (*Exporter)(nil)
// NewExporter is called every time we receive a scrape request and knows how
// to collect metrics using each of the scrapers. It will live only for the
// duration of the scrape request.
func NewExporter(ctx context.Context, logger kitlog.Logger, connConfig *pgx.ConnConfig) *Exporter {
return &Exporter{
ctx: ctx,
logger: logger,
connConfig: connConfig,
scrapers: []Scraper{
NewInfoScraper(),
NewLocksScraper(),
NewStatActivityScraper(),
NewStatArchiverScraper(),
NewStatBgwriterScraper(),
NewStatDatabaseScraper(),
NewStatReplicationScraper(),
},
datnameScrapers: []Scraper{
NewStatVacuumProgressScraper(),
NewStatUserTablesScraper(),
NewDiskUsageScraper(),
},
}
}
// Describe implements the prometheus.Collector interface.
func (e Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- scrapeDurationDesc
ch <- scrapeSuccessDesc
}
// Collect implements the prometheus.Collector interface.
func (e Exporter) Collect(ch chan<- prometheus.Metric) {
conn, err := pgx.ConnectConfig(e.ctx, e.connConfig)
if err != nil {
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0)
level.Error(e.logger).Log("error", err)
return // cannot continue without a valid connection
}
defer conn.Close(e.ctx)
// postgres_up
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 1)
var version string
if err := conn.QueryRow(e.ctx, infoQuery).Scan(&version); err != nil {
level.Error(e.logger).Log("error", err)
return // cannot continue without a version
}
v := NewVersion(version)
// postgres_info
ch <- prometheus.MustNewConstMetric(infoDesc, prometheus.GaugeValue, 1, v.String())
// discovery databases
var dbnames []string
rows, err := conn.Query(e.ctx, listDatnameQuery)
if err != nil {
level.Error(e.logger).Log("error", err)
return
}
defer rows.Close()
for rows.Next() {
var dbname string
err := rows.Scan(&dbname)
if err != nil {
level.Error(e.logger).Log("error", err)
return
}
dbnames = append(dbnames, dbname)
}
// run global scrapers
for _, scraper := range e.scrapers {
e.scrape(scraper, conn, v, ch)
}
// run datname scrapers
for _, dbname := range dbnames {
// update connection dbname
e.connConfig.Config.Database = dbname
// establish a new connection
conn, err := pgx.ConnectConfig(e.ctx, e.connConfig)
if err != nil {
level.Error(e.logger).Log("error", err)
return // cannot continue without a valid connection
}
// scrape
for _, scraper := range e.datnameScrapers {
e.scrape(scraper, conn, v, ch)
}
conn.Close(e.ctx)
}
}
func (e Exporter) scrape(scraper Scraper, conn *pgx.Conn, version Version, ch chan<- prometheus.Metric) {
start := time.Now()
err := scraper.Scrape(e.ctx, conn, version, ch)
duration := time.Since(start)
var success float64
logger := kitlog.With(e.logger, "scraper", scraper.Name(), "duration", duration.Seconds())
if err != nil {
logger.Log("error", err)
success = 0
} else {
level.Debug(logger).Log("event", "scraper.success")
success = 1
}
datname := e.connConfig.Config.Database
ch <- prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), scraper.Name(), datname)
ch <- prometheus.MustNewConstMetric(scrapeSuccessDesc, prometheus.GaugeValue, success, scraper.Name(), datname)
}