Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions receiver/postgresqlreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ to grant the user you are using `pg_monitor`. Take the example from `testdata/in
GRANT pg_monitor TO otelu;
```

The `postgresql.backends` metric reports the number of PostgreSQL backend processes associated with each database,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The README paragraph is more of an extended explanation for users, and while it's debatable there, I'd still lean toward removing the explicit pg_stat_activity reference from the README too, since the review guidelines are clear about this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The only reason it was added here was because of the presence of pg_stat_activity. It makes sense to remove it from here since its an internal detail.

as counted from `pg_stat_activity`. It applies no filter on connection state or backend type, so it counts active,
idle, and idle-in-transaction client connections as well as non-client backends such as autovacuum workers and
parallel query workers. Background processes that are not associated with a database (e.g. the background writer and
WAL writer, which have a `NULL` `datname`) are not attributed to any database. This differs from query sample events,
which include each backend's `postgresql.state`.

The following options are available:
- `max_rows_per_query`: (optional, default=1000) The max number of rows would return from the query
against `pg_stat_activity`.
Expand Down
5 changes: 4 additions & 1 deletion receiver/postgresqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,10 @@ func (c *postgreSQLClient) getDatabaseLocks(ctx context.Context) ([]databaseLock
return dl, multierr.Combine(errs...)
}

// getBackends returns a map of database names to the number of active connections
// getBackends returns the number of backend processes for each database, counted from pg_stat_activity
// across all connection states (active, idle, idle-in-transaction) and all backend types, including
// non-client backends such as autovacuum and parallel workers. Backends with no associated database
// (NULL datname, e.g. the background writer and WAL writer) are not attributed to any database.
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)
rows, err := c.client.QueryContext(ctx, query)
Expand Down
2 changes: 1 addition & 1 deletion receiver/postgresqlreceiver/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ metrics:

### postgresql.backends

The number of backends.
The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to follow the rule of not disclosing the underlying datasoruce in documentation. How about:

"The number of backend processes associated with each database. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers." - {in all relevant places in the MR}


| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic | Stability |
| ---- | ----------- | ---------- | ----------------------- | --------- | --------- |
Expand Down
3 changes: 1 addition & 2 deletions receiver/postgresqlreceiver/generated_package_test.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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heads up — the generated files (generated_package_test.go, generated_logs.go, generated_logs_test.go, generated_metrics.go, generated_resource_test.go) have import reordering where standard library packages ended up below third-party imports, which can cause CI lint failures.

What I do is run make generate from within receiver/postgresqlreceiver/ — that runs mdatagen and then applies gofumpt, goimports, and gci to fix import ordering automatically. If you only ran go generate ./... or make mdatagen, those formatting steps get skipped.

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.

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.

2 changes: 1 addition & 1 deletion receiver/postgresqlreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ events:
metrics:
postgresql.backends:
enabled: true
description: The number of backends.
description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
stability: development
unit: "1"
sum:
Expand Down
24 changes: 12 additions & 12 deletions receiver/postgresqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ func legacyMetricAttributes[T ~string](attributes []T) []T {

type dbRetrieval struct {
sync.RWMutex
activityMap map[databaseName]int64
dbSizeMap map[databaseName]int64
dbStats map[databaseName]databaseStats
dbConflictStats map[databaseName]databaseConflictStats
backendCountByDB map[databaseName]int64
dbSizeMap map[databaseName]int64
dbStats map[databaseName]databaseStats
dbConflictStats map[databaseName]databaseConflictStats
}

// scrape scrapes the metric stats, transforms them and attributes them into a metric slices.
Expand Down Expand Up @@ -222,10 +222,10 @@ func (p *postgreSQLScraper) scrape(ctx context.Context) (pmetric.Metrics, error)

var errs errsMux
r := &dbRetrieval{
activityMap: make(map[databaseName]int64),
dbSizeMap: make(map[databaseName]int64),
dbStats: make(map[databaseName]databaseStats),
dbConflictStats: make(map[databaseName]databaseConflictStats),
backendCountByDB: make(map[databaseName]int64),
dbSizeMap: make(map[databaseName]int64),
dbStats: make(map[databaseName]databaseStats),
dbConflictStats: make(map[databaseName]databaseConflictStats),
}
p.retrieveDBMetrics(ctx, listClient, databases, r, &errs)

Expand Down Expand Up @@ -552,8 +552,8 @@ func (p *postgreSQLScraper) retrieveDBMetrics(
func (p *postgreSQLScraper) recordDatabase(now pcommon.Timestamp, db string, r *dbRetrieval, numTables int64) {
dbName := databaseName(db)
p.mb.RecordPostgresqlTableCountDataPoint(now, numTables, db)
if activeConnections, ok := r.activityMap[dbName]; ok {
p.mb.RecordPostgresqlBackendsDataPoint(now, activeConnections, db)
if backendCount, ok := r.backendCountByDB[dbName]; ok {
p.mb.RecordPostgresqlBackendsDataPoint(now, backendCount, db)
}
if size, ok := r.dbSizeMap[dbName]; ok {
p.mb.RecordPostgresqlDbSizeDataPoint(now, size, db)
Expand Down Expand Up @@ -893,13 +893,13 @@ func (*postgreSQLScraper) retrieveBackends(
errs *errsMux,
) {
defer wg.Done()
activityByDB, err := client.getBackends(ctx, databases)
backendCountByDB, err := client.getBackends(ctx, databases)
if err != nil {
errs.addPartial(err)
return
}
r.Lock()
r.activityMap = activityByDB
r.backendCountByDB = backendCountByDB
r.Unlock()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ resourceMetrics:
stringValue: postgres
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resourceMetrics:
stringValue: postgres
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -483,7 +483,7 @@ resourceMetrics:
stringValue: otel
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -1028,7 +1028,7 @@ resourceMetrics:
stringValue: otel2
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ resourceMetrics:
stringValue: postgres
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ resourceMetrics:
stringValue: otel
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -871,7 +871,7 @@ resourceMetrics:
stringValue: otel2
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ resourceMetrics:
stringValue: otel
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ resourceMetrics:
stringValue: otel
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -203,7 +203,7 @@ resourceMetrics:
stringValue: telemetry
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ resourceMetrics:
stringValue: otel
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -203,7 +203,7 @@ resourceMetrics:
stringValue: telemetry
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ resourceMetrics:
stringValue: open
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -369,7 +369,7 @@ resourceMetrics:
stringValue: otel
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -526,7 +526,7 @@ resourceMetrics:
stringValue: telemetry
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ resourceMetrics:
stringValue: open
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -369,7 +369,7 @@ resourceMetrics:
stringValue: otel
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -526,7 +526,7 @@ resourceMetrics:
stringValue: telemetry
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ resourceMetrics:
stringValue: open
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -369,7 +369,7 @@ resourceMetrics:
stringValue: otel
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down Expand Up @@ -526,7 +526,7 @@ resourceMetrics:
stringValue: telemetry
scopeMetrics:
- metrics:
- description: The number of backends.
- description: The number of backend processes associated with each database, as reported by `pg_stat_activity`. Counts backends across all connection states (active, idle, idle-in-transaction) and all backend types, including non-client backends such as autovacuum and parallel workers.
name: postgresql.backends
sum:
aggregationTemporality: 2
Expand Down
Loading
Loading