mirror of
https://github.com/prometheus-community/postgres_exporter
synced 2025-04-01 22:48:15 +00:00
stat_user_tables: Add total size metric (#904)
Signed-off-by: David Cook <dcook@divviup.org>
This commit is contained in:
parent
0b6d9860ab
commit
31ef4ed5a2
@ -150,6 +150,12 @@ var (
|
|||||||
[]string{"datname", "schemaname", "relname"},
|
[]string{"datname", "schemaname", "relname"},
|
||||||
prometheus.Labels{},
|
prometheus.Labels{},
|
||||||
)
|
)
|
||||||
|
statUserTablesTotalSize = prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(namespace, userTableSubsystem, "size_bytes"),
|
||||||
|
"Total disk space used by this table, in bytes, including all indexes and TOAST data",
|
||||||
|
[]string{"datname", "schemaname", "relname"},
|
||||||
|
prometheus.Labels{},
|
||||||
|
)
|
||||||
|
|
||||||
statUserTablesQuery = `SELECT
|
statUserTablesQuery = `SELECT
|
||||||
current_database() datname,
|
current_database() datname,
|
||||||
@ -173,7 +179,8 @@ var (
|
|||||||
vacuum_count,
|
vacuum_count,
|
||||||
autovacuum_count,
|
autovacuum_count,
|
||||||
analyze_count,
|
analyze_count,
|
||||||
autoanalyze_count
|
autoanalyze_count,
|
||||||
|
pg_total_relation_size(relid) as total_size
|
||||||
FROM
|
FROM
|
||||||
pg_stat_user_tables`
|
pg_stat_user_tables`
|
||||||
)
|
)
|
||||||
@ -191,10 +198,10 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
|
|||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var datname, schemaname, relname sql.NullString
|
var datname, schemaname, relname sql.NullString
|
||||||
var seqScan, seqTupRead, idxScan, idxTupFetch, nTupIns, nTupUpd, nTupDel, nTupHotUpd, nLiveTup, nDeadTup,
|
var seqScan, seqTupRead, idxScan, idxTupFetch, nTupIns, nTupUpd, nTupDel, nTupHotUpd, nLiveTup, nDeadTup,
|
||||||
nModSinceAnalyze, vacuumCount, autovacuumCount, analyzeCount, autoanalyzeCount sql.NullInt64
|
nModSinceAnalyze, vacuumCount, autovacuumCount, analyzeCount, autoanalyzeCount, totalSize sql.NullInt64
|
||||||
var lastVacuum, lastAutovacuum, lastAnalyze, lastAutoanalyze sql.NullTime
|
var lastVacuum, lastAutovacuum, lastAnalyze, lastAutoanalyze sql.NullTime
|
||||||
|
|
||||||
if err := rows.Scan(&datname, &schemaname, &relname, &seqScan, &seqTupRead, &idxScan, &idxTupFetch, &nTupIns, &nTupUpd, &nTupDel, &nTupHotUpd, &nLiveTup, &nDeadTup, &nModSinceAnalyze, &lastVacuum, &lastAutovacuum, &lastAnalyze, &lastAutoanalyze, &vacuumCount, &autovacuumCount, &analyzeCount, &autoanalyzeCount); err != nil {
|
if err := rows.Scan(&datname, &schemaname, &relname, &seqScan, &seqTupRead, &idxScan, &idxTupFetch, &nTupIns, &nTupUpd, &nTupDel, &nTupHotUpd, &nLiveTup, &nDeadTup, &nModSinceAnalyze, &lastVacuum, &lastAutovacuum, &lastAnalyze, &lastAutoanalyze, &vacuumCount, &autovacuumCount, &analyzeCount, &autoanalyzeCount, &totalSize); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,6 +426,17 @@ func (c *PGStatUserTablesCollector) Update(ctx context.Context, instance *instan
|
|||||||
autoanalyzeCountMetric,
|
autoanalyzeCountMetric,
|
||||||
datnameLabel, schemanameLabel, relnameLabel,
|
datnameLabel, schemanameLabel, relnameLabel,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
totalSizeMetric := 0.0
|
||||||
|
if totalSize.Valid {
|
||||||
|
totalSizeMetric = float64(totalSize.Int64)
|
||||||
|
}
|
||||||
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
statUserTablesTotalSize,
|
||||||
|
prometheus.GaugeValue,
|
||||||
|
totalSizeMetric,
|
||||||
|
datnameLabel, schemanameLabel, relnameLabel,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := rows.Err(); err != nil {
|
if err := rows.Err(); err != nil {
|
||||||
|
@ -71,7 +71,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
|
|||||||
"vacuum_count",
|
"vacuum_count",
|
||||||
"autovacuum_count",
|
"autovacuum_count",
|
||||||
"analyze_count",
|
"analyze_count",
|
||||||
"autoanalyze_count"}
|
"autoanalyze_count",
|
||||||
|
"total_size"}
|
||||||
rows := sqlmock.NewRows(columns).
|
rows := sqlmock.NewRows(columns).
|
||||||
AddRow("postgres",
|
AddRow("postgres",
|
||||||
"public",
|
"public",
|
||||||
@ -94,7 +95,8 @@ func TestPGStatUserTablesCollector(t *testing.T) {
|
|||||||
11,
|
11,
|
||||||
12,
|
12,
|
||||||
13,
|
13,
|
||||||
14)
|
14,
|
||||||
|
15)
|
||||||
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
|
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
|
||||||
ch := make(chan prometheus.Metric)
|
ch := make(chan prometheus.Metric)
|
||||||
go func() {
|
go func() {
|
||||||
@ -170,7 +172,8 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
|
|||||||
"vacuum_count",
|
"vacuum_count",
|
||||||
"autovacuum_count",
|
"autovacuum_count",
|
||||||
"analyze_count",
|
"analyze_count",
|
||||||
"autoanalyze_count"}
|
"autoanalyze_count",
|
||||||
|
"total_size"}
|
||||||
rows := sqlmock.NewRows(columns).
|
rows := sqlmock.NewRows(columns).
|
||||||
AddRow("postgres",
|
AddRow("postgres",
|
||||||
nil,
|
nil,
|
||||||
@ -193,6 +196,7 @@ func TestPGStatUserTablesCollectorNullValues(t *testing.T) {
|
|||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
|
nil,
|
||||||
nil)
|
nil)
|
||||||
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
|
mock.ExpectQuery(sanitizeQuery(statUserTablesQuery)).WillReturnRows(rows)
|
||||||
ch := make(chan prometheus.Metric)
|
ch := make(chan prometheus.Metric)
|
||||||
|
Loading…
Reference in New Issue
Block a user