Skip to content

Commit

Permalink
[receiver/postgresql] Add more Table Metrics (#13229)
Browse files Browse the repository at this point in the history
Adds the following metrics:
    postgresql.database.count
    postgresql.table.count
    postgresql.table.size
    postgresql.table.vacuum.count
  • Loading branch information
schmikei committed Aug 12, 2022
1 parent 2341bf7 commit 94d9600
Show file tree
Hide file tree
Showing 11 changed files with 1,851 additions and 958 deletions.
42 changes: 24 additions & 18 deletions receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,16 @@ func (c *postgreSQLClient) getDatabaseSize(ctx context.Context, databases []stri

// tableStats contains a result for a row of the getDatabaseTableMetrics result
type tableStats struct {
database string
table string
live int64
dead int64
inserts int64
upd int64
del int64
hotUpd int64
database string
table string
live int64
dead int64
inserts int64
upd int64
del int64
hotUpd int64
size int64
vacuumCount int64
}

func (c *postgreSQLClient) getDatabaseTableMetrics(ctx context.Context, db string) (map[tableIdentifier]tableStats, error) {
Expand All @@ -226,7 +228,9 @@ func (c *postgreSQLClient) getDatabaseTableMetrics(ctx context.Context, db strin
n_tup_ins AS ins,
n_tup_upd AS upd,
n_tup_del AS del,
n_tup_hot_upd AS hot_upd
n_tup_hot_upd AS hot_upd,
pg_relation_size(relid) AS table_size,
vacuum_count
FROM pg_stat_user_tables;`

ts := map[tableIdentifier]tableStats{}
Expand All @@ -237,20 +241,22 @@ func (c *postgreSQLClient) getDatabaseTableMetrics(ctx context.Context, db strin
}
for rows.Next() {
var table string
var live, dead, ins, upd, del, hotUpd int64
err = rows.Scan(&table, &live, &dead, &ins, &upd, &del, &hotUpd)
var live, dead, ins, upd, del, hotUpd, tableSize, vacuumCount int64
err = rows.Scan(&table, &live, &dead, &ins, &upd, &del, &hotUpd, &tableSize, &vacuumCount)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
ts[tableKey(db, table)] = tableStats{
database: db,
table: table,
live: live,
inserts: ins,
upd: upd,
del: del,
hotUpd: hotUpd,
database: db,
table: table,
live: live,
inserts: ins,
upd: upd,
del: del,
hotUpd: hotUpd,
size: tableSize,
vacuumCount: vacuumCount,
}
}
return ts, errors
Expand Down
4 changes: 4 additions & 0 deletions receiver/postgresqlreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ These are the metrics available for this scraper.
| **postgresql.backends** | The number of backends. | 1 | Sum(Int) | <ul> <li>database</li> </ul> |
| **postgresql.blocks_read** | The number of blocks read. | 1 | Sum(Int) | <ul> <li>database</li> <li>table</li> <li>source</li> </ul> |
| **postgresql.commits** | The number of commits. | 1 | Sum(Int) | <ul> <li>database</li> </ul> |
| **postgresql.database.count** | Number of user databases. | {databases} | Sum(Int) | <ul> </ul> |
| **postgresql.db_size** | The database disk usage. | By | Sum(Int) | <ul> <li>database</li> </ul> |
| **postgresql.index.scans** | The number of index scans on a table. | {scans} | Sum(Int) | <ul> </ul> |
| **postgresql.index.size** | The size of the index on disk. | By | Gauge(Int) | <ul> </ul> |
| **postgresql.operations** | The number of db row operations. | 1 | Sum(Int) | <ul> <li>database</li> <li>table</li> <li>operation</li> </ul> |
| **postgresql.rollbacks** | The number of rollbacks. | 1 | Sum(Int) | <ul> <li>database</li> </ul> |
| **postgresql.rows** | The number of rows in the database. | 1 | Sum(Int) | <ul> <li>database</li> <li>table</li> <li>state</li> </ul> |
| **postgresql.table.count** | Number of user tables in a database. | | Sum(Int) | <ul> </ul> |
| **postgresql.table.size** | Disk space used by a table. | By | Sum(Int) | <ul> </ul> |
| **postgresql.table.vacuum.count** | Number of times a table has manually been vacuumed. | {vacuums} | Sum(Int) | <ul> </ul> |

**Highlighted metrics** are emitted by default. Other metrics are optional and not emitted by default.
Any metric can be enabled or disabled with the following scraper configuration:
Expand Down
Loading

0 comments on commit 94d9600

Please sign in to comment.