Skip to content

Commit

Permalink
[receiver/postgresql] Add optional postgresql.database.locks metric (#…
Browse files Browse the repository at this point in the history
…26318)

**Description:** Added new metric -- postgresql.database_locks -- the
number of database locks .

**Link to tracking Issue:** #26317

**Testing:** <Describe what testing was performed and which tests were
added.>

**Documentation:** <Describe the documentation added.>

---------

Co-authored-by: Antoine Toulme <antoine@toulme.name>
  • Loading branch information
Samiur Arif and atoulme authored Sep 5, 2023
1 parent 7b1f2b7 commit 92c1d44
Show file tree
Hide file tree
Showing 17 changed files with 529 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .chloggen/added-datbase-locks-metric.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: postgresqlreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added `postgresql.database.locks` metric.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [26317]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
39 changes: 39 additions & 0 deletions receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var errNoLastArchive = errors.New("no last archive found, not able to calculate
type client interface {
Close() error
getDatabaseStats(ctx context.Context, databases []string) (map[databaseName]databaseStats, error)
getDatabaseLocks(ctx context.Context) ([]databaseLocks, 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)
Expand Down Expand Up @@ -163,6 +164,44 @@ func (c *postgreSQLClient) getDatabaseStats(ctx context.Context, databases []str
return dbStats, errs
}

type databaseLocks struct {
relation string
mode string
lockType string
locks int64
}

func (c *postgreSQLClient) getDatabaseLocks(ctx context.Context) ([]databaseLocks, error) {
query := `SELECT relname AS relation, mode, locktype,COUNT(pid)
AS locks FROM pg_locks
JOIN pg_class ON pg_locks.relation = pg_class.oid
GROUP BY relname, mode, locktype;`

rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_locks and pg_locks.relation: %w", err)
}
defer rows.Close()
var dl []databaseLocks
var errs []error
for rows.Next() {
var relation, mode, lockType string
var locks int64
err = rows.Scan(&relation, &mode, &lockType, &locks)
if err != nil {
errs = append(errs, err)
continue
}
dl = append(dl, databaseLocks{
relation: relation,
mode: mode,
lockType: lockType,
locks: locks,
})
}
return dl, multierr.Combine(errs...)
}

// getBackends returns a map of database names to the number of active connections
func (c *postgreSQLClient) getBackends(ctx context.Context, databases []string) (map[databaseName]int64, error) {
query := filterQueryByDatabases("SELECT datname, count(*) as count from pg_stat_activity", databases, true)
Expand Down
16 changes: 16 additions & 0 deletions receiver/postgresqlreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ metrics:
enabled: true
```
### postgresql.database.locks
The number of database locks.
| Unit | Metric Type | Value Type |
| ---- | ----------- | ---------- |
| {lock} | Gauge | Int |
#### Attributes
| Name | Description | Values |
| ---- | ----------- | ------ |
| relation | OID of the relation targeted by the lock, or null if the target is not a relation or part of a relation. | Any Str |
| mode | Name of the lock mode held or desired by the process. | Any Str |
| lock_type | Type of the lockable object. | Any Str |
### postgresql.deadlocks
The number of deadlocks.
Expand Down
1 change: 1 addition & 0 deletions receiver/postgresqlreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func integrationTest(name string, databases []string) func(*testing.T) {
rCfg.Metrics.PostgresqlDeadlocks.Enabled = true
rCfg.Metrics.PostgresqlTempFiles.Enabled = true
rCfg.Metrics.PostgresqlSequentialScans.Enabled = true
rCfg.Metrics.PostgresqlDatabaseLocks.Enabled = true
}),
scraperinttest.WithExpectedFile(expectedFile),
scraperinttest.WithCompareOptions(
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.

61 changes: 61 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 @@ -21,6 +21,8 @@ all_set:
enabled: true
postgresql.database.count:
enabled: true
postgresql.database.locks:
enabled: true
postgresql.db_size:
enabled: true
postgresql.deadlocks:
Expand Down Expand Up @@ -80,6 +82,8 @@ none_set:
enabled: false
postgresql.database.count:
enabled: false
postgresql.database.locks:
enabled: false
postgresql.db_size:
enabled: false
postgresql.deadlocks:
Expand Down
16 changes: 16 additions & 0 deletions receiver/postgresqlreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ attributes:
- sync
- write
name_override: type
lock_type:
description: Type of the lockable object.
type: string
mode:
description: Name of the lock mode held or desired by the process.
type: string
source:
description: The block read source type.
type: string
Expand All @@ -62,6 +68,9 @@ attributes:
description: The database operation.
type: string
enum: [ins, upd, del, hot_upd]
relation:
description: OID of the relation targeted by the lock, or null if the target is not a relation or part of a relation.
type: string
replication_client:
description: The IP address of the client connected to this backend. If this field is "unix", it indicates either that the client is connected via a Unix socket.
type: string
Expand Down Expand Up @@ -147,6 +156,13 @@ metrics:
monotonic: false
value_type: int
unit: "{databases}"
postgresql.database.locks:
enabled: false
description: The number of database locks.
unit: "{lock}"
gauge:
value_type: int
attributes: [relation, mode, lock_type]
postgresql.db_size:
enabled: true
description: The database disk usage.
Expand Down
18 changes: 18 additions & 0 deletions receiver/postgresqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ 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.collectDatabaseLocks(ctx, now, listClient, &errs)

return p.mb.Emit(), errs.combine()
}
Expand Down Expand Up @@ -266,6 +267,23 @@ func (p *postgreSQLScraper) collectBGWriterStats(
p.mb.RecordPostgresqlBgwriterMaxwrittenDataPoint(now, bgStats.maxWritten)
}

func (p *postgreSQLScraper) collectDatabaseLocks(
ctx context.Context,
now pcommon.Timestamp,
client client,
errs *errsMux,
) {
dbLocks, err := client.getDatabaseLocks(ctx)
if err != nil {
p.logger.Error("Errors encountered while fetching database locks", zap.Error(err))
errs.addPartial(err)
return
}
for _, dbLock := range dbLocks {
p.mb.RecordPostgresqlDatabaseLocksDataPoint(now, dbLock.locks, dbLock.relation, dbLock.mode, dbLock.lockType)
}
}

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

0 comments on commit 92c1d44

Please sign in to comment.