Skip to content

Commit 2e79b14

Browse files
committed
database_observability: don't log query text if redaction is enabled
1 parent cfc4715 commit 2e79b14

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (c *QuerySamples) Name() string {
127127

128128
func (c *QuerySamples) Start(ctx context.Context) error {
129129
if c.disableQueryRedaction {
130-
level.Warn(c.logger).Log("msg", "collector started with query redaction disabled. Query samples will include complete SQL text including query parameters.")
130+
level.Warn(c.logger).Log("msg", "collector started with query redaction disabled. SQL text in query samples might include query parameters.")
131131
} else {
132132
level.Debug(c.logger).Log("msg", "collector started")
133133
}

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const (
2424
)
2525

2626
const (
27-
stateActive = "active"
27+
queryTextClause = ", s.query"
28+
stateActive = "active"
2829
)
2930

3031
const selectPgStatActivity = `
@@ -48,16 +49,16 @@ const selectPgStatActivity = `
4849
s.wait_event,
4950
pg_blocking_pids(s.pid) as blocked_by_pids,
5051
s.query_start,
51-
s.query_id,
52-
s.query
52+
s.query_id
53+
%s
5354
FROM pg_stat_activity s
5455
JOIN pg_database d ON s.datid = d.oid AND NOT d.datistemplate AND d.datallowconn
5556
WHERE
5657
s.pid != pg_backend_pid() AND
5758
s.state != 'idle' AND
5859
(
5960
s.backend_type != 'client backend' OR
60-
(
61+
(
6162
coalesce(TRIM(s.query), '') != '' AND
6263
s.query_id != 0
6364
)
@@ -194,7 +195,11 @@ func (c *QuerySamples) Name() string {
194195
}
195196

196197
func (c *QuerySamples) Start(ctx context.Context) error {
197-
level.Debug(c.logger).Log("msg", "collector started")
198+
if c.disableQueryRedaction {
199+
level.Warn(c.logger).Log("msg", "collector started with query redaction disabled. SQL text in query samples might include query parameters.")
200+
} else {
201+
level.Debug(c.logger).Log("msg", "collector started")
202+
}
198203

199204
c.running.Store(true)
200205
ctx, cancel := context.WithCancel(ctx)
@@ -236,7 +241,13 @@ func (c *QuerySamples) Stop() {
236241
}
237242

238243
func (c *QuerySamples) fetchQuerySample(ctx context.Context) error {
239-
rows, err := c.dbConnection.QueryContext(ctx, selectPgStatActivity)
244+
queryTextField := ""
245+
if c.disableQueryRedaction {
246+
queryTextField = queryTextClause
247+
}
248+
249+
query := fmt.Sprintf(selectPgStatActivity, queryTextField)
250+
rows, err := c.dbConnection.QueryContext(ctx, query)
240251
if err != nil {
241252
return fmt.Errorf("failed to query pg_stat_activity: %w", err)
242253
}
@@ -279,7 +290,7 @@ func (c *QuerySamples) fetchQuerySample(ctx context.Context) error {
279290

280291
func (c *QuerySamples) scanRow(rows *sql.Rows) (QuerySamplesInfo, error) {
281292
sample := QuerySamplesInfo{}
282-
err := rows.Scan(
293+
scanArgs := []interface{}{
283294
&sample.Now,
284295
&sample.DatabaseName,
285296
&sample.PID,
@@ -300,8 +311,11 @@ func (c *QuerySamples) scanRow(rows *sql.Rows) (QuerySamplesInfo, error) {
300311
&sample.BlockedByPIDs,
301312
&sample.QueryStart,
302313
&sample.QueryID,
303-
&sample.Query,
304-
)
314+
}
315+
if c.disableQueryRedaction {
316+
scanArgs = append(scanArgs, &sample.Query)
317+
}
318+
err := rows.Scan(scanArgs...)
305319
return sample, err
306320
}
307321

@@ -426,13 +440,8 @@ func (c *QuerySamples) buildQuerySampleLabels(state *SampleState) string {
426440
}
427441
}
428442

429-
queryText := state.LastRow.Query.String
430-
if !c.disableQueryRedaction {
431-
queryText = redact(queryText)
432-
}
433-
434443
labels := fmt.Sprintf(
435-
`datname="%s" pid="%d" leader_pid="%s" user="%s" app="%s" client="%s" backend_type="%s" state="%s" xid="%d" xmin="%d" xact_time="%s" query_time="%s" queryid="%d" query="%s" engine="postgres"`,
444+
`datname="%s" pid="%d" leader_pid="%s" user="%s" app="%s" client="%s" backend_type="%s" state="%s" xid="%d" xmin="%d" xact_time="%s" query_time="%s" queryid="%d"`,
436445
state.LastRow.DatabaseName.String,
437446
state.LastRow.PID,
438447
leaderPID,
@@ -446,11 +455,13 @@ func (c *QuerySamples) buildQuerySampleLabels(state *SampleState) string {
446455
xactDuration,
447456
queryDuration,
448457
state.LastRow.QueryID.Int64,
449-
queryText,
450458
)
451459
if state.LastCpuTime != "" {
452460
labels = fmt.Sprintf(`%s cpu_time="%s"`, labels, state.LastCpuTime)
453461
}
462+
if c.disableQueryRedaction && state.LastRow.Query.Valid {
463+
labels = fmt.Sprintf(`%s query_text="%s"`, labels, state.LastRow.Query.String)
464+
}
454465
return labels
455466
}
456467

@@ -460,12 +471,8 @@ func (c *QuerySamples) buildWaitEventLabels(state *SampleState, we WaitEventOccu
460471
if state.LastRow.LeaderPID.Valid {
461472
leaderPID = fmt.Sprintf(`%d`, state.LastRow.LeaderPID.Int64)
462473
}
463-
queryText := state.LastRow.Query.String
464-
if !c.disableQueryRedaction {
465-
queryText = redact(queryText)
466-
}
467474
return fmt.Sprintf(
468-
`datname="%s" pid="%d" leader_pid="%s" user="%s" backend_type="%s" state="%s" xid="%d" xmin="%d" wait_time="%s" wait_event_type="%s" wait_event="%s" wait_event_name="%s" blocked_by_pids="%v" queryid="%d" query="%s" engine="postgres"`,
475+
`datname="%s" pid="%d" leader_pid="%s" user="%s" backend_type="%s" state="%s" xid="%d" xmin="%d" wait_time="%s" wait_event_type="%s" wait_event="%s" wait_event_name="%s" blocked_by_pids="%v" queryid="%d"`,
469476
state.LastRow.DatabaseName.String,
470477
state.LastRow.PID,
471478
leaderPID,
@@ -480,7 +487,6 @@ func (c *QuerySamples) buildWaitEventLabels(state *SampleState, we WaitEventOccu
480487
waitEventFullName,
481488
we.BlockedByPIDs,
482489
state.LastRow.QueryID.Int64,
483-
queryText,
484490
)
485491
}
486492

0 commit comments

Comments
 (0)