/
replication.go
88 lines (78 loc) · 3.43 KB
/
replication.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
package collector
import (
"context"
"github.com/prometheus/client_golang/prometheus"
"github.com/uptrace/bun"
"github.com/1and1/pg-exporter/collector/models"
)
const (
// subsystem
replication = "replication"
)
// ScrapeReplication scrapes from pg_stat_replication
type ScrapeReplication struct{}
// Name of the Scraper
func (ScrapeReplication) Name() string {
return "pg_stat_replication"
}
// Help describes the role of the Scraper
func (ScrapeReplication) Help() string {
return "Collect from pg_stat_replication"
}
// Version returns minimum PostgreSQL version
func (ScrapeReplication) Version() int {
return 90200
}
// Type returns the scrape type
func (ScrapeReplication) Type() ScrapeType {
return SCRAPEGLOBAL
}
// Scrape collects data from database connection and sends it over channel as prometheus metric.
func (ScrapeReplication) Scrape(ctx context.Context, db *bun.DB, ch chan<- prometheus.Metric) error {
var qs string
if pgversion < 100000 {
qs = `SELECT pid,
application_name,
client_addr,
backend_xmin,
pg_xlog_location_diff(
CASE WHEN pg_is_in_recovery() THEN pg_last_xlog_receive_location() ELSE pg_current_xlog_location() END,
sent_location) AS sent_lag_bytes,
pg_xlog_location_diff(
CASE WHEN pg_is_in_recovery() THEN pg_last_xlog_receive_location() ELSE pg_current_xlog_location() END,
write_location) AS write_lag_bytes,
pg_xlog_location_diff(
CASE WHEN pg_is_in_recovery() THEN pg_last_xlog_receive_location() ELSE pg_current_xlog_location() END,
flush_location) AS flush_lag_bytes,
pg_xlog_location_diff(
CASE WHEN pg_is_in_recovery() THEN pg_last_xlog_receive_location() ELSE pg_current_xlog_location() END,
replay_location) AS replay_lag_bytes
FROM pg_stat_replication;`
} else {
qs = `SELECT pid,
application_name,
client_addr,
backend_xmin,
pg_wal_lsn_diff(CASE WHEN pg_is_in_recovery() THEN pg_last_wal_receive_lsn() ELSE pg_current_wal_lsn() END,
sent_lsn) AS sent_lag_bytes,
pg_wal_lsn_diff(CASE WHEN pg_is_in_recovery() THEN pg_last_wal_receive_lsn() ELSE pg_current_wal_lsn() END,
flush_lsn) AS flush_lag_bytes,
pg_wal_lsn_diff(CASE WHEN pg_is_in_recovery() THEN pg_last_wal_receive_lsn() ELSE pg_current_wal_lsn() END,
write_lsn) AS write_lag_bytes,
pg_wal_lsn_diff(CASE WHEN pg_is_in_recovery() THEN pg_last_wal_receive_lsn() ELSE pg_current_wal_lsn() END,
replay_lsn) AS replay_lag_bytes,
extract(EPOCH FROM write_lag) AS write_lag,
extract(EPOCH FROM flush_lag) AS flush_lag,
extract(EPOCH FROM replay_lag) AS replay_lag
FROM pg_stat_replication;`
}
var statReplication models.PgStatReplicationSlice
rows, err := db.QueryContext(ctx, qs)
if err != nil {
return err
}
if err := db.ScanRows(ctx, rows, &statReplication); err != nil {
return err
}
return statReplication.ToMetrics(namespace, replication, ch)
}