diff --git a/agent/agents/mongodb/internal/profiler/aggregator/aggregator.go b/agent/agents/mongodb/internal/profiler/aggregator/aggregator.go index 9b80c9d728..fe429aa1c4 100644 --- a/agent/agents/mongodb/internal/profiler/aggregator/aggregator.go +++ b/agent/agents/mongodb/internal/profiler/aggregator/aggregator.go @@ -254,8 +254,8 @@ func (a *Aggregator) createResult(ctx context.Context) *report.Result { collection = s[1] } - fingerprint, _ := truncate.Query(v.Fingerprint, a.maxQueryLength) - query, truncated := truncate.Query(v.Query, a.maxQueryLength) + fingerprint, _ := truncate.Query(v.Fingerprint, a.maxQueryLength, truncate.GetMongoDBDefaultMaxQueryLength()) + query, truncated := truncate.Query(v.Query, a.maxQueryLength, truncate.GetMongoDBDefaultMaxQueryLength()) bucket := &agentv1.MetricsBucket{ Common: &agentv1.MetricsBucket_Common{ Queryid: v.ID, diff --git a/agent/agents/mongodb/internal/profiler/aggregator/aggregator_test.go b/agent/agents/mongodb/internal/profiler/aggregator/aggregator_test.go index 4cf8980c58..7eb650dd34 100644 --- a/agent/agents/mongodb/internal/profiler/aggregator/aggregator_test.go +++ b/agent/agents/mongodb/internal/profiler/aggregator/aggregator_test.go @@ -37,7 +37,7 @@ func TestAggregator(t *testing.T) { // we need at least one test per package to correctly calculate coverage t.Run("Add", func(t *testing.T) { t.Run("error if aggregator is not running", func(t *testing.T) { - a := New(time.Now(), "test-agent", logrus.WithField("component", "test"), truncate.GetDefaultMaxQueryLength()) + a := New(time.Now(), "test-agent", logrus.WithField("component", "test"), truncate.GetMongoDBDefaultMaxQueryLength()) err := a.Add(nil, proto.SystemProfile{}) assert.EqualError(t, err, "aggregator is not running") }) @@ -46,7 +46,7 @@ func TestAggregator(t *testing.T) { t.Run("createResult", func(t *testing.T) { agentID := "test-agent" startPeriod := time.Now() - aggregator := New(startPeriod, agentID, logrus.WithField("component", "test"), truncate.GetDefaultMaxQueryLength()) + aggregator := New(startPeriod, agentID, logrus.WithField("component", "test"), truncate.GetMongoDBDefaultMaxQueryLength()) aggregator.Start() defer aggregator.Stop() ctx := context.TODO() @@ -99,7 +99,7 @@ func TestAggregator(t *testing.T) { t.Run("createResultInvalidUTF8", func(t *testing.T) { agentID := "test-agent" startPeriod := time.Now() - aggregator := New(startPeriod, agentID, logrus.WithField("component", "test"), truncate.GetDefaultMaxQueryLength()) + aggregator := New(startPeriod, agentID, logrus.WithField("component", "test"), truncate.GetMongoDBDefaultMaxQueryLength()) aggregator.Start() defer aggregator.Stop() ctx := context.TODO() diff --git a/agent/agents/mongodb/internal/profiler/parser/parser_test.go b/agent/agents/mongodb/internal/profiler/parser/parser_test.go index 834fe7cc19..9aff073334 100644 --- a/agent/agents/mongodb/internal/profiler/parser/parser_test.go +++ b/agent/agents/mongodb/internal/profiler/parser/parser_test.go @@ -33,7 +33,7 @@ import ( func TestNew(t *testing.T) { docsChan := make(chan pm.SystemProfile) - a := aggregator.New(time.Now(), "test-id", logrus.WithField("component", "aggregator"), truncate.GetDefaultMaxQueryLength()) + a := aggregator.New(time.Now(), "test-id", logrus.WithField("component", "aggregator"), truncate.GetMongoDBDefaultMaxQueryLength()) type args struct { docsChan <-chan pm.SystemProfile @@ -65,7 +65,7 @@ func TestNew(t *testing.T) { func TestParserStartStop(t *testing.T) { var err error docsChan := make(chan pm.SystemProfile) - a := aggregator.New(time.Now(), "test-id", logrus.WithField("component", "aggregator"), truncate.GetDefaultMaxQueryLength()) + a := aggregator.New(time.Now(), "test-id", logrus.WithField("component", "aggregator"), truncate.GetMongoDBDefaultMaxQueryLength()) ctx := context.TODO() parser1 := New(docsChan, a, logrus.WithField("component", "test-parser")) @@ -83,7 +83,7 @@ func TestParserStartStop(t *testing.T) { func TestParserRunning(t *testing.T) { docsChan := make(chan pm.SystemProfile) - a := aggregator.New(time.Now(), "test-id", logrus.WithField("component", "aggregator"), truncate.GetDefaultMaxQueryLength()) + a := aggregator.New(time.Now(), "test-id", logrus.WithField("component", "aggregator"), truncate.GetMongoDBDefaultMaxQueryLength()) reportChan := a.Start() defer a.Stop() d := aggregator.DefaultInterval diff --git a/agent/agents/mongodb/internal/profiler/profiler_test.go b/agent/agents/mongodb/internal/profiler/profiler_test.go index c4c4802243..286c097788 100644 --- a/agent/agents/mongodb/internal/profiler/profiler_test.go +++ b/agent/agents/mongodb/internal/profiler/profiler_test.go @@ -103,7 +103,7 @@ func testProfiler(t *testing.T, url string) { t: t, reports: []*report.Report{}, } - prof := New(url, logrus.WithField("component", "profiler-test"), ms, "test-id", truncate.GetDefaultMaxQueryLength()) + prof := New(url, logrus.WithField("component", "profiler-test"), ms, "test-id", truncate.GetMongoDBDefaultMaxQueryLength()) err = prof.Start() defer prof.Stop() require.NoError(t, err) diff --git a/agent/agents/mysql/perfschema/perfschema.go b/agent/agents/mysql/perfschema/perfschema.go index 7da93c9781..0853509a3f 100644 --- a/agent/agents/mysql/perfschema/perfschema.go +++ b/agent/agents/mysql/perfschema/perfschema.go @@ -337,7 +337,7 @@ func (m *PerfSchema) getNewBuckets(periodStart time.Time, periodLengthSecs uint3 if esh.SQLText != nil { explainFingerprint, placeholdersCount := queryparser.GetMySQLFingerprintPlaceholders(*esh.SQLText, *esh.DigestText) - explainFingerprint, truncated := truncate.Query(explainFingerprint, m.maxQueryLength) + explainFingerprint, truncated := truncate.Query(explainFingerprint, m.maxQueryLength, truncate.GetDefaultMaxQueryLength()) if truncated { b.Common.IsTruncated = truncated } @@ -345,7 +345,7 @@ func (m *PerfSchema) getNewBuckets(periodStart time.Time, periodLengthSecs uint3 b.Common.PlaceholdersCount = placeholdersCount if !m.disableQueryExamples { - example, truncated := truncate.Query(*esh.SQLText, m.maxQueryLength) + example, truncated := truncate.Query(*esh.SQLText, m.maxQueryLength, truncate.GetDefaultMaxQueryLength()) if truncated { b.Common.IsTruncated = truncated } @@ -407,7 +407,7 @@ func makeBuckets(current, prev summaryMap, l *logrus.Entry, maxQueryLength int32 } count := inc(currentESS.CountStar, prevESS.CountStar) - fingerprint, isTruncated := truncate.Query(*currentESS.DigestText, maxQueryLength) + fingerprint, isTruncated := truncate.Query(*currentESS.DigestText, maxQueryLength, truncate.GetDefaultMaxQueryLength()) mb := &agentv1.MetricsBucket{ Common: &agentv1.MetricsBucket_Common{ Schema: pointer.GetString(currentESS.SchemaName), // TODO can it be NULL? diff --git a/agent/agents/mysql/slowlog/slowlog.go b/agent/agents/mysql/slowlog/slowlog.go index cddcf8003d..136c7193b2 100644 --- a/agent/agents/mysql/slowlog/slowlog.go +++ b/agent/agents/mysql/slowlog/slowlog.go @@ -431,7 +431,7 @@ func makeBuckets( // added here: https://github.com/percona/go-mysql/blob/v3/event/class.go#L56 q := v.Fingerprint v.Fingerprint = query.Fingerprint(v.Fingerprint) - fingerprint, isTruncated := truncate.Query(v.Fingerprint, maxQueryLength) + fingerprint, isTruncated := truncate.Query(v.Fingerprint, maxQueryLength, truncate.GetDefaultMaxQueryLength()) mb := &agentv1.MetricsBucket{ Common: &agentv1.MetricsBucket_Common{ Queryid: v.Id, @@ -454,7 +454,7 @@ func makeBuckets( if q != "" { explainFingerprint, placeholdersCount := queryparser.GetMySQLFingerprintPlaceholders(q, fingerprint) - explainFingerprint, truncated := truncate.Query(explainFingerprint, maxQueryLength) + explainFingerprint, truncated := truncate.Query(explainFingerprint, maxQueryLength, truncate.GetDefaultMaxQueryLength()) if truncated { mb.Common.IsTruncated = truncated } @@ -471,7 +471,7 @@ func makeBuckets( } if v.Example != nil && !disableQueryExamples { - example, truncated := truncate.Query(v.Example.Query, maxQueryLength) + example, truncated := truncate.Query(v.Example.Query, maxQueryLength, truncate.GetDefaultMaxQueryLength()) if truncated { mb.Common.IsTruncated = truncated } diff --git a/agent/agents/postgres/pgstatmonitor/stat_monitor_cache.go b/agent/agents/postgres/pgstatmonitor/stat_monitor_cache.go index 28635a135c..edaf1f5a66 100644 --- a/agent/agents/postgres/pgstatmonitor/stat_monitor_cache.go +++ b/agent/agents/postgres/pgstatmonitor/stat_monitor_cache.go @@ -184,11 +184,11 @@ func (ssc *statMonitorCache) getStatMonitorExtended( c.Fingerprint = c.Query } else { var isTruncated bool - c.Fingerprint, isTruncated = truncate.Query(fingerprint, maxQueryLength) + c.Fingerprint, isTruncated = truncate.Query(fingerprint, maxQueryLength, truncate.GetDefaultMaxQueryLength()) if isTruncated { c.IsQueryTruncated = isTruncated } - c.Example, isTruncated = truncate.Query(example, maxQueryLength) + c.Example, isTruncated = truncate.Query(example, maxQueryLength, truncate.GetDefaultMaxQueryLength()) if isTruncated { c.IsQueryTruncated = isTruncated } diff --git a/agent/agents/postgres/pgstatstatements/pgstatstatements.go b/agent/agents/postgres/pgstatstatements/pgstatstatements.go index 799e165b96..52bae79df4 100644 --- a/agent/agents/postgres/pgstatstatements/pgstatstatements.go +++ b/agent/agents/postgres/pgstatstatements/pgstatstatements.go @@ -262,7 +262,7 @@ func (m *PGStatStatementsQAN) getStatStatementsExtended( } else { newN++ - c.Query, c.IsQueryTruncated = truncate.Query(c.Query, maxQueryLength) + c.Query, c.IsQueryTruncated = truncate.Query(c.Query, maxQueryLength, truncate.GetDefaultMaxQueryLength()) } current[c.QueryID] = c diff --git a/agent/agents/postgres/pgstatstatements/utils.go b/agent/agents/postgres/pgstatstatements/utils.go index 0f739df4bf..50f7147e98 100644 --- a/agent/agents/postgres/pgstatstatements/utils.go +++ b/agent/agents/postgres/pgstatstatements/utils.go @@ -74,7 +74,7 @@ func queryUsernames(q *reform.Querier) map[int64]string { func extractTables(query string, maxQueryLength int32, l *logrus.Entry) []string { start := time.Now() - t, _ := truncate.Query(query, maxQueryLength) + t, _ := truncate.Query(query, maxQueryLength, truncate.GetDefaultMaxQueryLength()) tables, err := parser.ExtractTables(query) if err != nil { // log full query and error stack on debug level or more diff --git a/agent/client/channel/channel_test.go b/agent/client/channel/channel_test.go index 2555c96b2d..6eb8b69527 100644 --- a/agent/client/channel/channel_test.go +++ b/agent/client/channel/channel_test.go @@ -108,9 +108,9 @@ func setup(t *testing.T, connect func(agentv1.AgentService_ConnectServer) error, func TestAgentRequestWithTruncatedInvalidUTF8(t *testing.T) { defaultMaxQueryLength := truncate.GetDefaultMaxQueryLength() - fingerprint, _ := truncate.Query("SELECT * FROM contacts t0 WHERE t0.person_id = '?';", defaultMaxQueryLength) + fingerprint, _ := truncate.Query("SELECT * FROM contacts t0 WHERE t0.person_id = '?';", defaultMaxQueryLength, truncate.GetDefaultMaxQueryLength()) invalidQuery := "SELECT * FROM contacts t0 WHERE t0.person_id = '\u0241\xff\\uD83D\xddü\xf1'" - query, _ := truncate.Query(invalidQuery, defaultMaxQueryLength) + query, _ := truncate.Query(invalidQuery, defaultMaxQueryLength, truncate.GetDefaultMaxQueryLength()) connect := func(stream agentv1.AgentService_ConnectServer) error { msg, err := stream.Recv() diff --git a/agent/utils/truncate/query.go b/agent/utils/truncate/query.go index 3e71f6fecf..3257789bac 100644 --- a/agent/utils/truncate/query.go +++ b/agent/utils/truncate/query.go @@ -15,11 +15,14 @@ // Package truncate privides strings truncation utilities. package truncate -var defaultMaxQueryLength = int32(2048) +const ( + defaultMaxQueryLength = int32(2048) + defaultMongoDBMaxQueryLength = int32(4096) +) -// Query truncate query to specific length of chars, if needed. -1: No limit, 0: Default (2048). +// Query truncate query to specific length of chars, if needed. -1: No limit, 0: Default (2048, Mongo 4096). // Also truncate all invalid UTF-8 chars. -func Query(q string, maxQueryLength int32) (string, bool) { +func Query(q string, maxQueryLength, defaultMaxQueryLength int32) (string, bool) { if maxQueryLength < 0 { return string([]rune(q)), false } @@ -46,3 +49,8 @@ func Query(q string, maxQueryLength int32) (string, bool) { func GetDefaultMaxQueryLength() int32 { return defaultMaxQueryLength } + +// GetMongoDBDefaultMaxQueryLength returns default decimal value for MongoDB query length. +func GetMongoDBDefaultMaxQueryLength() int32 { + return defaultMongoDBMaxQueryLength +} diff --git a/agent/utils/truncate/query_test.go b/agent/utils/truncate/query_test.go index 7be09ebbf0..dc5f01256b 100644 --- a/agent/utils/truncate/query_test.go +++ b/agent/utils/truncate/query_test.go @@ -21,12 +21,6 @@ import ( ) func TestQuery(t *testing.T) { - m := defaultMaxQueryLength - defaultMaxQueryLength = 5 - defer func() { - defaultMaxQueryLength = m - }() - for q, expected := range map[string]struct { query string truncated bool @@ -40,7 +34,7 @@ func TestQuery(t *testing.T) { "\xff\xff\xff\xff\xff": {"\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD", false}, "\xff\xff\xff\xff\xff\xff": {"\uFFFD ...", true}, } { - query, truncated := Query(q, defaultMaxQueryLength) + query, truncated := Query(q, 5, GetDefaultMaxQueryLength()) assert.Equal(t, expected.query, query) assert.Equal(t, expected.truncated, truncated) }