Skip to content

Commit

Permalink
database_observability: attempt to parse queries truncated in a comment
Browse files Browse the repository at this point in the history
The `query_sample` collector for now attempts to detect if a query is
truncated in a multi-line comment (those between `/*` and `*/`).

This is a best-effort attempt to avoid skipping queries that might be
truncated in the middle of a trailing comment that is not closed, but
the first part of the query is still valid.
  • Loading branch information
cristiangreco committed Jan 17, 2025
1 parent f39e143 commit cdf253c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Main (unreleased)

- (_Experimental_) Log instance label key in `database_observability.mysql` (@cristiangreco)

- (_Experimental_) Improve parsing of truncated queries in `database_observability.mysql` (@cristiangreco)

- Add json format support for log export via faro receiver (@ravishankar15)

v1.6.0-rc.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,16 @@ func (c *QuerySample) fetchQuerySamples(ctx context.Context) error {
}

if strings.HasSuffix(sampleText, "...") {
level.Debug(c.logger).Log("msg", "skipping parsing truncated query", "digest", digest)
continue
// best-effort attempt to detect truncated trailing comment
if idx := strings.LastIndex(sampleText, "/*"); idx >= 0 {
trailingPart := sampleText[idx:]
if strings.LastIndex(trailingPart, "*/") < 0 {
sampleText = sampleText[:idx]
}
} else {
level.Debug(c.logger).Log("msg", "skipping parsing truncated query", "digest", digest)
continue
}
}

stmt, err := sqlparser.Parse(sampleText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ func TestQuerySample(t *testing.T) {
`level=info msg="table name parsed" op="query_parsed_table_name" instance="mysql-db" digest="abc123" table="performance_schema.events_statements_summary_by_digest"`,
},
},
{
name: "with comment",
rows: [][]driver.Value{{
"abc123",
"select val1, /* val2,*/ val3 from some_table where id = 1",
"2024-01-01T00:00:00.000Z",
"1000",
}},
logs: []string{
`level=info msg="query samples fetched" op="query_sample" instance="mysql-db" digest="abc123" query_type="select" query_sample_seen="2024-01-01T00:00:00.000Z" query_sample_timer_wait="1000" query_sample_redacted="select val1, val3 from some_table where id = :redacted1"`,
`level=info msg="table name parsed" op="query_parsed_table_name" instance="mysql-db" digest="abc123" table="some_table"`,
},
},
{
name: "truncated query",
rows: [][]driver.Value{{
Expand All @@ -129,6 +142,19 @@ func TestQuerySample(t *testing.T) {
`level=info msg="table name parsed" op="query_parsed_table_name" instance="mysql-db" digest="abc123" table="some_table"`,
},
},
{
name: "truncated in multi-line comment",
rows: [][]driver.Value{{
"abc123",
"select * from some_table where id = 1 /*traceparent='00-abc...",
"2024-01-01T00:00:00.000Z",
"1000",
}},
logs: []string{
`level=info msg="query samples fetched" op="query_sample" instance="mysql-db" digest="abc123" query_type="select" query_sample_seen="2024-01-01T00:00:00.000Z" query_sample_timer_wait="1000" query_sample_redacted="select * from some_table where id = :redacted1"`,
`level=info msg="table name parsed" op="query_parsed_table_name" instance="mysql-db" digest="abc123" table="some_table"`,
},
},
{
name: "start transaction",
rows: [][]driver.Value{{
Expand Down

0 comments on commit cdf253c

Please sign in to comment.