Skip to content

Commit

Permalink
Added DBM metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
naman47vyas committed Jun 3, 2024
1 parent 4a068cd commit 73b6831
Show file tree
Hide file tree
Showing 11 changed files with 1,149 additions and 2 deletions.
162 changes: 162 additions & 0 deletions receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type client interface {
getMaxConnections(ctx context.Context) (int64, error)
getIndexStats(ctx context.Context, database string) (map[indexIdentifer]indexStat, error)
listDatabases(ctx context.Context) ([]string, error)
getRowStats(ctx context.Context) ([]RowStats, error)
getQueryStats(ctx context.Context) ([]queryStats, error)
getBufferHit(ctx context.Context) ([]BufferHit, error)
}

type postgreSQLClient struct {
Expand Down Expand Up @@ -486,6 +489,165 @@ func (c *postgreSQLClient) getReplicationStats(ctx context.Context) ([]replicati
return rs, errors
}

type RowStats struct {
relationName string
rowsReturned int64
rowsFetched int64
rowsInserted int64
rowsUpdated int64
rowsDeleted int64
rowsHotUpdated int64
liveRows int64
deadRows int64
}

func (c *postgreSQLClient) getRowStats(ctx context.Context) ([]RowStats, error) {
query := `SELECT
relname,
pg_stat_get_tuples_returned(relid) AS rows_returned,
pg_stat_get_tuples_fetched(relid) AS rows_fetched,
pg_stat_get_tuples_inserted(relid) AS rows_inserted,
pg_stat_get_tuples_updated(relid) AS rows_updated,
pg_stat_get_tuples_deleted(relid) AS rows_deleted,
pg_stat_get_tuples_hot_updated(relid) AS rows_hot_updated,
pg_stat_get_live_tuples(relid) AS live_rows,
pg_stat_get_dead_tuples(relid) AS dead_rows
FROM
pg_stat_all_tables;
`

rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_stat_all_tables:: %w", err)
}

defer rows.Close()

var rs []RowStats
var errors error

for rows.Next() {
var (
relname sql.NullString
rowsReturned sql.NullInt64
rowsFetched sql.NullInt64
rowsInserted sql.NullInt64
rowsUpdated sql.NullInt64
rowsDeleted sql.NullInt64
rowsHotUpdated sql.NullInt64
liveRows sql.NullInt64
deadRows sql.NullInt64
)

err := rows.Scan(
&relname,
&rowsReturned,
&rowsFetched,
&rowsInserted,
&rowsUpdated,
&rowsDeleted,
&rowsHotUpdated,
&liveRows,
&deadRows,
)

if err != nil {
errors = multierr.Append(errors, err)
}

rs = append(rs, RowStats{
relname.String,
rowsReturned.Int64,
rowsFetched.Int64,
rowsInserted.Int64,
rowsUpdated.Int64,
rowsDeleted.Int64,
rowsHotUpdated.Int64,
liveRows.Int64,
deadRows.Int64,
})
}
return rs, nil
}

type queryStats struct {
queryId string
queryText string
queryCount int64
queryExecTime int64
}

func (c *postgreSQLClient) getQueryStats(ctx context.Context) ([]queryStats, error) {
query := `SELECT
queryid,
query,
calls,
total_exec_time
FROM pg_stat_statements;
`

rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_stat_statements: %w", err)
}
defer rows.Close()
var qs []queryStats
var errors error
for rows.Next() {
var queryId, queryText string
var queryCount int64
var queryExecTime float64
err = rows.Scan(&queryId, &queryText, &queryCount, &queryExecTime)
if err != nil {
errors = multierr.Append(errors, err)
}
queryExectimeNS := int64(queryExecTime * 1000000)
qs = append(qs, queryStats{
queryId: queryId,
queryText: queryText,
queryCount: queryCount,
queryExecTime: queryExectimeNS,
})
}
return qs, errors
}

type BufferHit struct {
dbName string
hits int64
}

func (c *postgreSQLClient) getBufferHit(ctx context.Context) ([]BufferHit, error) {
query := `SELECT datname, blks_hit FROM pg_stat_database;`

rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_stat_database:: %w", err)
}

