Skip to content

[pg_stat_statements] Strip special characters from query #1166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ This will build the docker image as `prometheuscommunity/postgres_exporter:${bra
* `--collector.stat_statements.query_length`
Maximum length of the statement text. Default is 120.

* `--[no-]collector.stat_statements.strip_special`
Strip special characters like line breaks, carriage returns, tabs from query text. (default: disabled)

* `[no-]collector.stat_user_tables`
Enable the `stat_user_tables` collector (default: enabled).

Expand Down
19 changes: 16 additions & 3 deletions collector/pg_stat_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (
const statStatementsSubsystem = "stat_statements"

var (
includeQueryFlag *bool = nil
statementLengthFlag *uint = nil
includeQueryFlag *bool = nil
statementStripSpecialFlag *bool = nil
statementLengthFlag *uint = nil
)

func init() {
Expand All @@ -42,6 +43,11 @@ func init() {
"Enable selecting statement query together with queryId. (default: disabled)").
Default(fmt.Sprintf("%v", defaultDisabled)).
Bool()
statementStripSpecialFlag = kingpin.Flag(
fmt.Sprint(collectorFlagPrefix, statStatementsSubsystem, ".strip_special"),
"Strip special characters from query text. (default: disabled)").
Default(fmt.Sprintf("%v", defaultDisabled)).
Bool()
statementLengthFlag = kingpin.Flag(
fmt.Sprint(collectorFlagPrefix, statStatementsSubsystem, ".query_length"),
"Maximum length of the statement text.").
Expand All @@ -52,13 +58,15 @@ func init() {
type PGStatStatementsCollector struct {
log *slog.Logger
includeQueryStatement bool
stripSpecialFromQuery bool
statementLength uint
}

func NewPGStatStatementsCollector(config collectorConfig) (Collector, error) {
return &PGStatStatementsCollector{
log: config.logger,
includeQueryStatement: *includeQueryFlag,
stripSpecialFromQuery: *statementStripSpecialFlag,
statementLength: *statementLengthFlag,
}, nil
}
Expand Down Expand Up @@ -105,6 +113,7 @@ var (

const (
pgStatStatementQuerySelect = `LEFT(pg_stat_statements.query, %d) as query,`
pgStatStatementQuerySelectStrip = `LEFT(regexp_replace(pg_stat_statements.query, '\s+', ' ', 'g'), %d) as query,`

pgStatStatementsQuery = `SELECT
pg_get_userbyid(userid) as user,
Expand Down Expand Up @@ -185,7 +194,11 @@ func (c PGStatStatementsCollector) Update(ctx context.Context, instance *instanc
}
var querySelect = ""
if c.includeQueryStatement {
querySelect = fmt.Sprintf(pgStatStatementQuerySelect, c.statementLength)
if c.stripSpecialFromQuery {
querySelect = fmt.Sprintf(pgStatStatementQuerySelectStrip, c.statementLength)
} else {
querySelect = fmt.Sprintf(pgStatStatementQuerySelect, c.statementLength)
}
}
query := fmt.Sprintf(queryTemplate, querySelect)

Expand Down