Skip to content

Commit 2bfa3e8

Browse files
committed
dbo11y: stop tracking alloy's own queries in pg_stat_activity
This PR introduces a change for excluding "own queries" in postgres from `pg_stat_activity`: we need to drop samples (and wait events) for the currently connected user. This new behaviour is enabled by default through the setting `exclude_current_user`, as it's deemed safe / recommended. No changes to be done on `pg_stat_statements` as that is achieved through user permissions update (docs on the website).
1 parent 7a95f62 commit 2bfa3e8

File tree

5 files changed

+159
-37
lines changed

5 files changed

+159
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ Main (unreleased)
2626

2727
### Bugfixes
2828

29-
3029
- (_Public Preview_) Additions to `database_observability.postgres` component:
3130
- `schema_details`
3231
- fixes collection of schema details for mixed case table names (@fridgepoet)
32+
- do not track query samples for currently connected user, via option `exclude_current_user` (@cristiangreco)
3333

3434
- (_Public Preview_) Additions to `database_observability.mysql` component:
3535
- replace the internal `server_id` label attribution in favor of a hash composed from `@@server_uuid` and `@@hostname`

docs/sources/reference/components/database_observability/database_observability.postgres.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ The `aws` block supplies the [ARN](https://docs.aws.amazon.com/IAM/latest/UserGu
9494
|---------------------------|------------|---------------------------------------------------------------|---------|----------|
9595
| `collect_interval` | `duration` | How frequently to collect information from database. | `"15s"` | no |
9696
| `disable_query_redaction` | `bool` | Collect unredacted SQL query text (might include parameters). | `false` | no |
97+
| `exclude_current_user` | `bool` | Do not collect query samples for current database user. | `true` | no |
9798

9899
### `schema_details`
99100

internal/component/database_observability/postgres/collector/query_samples.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ const selectPgStatActivity = `
6666
s.query_id != 0
6767
)
6868
)
69+
%s
6970
`
7071

72+
const excludeCurrentUserClause = `AND s.usesysid != (select oid from pg_roles where rolname = current_user)`
73+
7174
type QuerySamplesInfo struct {
7275
DatabaseName sql.NullString
7376
DatabaseID int
@@ -100,13 +103,15 @@ type QuerySamplesArguments struct {
100103
EntryHandler loki.EntryHandler
101104
Logger log.Logger
102105
DisableQueryRedaction bool
106+
ExcludeCurrentUser bool
103107
}
104108

105109
type QuerySamples struct {
106110
dbConnection *sql.DB
107111
collectInterval time.Duration
108112
entryHandler loki.EntryHandler
109113
disableQueryRedaction bool
114+
excludeCurrentUser bool
110115

111116
logger log.Logger
112117
running *atomic.Bool
@@ -204,14 +209,15 @@ func (w WaitEventIdentity) Equal(other WaitEventIdentity) bool {
204209
}
205210

206211
func NewQuerySamples(args QuerySamplesArguments) (*QuerySamples, error) {
207-
const emittedCacheSize = 1000 //pg_stat_statements default max number of statements to track
212+
const emittedCacheSize = 1000 // pg_stat_statements default max number of statements to track
208213
const emittedCacheTTL = 10 * time.Minute
209214

210215
return &QuerySamples{
211216
dbConnection: args.DB,
212217
collectInterval: args.CollectInterval,
213218
entryHandler: args.EntryHandler,
214219
disableQueryRedaction: args.DisableQueryRedaction,
220+
excludeCurrentUser: args.ExcludeCurrentUser,
215221
logger: log.With(args.Logger, "collector", QuerySamplesCollector),
216222
running: &atomic.Bool{},
217223
samples: map[SampleKey]*SampleState{},
@@ -275,7 +281,11 @@ func (c *QuerySamples) fetchQuerySample(ctx context.Context) error {
275281
queryTextField = queryTextClause
276282
}
277283

278-
query := fmt.Sprintf(selectPgStatActivity, queryTextField)
284+
excludeCurrentUserClauseField := ""
285+
if c.excludeCurrentUser {
286+
excludeCurrentUserClauseField = excludeCurrentUserClause
287+
}
288+
query := fmt.Sprintf(selectPgStatActivity, queryTextField, excludeCurrentUserClauseField)
279289
rows, err := c.dbConnection.QueryContext(ctx, query)
280290
if err != nil {
281291
return fmt.Errorf("failed to query pg_stat_activity: %w", err)

0 commit comments

Comments
 (0)