defer rows.Close()

var bh []BufferHit
var errors error

for rows.Next() {
var dbname sql.NullString
var hits sql.NullInt64

err = rows.Scan(&dbname, &hits)

if err != nil {
errors = multierr.Append(errors, err)
continue
}
bh = append(bh, BufferHit{
dbName: dbname.String,
hits: hits.Int64,
})
}
return bh, errors
}

func (c *postgreSQLClient) getLatestWalAgeSeconds(ctx context.Context) (int64, error) {
query := `SELECT
coalesce(last_archived_time, CURRENT_TIMESTAMP) AS last_archived_wal,
Expand Down
114 changes: 114 additions & 0 deletions receiver/postgresqlreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ The number of blocks read.
| ---- | ----------- | ------ |
| source | The block read source type. | Str: ``heap_read``, ``heap_hit``, ``idx_read``, ``idx_hit``, ``toast_read``, ``toast_hit``, ``tidx_read``, ``tidx_hit`` |
### postgresql.buffer_hit
The number of times disk blocks were found in the buffer cache, preventing the need to read from the database. This metric is tagged with db.
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {hit}/s | Gauge | Int |
#### Attributes
| Name | Description | Values |
| ---- | ----------- | ------ |
| dbname | name of the database | Any Str |
### postgresql.commits
The number of commits.
Expand Down Expand Up @@ -140,6 +154,20 @@ The size of the index on disk.
| ---- | ----------- | ---------- |
| By | Gauge | Int |
### postgresql.live_rows
Enabled with `relations`. The estimated number of live rows. This metric is tagged with db, schema, table.

| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {row} | Gauge | Int |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| relation_name | name of the relation | Any Str |

### postgresql.operations

The number of db row operations.
Expand All @@ -154,6 +182,36 @@ The number of db row operations.
| ---- | ----------- | ------ |
| operation | The database operation. | Str: ``ins``, ``upd``, ``del``, ``hot_upd`` |

### postgresql.query.count

Number of times the statement was executed

| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| 1 | Sum | Int | Cumulative | false |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| query_text | Text of a representative statement | Any Str |
| query_id | Hash code to identify identical normalized queries. | Any Str |

### postgresql.query.total_exec_time

The total wait time of the summarized timed events in nanaoseconds.

| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
| ---- | ----------- | ---------- | ----------------------- | --------- |
| ns | Sum | Int | Cumulative | false |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| query_text | Text of a representative statement | Any Str |
| query_id | Hash code to identify identical normalized queries. | Any Str |

### postgresql.replication.data_delay

The amount of data delayed in replication.
Expand Down Expand Up @@ -190,6 +248,62 @@ The number of rows in the database.
| ---- | ----------- | ------ |
| state | The tuple (row) state. | Str: ``dead``, ``live`` |

### postgresql.rows_deleted

Enabled with `relations`. The number of rows deleted by queries in this database. This metric is tagged with db.

| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {row}/s | Gauge | Int |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| relation_name | name of the relation | Any Str |

### postgresql.rows_fetched

The number of rows fetched by queries in this database. This metric is tagged with db.

| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {row}/s | Gauge | Int |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| relation_name | name of the relation | Any Str |

### postgresql.rows_inserted

Enabled with `relations`. The number of rows inserted by queries in this database. This metric is tagged with db.

| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {row}/s | Gauge | Int |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| relation_name | name of the relation | Any Str |

### postgresql.rows_updated

Enabled with `relations`. The number of rows updated by queries in this database. This metric is tagged with db.

| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {row}/s | Gauge | Int |

#### Attributes

| Name | Description | Values |
| ---- | ----------- | ------ |
| relation_name | name of the relation | Any Str |

### postgresql.table.count

Number of user tables in a database.
Expand Down
6 changes: 6 additions & 0 deletions receiver/postgresqlreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ require (
go.uber.org/zap v1.25.0
)

require (
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
Expand All @@ -39,6 +44,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/k0kubun/pp v3.0.1+incompatible
github.com/klauspost/compress v1.16.7 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/knadh/koanf/v2 v2.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions receiver/postgresqlreceiver/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 73b6831

Please sign in to comment.