diff --git a/CHANGELOG.md b/CHANGELOG.md index 827e29b14..1074f525c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/internal/component/database_observability/mysql/collector/query_sample.go b/internal/component/database_observability/mysql/collector/query_sample.go index 28422196c..5252ed2a7 100644 --- a/internal/component/database_observability/mysql/collector/query_sample.go +++ b/internal/component/database_observability/mysql/collector/query_sample.go @@ -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) diff --git a/internal/component/database_observability/mysql/collector/query_sample_test.go b/internal/component/database_observability/mysql/collector/query_sample_test.go index 3bd50675d..96dc30eff 100644 --- a/internal/component/database_observability/mysql/collector/query_sample_test.go +++ b/internal/component/database_observability/mysql/collector/query_sample_test.go @@ -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{{ @@ -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{{