Skip to content

Commit

Permalink
Metric/postgresql connections (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhogayatakb authored May 25, 2024
1 parent a45900d commit 7a7e793
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 5 deletions.
9 changes: 9 additions & 0 deletions receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type client interface {
getLatestWalAgeSeconds(ctx context.Context) (int64, error)
getMaxConnections(ctx context.Context) (int64, error)
getIndexStats(ctx context.Context, database string) (map[indexIdentifer]indexStat, error)
getActiveConnections(ctx context.Context) (int64, error)
listDatabases(ctx context.Context) ([]string, error)
}

Expand Down Expand Up @@ -442,6 +443,14 @@ func (c *postgreSQLClient) getMaxConnections(ctx context.Context) (int64, error)
return maxConns, err
}

func (c *postgreSQLClient) getActiveConnections(ctx context.Context) (int64, error) {
query := `SELECT COUNT(*) FROM pg_stat_activity WHERE state = 'active';`
row := c.client.QueryRowContext(ctx, query)
var activeConns int64
err := row.Scan(&activeConns)
return activeConns, err
}

type replicationStats struct {
clientAddr string
pendingBytes int64
Expand Down
10 changes: 9 additions & 1 deletion receiver/postgresqlreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ The number of commits.
| ---- | ----------- | ---------- | ----------------------- | --------- |
| 1 | Sum | Int | Cumulative | true |
### postgresql.connection.count
The number of active connections to this database. If DBM is enabled, this metric is tagged with state, app, db and user
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {connection} | Gauge | Int |
### postgresql.connection.max
Configured maximum number of client connections allowed
Expand Down Expand Up @@ -229,7 +237,7 @@ This metric requires WAL to be enabled with at least one replica.
Time between flushing recent WAL locally and receiving notification that the standby server has completed an operation with it.
This metric requires WAL to be enabled with at least one replica.
This metric requires WAL to be enabled with at least one replica.
| Unit | Metric Type | Value Type |
Expand Down

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

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

57 changes: 57 additions & 0 deletions receiver/postgresqlreceiver/internal/metadata/generated_metrics.go

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

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

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ all_set:
enabled: true
postgresql.commits:
enabled: true
postgresql.connection.count:
enabled: true
postgresql.connection.max:
enabled: true
postgresql.database.count:
Expand Down Expand Up @@ -76,6 +78,8 @@ none_set:
enabled: false
postgresql.commits:
enabled: false
postgresql.connection.count:
enabled: false
postgresql.connection.max:
enabled: false
postgresql.database.count:
Expand Down
11 changes: 9 additions & 2 deletions receiver/postgresqlreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ status:
resource_attributes:
postgresql.database.name:
description: The name of the database.
enabled: true
enabled: true
type: string
postgresql.table.name:
description: The schema name followed by the table name.
Expand Down Expand Up @@ -280,4 +280,11 @@ metrics:
gauge:
value_type: int
extended_documentation: |
This metric requires WAL to be enabled with at least one replica.
This metric requires WAL to be enabled with at least one replica.
postgresql.connection.count:
enabled: true
description: The number of active connections to this database. If DBM is enabled,
this metric is tagged with state, app, db and user
unit: '{connection}'
gauge:
value_type: int
19 changes: 17 additions & 2 deletions receiver/postgresqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ func (p *postgreSQLScraper) scrape(ctx context.Context) (pmetric.Metrics, error)
p.collectWalAge(ctx, now, listClient, &errs)
p.collectReplicationStats(ctx, now, listClient, &errs)
p.collectMaxConnections(ctx, now, listClient, &errs)

p.collectActiveConnections(ctx, now, listClient, &errs)

rb := p.mb.NewResourceBuilder()
rb.SetPostgresqlDatabaseName("N/A")
p.mb.EmitForResource(metadata.WithResource(rb.Emit()))

return p.mb.Emit(), errs.combine()
}

Expand Down Expand Up @@ -284,6 +285,20 @@ func (p *postgreSQLScraper) collectMaxConnections(
p.mb.RecordPostgresqlConnectionMaxDataPoint(now, mc)
}

func (p *postgreSQLScraper) collectActiveConnections(
ctx context.Context,
now pcommon.Timestamp,
client client,
errs *errsMux,
) {
ac, err := client.getActiveConnections(ctx)
if err != nil {
errs.addPartial(err)
return
}
p.mb.RecordPostgresqlConnectionCountDataPoint(now, ac)
}

func (p *postgreSQLScraper) collectReplicationStats(
ctx context.Context,
now pcommon.Timestamp,
Expand Down

0 comments on commit 7a7e793

Please sign in to comment.