Skip to content

Add pg_database collector #613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## master / unreleased

* [ENHANCEMENT] Add pg_database_size_bytes metric #613

## 0.10.1 / 2022-01-14

* [BUGFIX] Fix broken log-level for values other than debug. #560
Expand Down
14 changes: 14 additions & 0 deletions cmd/postgres_exporter/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
package main

import (
"context"
"database/sql"
"fmt"
"log"
"sync"
"time"

"github.com/blang/semver"
"github.com/go-kit/log/level"
"github.com/prometheus-community/postgres_exporter/collector"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -128,6 +131,17 @@ func (s *Server) Scrape(ch chan<- prometheus.Metric, disableSettingsMetrics bool
err = fmt.Errorf("queryNamespaceMappings returned %d errors", len(errMap))
}

{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in a block to not reset the err var that gets set above. I don't want to return early because I don't think the errors are fatal, just informational.

pgdb := collector.NewPGDatabaseCollector()
metrics, err := pgdb.Update(context.TODO(), s.db, s.String())
if err != nil {
log.Printf("Failed to scrape pg_database metrics: %s", err)
}
for _, m := range metrics {
ch <- m
}
}

return err
}

Expand Down
63 changes: 63 additions & 0 deletions collector/pg_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2021 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"context"
"database/sql"

"github.com/prometheus/client_golang/prometheus"
)

type PGDatabaseCollector struct{}

func NewPGDatabaseCollector() *PGDatabaseCollector {
return &PGDatabaseCollector{}
}

var pgDatabase = map[string]*prometheus.Desc{
"size_bytes": prometheus.NewDesc(
"pg_database_size_bytes",
"Disk space used by the database",
[]string{"datname"}, nil,
),
}

func (PGDatabaseCollector) Update(ctx context.Context, db *sql.DB, server string) ([]prometheus.Metric, error) {
metrics := []prometheus.Metric{}
rows, err := db.QueryContext(ctx,
`SELECT pg_database.datname
,pg_database_size(pg_database.datname)
FROM pg_database;`)
if err != nil {
return metrics, err
}
defer rows.Close()

for rows.Next() {
var datname string
var size int64
if err := rows.Scan(&datname, &size); err != nil {
return metrics, err
}
metrics = append(metrics, prometheus.MustNewConstMetric(
pgDatabase["size_bytes"],
prometheus.GaugeValue, float64(size), datname,
))
}
if err := rows.Err(); err != nil {
return metrics, err
}
return metrics, nil
}
12 changes: 0 additions & 12 deletions queries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,6 @@ pg_statio_user_tables:
usage: "COUNTER"
description: "Number of buffer hits in this table's TOAST table indexes (if any)"

pg_database:
query: "SELECT pg_database.datname, pg_database_size(pg_database.datname) as size_bytes FROM pg_database"
master: true
cache_seconds: 30
metrics:
- datname:
usage: "LABEL"
description: "Name of the database"
- size_bytes:
usage: "GAUGE"
description: "Disk space used by the database"

# WARNING: This set of metrics can be very expensive on a busy server as every unique query executed will create an additional time series
pg_stat_statements:
query: "SELECT t2.rolname, t3.datname, queryid, calls, total_time / 1000 as total_time_seconds, min_time / 1000 as min_time_seconds, max_time / 1000 as max_time_seconds, mean_time / 1000 as mean_time_seconds, stddev_time / 1000 as stddev_time_seconds, rows, shared_blks_hit, shared_blks_read, shared_blks_dirtied, shared_blks_written, local_blks_hit, local_blks_read, local_blks_dirtied, local_blks_written, temp_blks_read, temp_blks_written, blk_read_time / 1000 as blk_read_time_seconds, blk_write_time / 1000 as blk_write_time_seconds FROM pg_stat_statements t1 JOIN pg_roles t2 ON (t1.userid=t2.oid) JOIN pg_database t3 ON (t1.dbid=t3.oid) WHERE t2.rolname != 'rdsadmin'"
Expand Down