Skip to content

Commit

Permalink
[receiver/postgresql] Add bgwriter Metrics (#13328)
Browse files Browse the repository at this point in the history
Adds background writer metrics for the postgresqlreceiver. Feature is behind the receiver.postgresql.emitMetricsWithResourceAttributes feature gate.
- postgresql.bgwriter.buffers.allocated
- postgresql.bgwriter.buffers.writes
- postgresql.bgwriter.checkpoint.count
- postgresql.bgwriter.duration
- postgresql.bgwriter.maxwritten.count
  • Loading branch information
schmikei committed Aug 15, 2022
1 parent 7b7626b commit 42f8d4d
Show file tree
Hide file tree
Showing 10 changed files with 1,167 additions and 47 deletions.
66 changes: 66 additions & 0 deletions receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type indexIdentifer string
type client interface {
Close() error
getDatabaseStats(ctx context.Context, databases []string) (map[databaseName]databaseStats, error)
getBGWriterStats(ctx context.Context) (*bgStat, error)
getBackends(ctx context.Context, databases []string) (map[databaseName]int64, error)
getDatabaseSize(ctx context.Context, databases []string) (map[databaseName]int64, error)
getDatabaseTableMetrics(ctx context.Context, db string) (map[tableIdentifier]tableStats, error)
Expand Down Expand Up @@ -361,6 +362,71 @@ func (c *postgreSQLClient) getIndexStats(ctx context.Context, database string) (
return stats, multierr.Combine(errs...)
}

type bgStat struct {
checkpointsReq int64
checkpointsScheduled int64
checkpointWriteTime int64
checkpointSyncTime int64
bgWrites int64
backendWrites int64
bufferBackendWrites int64
bufferFsyncWrites int64
bufferCheckpoints int64
buffersAllocated int64
maxWritten int64
}

func (c *postgreSQLClient) getBGWriterStats(ctx context.Context) (*bgStat, error) {
query := `SELECT
checkpoints_req AS checkpoint_req,
checkpoints_timed AS checkpoint_scheduled,
checkpoint_write_time AS checkpoint_duration_write,
checkpoint_sync_time AS checkpoint_duration_sync,
buffers_clean AS bg_writes,
buffers_backend AS backend_writes,
buffers_backend_fsync AS buffers_written_fsync,
buffers_checkpoint AS buffers_checkpoints,
buffers_alloc AS buffers_allocated,
maxwritten_clean AS maxwritten_count
FROM pg_stat_bgwriter;`

row := c.client.QueryRowContext(ctx, query)
var (
checkpointsReq, checkpointsScheduled int64
checkpointSyncTime, checkpointWriteTime int64
bgWrites, bufferCheckpoints, bufferAllocated int64
bufferBackendWrites, bufferFsyncWrites, maxWritten int64
)
err := row.Scan(
&checkpointsReq,
&checkpointsScheduled,
&checkpointWriteTime,
&checkpointSyncTime,
&bgWrites,
&bufferBackendWrites,
&bufferFsyncWrites,
&bufferCheckpoints,
&bufferAllocated,
&maxWritten,
)
if err != nil {
return nil, err
}
return &bgStat{
checkpointsReq: checkpointsReq,
checkpointsScheduled: checkpointsScheduled,
checkpointWriteTime: checkpointWriteTime,
checkpointSyncTime: checkpointSyncTime,
bgWrites: bgWrites,
backendWrites: bufferBackendWrites,
bufferBackendWrites: bufferBackendWrites,
bufferFsyncWrites: bufferFsyncWrites,
bufferCheckpoints: bufferCheckpoints,
buffersAllocated: bufferAllocated,
maxWritten: maxWritten,
}, nil
}

func (c *postgreSQLClient) listDatabases(ctx context.Context) ([]string, error) {
query := `SELECT datname FROM pg_database
WHERE datistemplate = false;`
Expand Down
8 changes: 8 additions & 0 deletions receiver/postgresqlreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ These are the metrics available for this scraper.
| Name | Description | Unit | Type | Attributes |
| ---- | ----------- | ---- | ---- | ---------- |
| **postgresql.backends** | The number of backends. | 1 | Sum(Int) | <ul> <li>database</li> </ul> |
| **postgresql.bgwriter.buffers.allocated** | Number of buffers allocated. | {buffers} | Sum(Int) | <ul> </ul> |
| **postgresql.bgwriter.buffers.writes** | Number of buffers written. | {buffers} | Sum(Int) | <ul> <li>bg_buffer_source</li> </ul> |
| **postgresql.bgwriter.checkpoint.count** | The number of checkpoints performed. | {checkpoints} | Sum(Int) | <ul> <li>bg_checkpoint_type</li> </ul> |
| **postgresql.bgwriter.duration** | Total time spent writing and syncing files to disk by checkpoints. | ms | Sum(Int) | <ul> <li>bg_duration_type</li> </ul> |
| **postgresql.bgwriter.maxwritten** | Number of times the background writer stopped a cleaning scan because it had written too many buffers. | | Sum(Int) | <ul> </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> |
Expand Down Expand Up @@ -43,6 +48,9 @@ metrics:
| Name | Description | Values |
| ---- | ----------- | ------ |
| bg_buffer_source (source) | The source of a buffer write. | backend, backend_fsync, checkpoints, bgwriter |
| bg_checkpoint_type (type) | The type of checkpoint state. | requested, scheduled |
| bg_duration_type (type) | The type of time spent during the checkpoint. | sync, write |
| database | The name of the database. | |
| operation | The database operation. | ins, upd, del, hot_upd |
| source | The block read source type. | heap_read, heap_hit, idx_read, idx_hit, toast_read, toast_hit, tidx_read, tidx_hit |
Expand Down
Loading

0 comments on commit 42f8d4d

Please sign in to comment.