Skip to content

feat: Strip comments from normalized sql text in database_observability.postgres#5005

Merged
cristiangreco merged 2 commits intomainfrom
cristian/dbo11y-pg-remove-comments
Jan 8, 2026
Merged

feat: Strip comments from normalized sql text in database_observability.postgres#5005
cristiangreco merged 2 commits intomainfrom
cristian/dbo11y-pg-remove-comments

Conversation

@cristiangreco
Copy link
Contributor

PR Description

Strip away comments from pg_stat_statements normalized sql queries, as postgres doesn't do that automatically.

While at it:

  • move some stuff from lexer.go closer to postgres component
  • keep a normalizer instance around in query_details.go
  • polish some test cases formatting

Which issue(s) this PR fixes

n.a.

Notes to the Reviewer

PR Checklist

  • CHANGELOG.md updated
  • Documentation added
  • Tests updated
  • Config converters updated

@cristiangreco cristiangreco marked this pull request as ready for review December 4, 2025 15:34
@cristiangreco cristiangreco requested a review from a team as a code owner December 4, 2025 15:34
@cristiangreco cristiangreco force-pushed the cristian/dbo11y-pg-remove-comments branch from 982e208 to f3619fb Compare December 4, 2025 15:34
c.entryHandler.Chan() <- database_observability.BuildLokiEntry(
logging.LevelInfo,
OP_QUERY_ASSOCIATION,
fmt.Sprintf(`queryid="%s" querytext=%q datname="%s" engine="postgres"`, queryID, queryText, databaseName),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason we're removing the db engine? I understand that postgres is uniquely identified by the other values, I.E. queryid and datname. That said, having the engine listed doesn't increase cardinality, and isn't a huge increase in log line size either.

I don't have a strong opinion either way, just curious what the motivation is here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I understand that postgres is uniquely identified by the other values, I.E. queryid and datname. That said, having the engine listed doesn't increase cardinality, and isn't a huge increase in log line size either.

Yes that's correct, it doesn't really change the cardinality. It was added there at early development stage though and we've removed it from various places over time (this is likely the last place where it appears). Not a big deal, doing it just for consistency at this point.

@CLAassistant
Copy link

CLAassistant commented Dec 17, 2025

CLA assistant check
All committers have signed the CLA.

@cristiangreco cristiangreco force-pushed the cristian/dbo11y-pg-remove-comments branch from a180fcb to c204afc Compare January 7, 2026 10:23
@cristiangreco cristiangreco changed the title dbo11y: remove comments from normalized sql text in postgres feat: dbo11y: remove comments from normalized sql text in postgres Jan 7, 2026
@cristiangreco cristiangreco changed the title feat: dbo11y: remove comments from normalized sql text in postgres feat: Database Observability: remove comments from normalized sql text in postgres Jan 7, 2026
}

func (c QueryDetails) tryTokenizeTableNames(sqlText string) ([]string, error) {
func TokenizeTableNames(normalizer *sqllexer.Normalizer, sqlText string) ([]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be exposed?

return metadata.Tables, nil
}

func RemoveComments(normalizer *sqllexer.Normalizer, sqlText string) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this need to be exposed?

_, metadata, err := normalizer.Normalize(sqlText, sqllexer.WithDBMS(sqllexer.DBMSPostgres))
if err != nil {
return nil, fmt.Errorf("failed to extract table names: %w", err)
return sqlText, fmt.Errorf("failed to redact comments: %w", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: this is normalising, not redacting comments (which happens later)

Strip away comments from `pg_stat_statements` normalized sql queries,
as postgres doesn't do that automatically.

While at it:
- move some stuff from `lexer.go` closer to postgres component
- keep a normalizer instance around in `query_details.go`
- polish some test cases formatting
@cristiangreco cristiangreco force-pushed the cristian/dbo11y-pg-remove-comments branch from c204afc to 0275648 Compare January 8, 2026 13:28
@cristiangreco cristiangreco changed the title feat: Database Observability: remove comments from normalized sql text in postgres feat: database_observability: remove comments from normalized sql text in postgres Jan 8, 2026
@cristiangreco cristiangreco changed the title feat: database_observability: remove comments from normalized sql text in postgres feat: Strip comments from normalized sql text in database_observability.postgres Jan 8, 2026
@cristiangreco cristiangreco merged commit a58721a into main Jan 8, 2026
46 of 47 checks passed
@cristiangreco cristiangreco deleted the cristian/dbo11y-pg-remove-comments branch January 8, 2026 14:30
@grafana-alloybot grafana-alloybot bot mentioned this pull request Jan 8, 2026
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants