Skip to content

Commit

Permalink
Metric/postgresql connections (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhogayatakb committed Jun 12, 2024
1 parent 4dfc70f commit 0e2b2ac
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 4 deletions.
9 changes: 9 additions & 0 deletions receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 @@ -488,6 +489,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 @@ -82,6 +84,8 @@ none_set:
enabled: false
postgresql.commits:
enabled: false
postgresql.connection.count:
enabled: false
postgresql.connection.max:
enabled: false
postgresql.database.count:
Expand Down
14 changes: 11 additions & 3 deletions receiver/postgresqlreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ status:
resource_attributes:
postgresql.database.name:
description: The name of the database.
enabled: true
type: string
enabled: true type: string
postgresql.schema.name:
description: The schema name.
enabled: true
Expand Down Expand Up @@ -311,6 +310,15 @@ metrics:
value_type: double
extended_documentation: |
This metric requires WAL to be enabled with at least one replica.
tests:
config:

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
16 changes: 16 additions & 0 deletions receiver/postgresqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ func (p *postgreSQLScraper) scrape(ctx context.Context) (pmetric.Metrics, error)
p.collectDatabaseLocks(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()))
Expand Down Expand Up @@ -348,6 +350,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 0e2b2ac

Please sign in to comment.