Skip to content
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

PMM-13373 Different QAN max query length for MongoDB. #3273

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -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.MongoQuery(v.Fingerprint, a.maxQueryLength)
query, truncated := truncate.MongoQuery(v.Query, a.maxQueryLength)
bucket := &agentv1.MetricsBucket{
Common: &agentv1.MetricsBucket_Common{
Queryid: v.ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion agent/agents/mongodb/internal/profiler/profiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 26 additions & 4 deletions agent/utils/truncate/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,34 @@
// 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).
// Also truncate all invalid UTF-8 chars.
func Query(q string, maxQueryLength int32) (string, bool) {
if maxQueryLength < 0 {
return string([]rune(q)), false
if maxQueryLength == 0 {
maxQueryLength = defaultMaxQueryLength
}

return query(q, maxQueryLength)
}

// MongoQuery truncate query to specific length of chars, if needed. -1: No limit, 0: Default (4096).
// Also truncate all invalid UTF-8 chars.
func MongoQuery(q string, maxQueryLength int32) (string, bool) {
Copy link
Member

Choose a reason for hiding this comment

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

do we really need it to be a separate function instead of 3rd parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No we dont, but for me it looked more clear. Also it has less affected files. Changed back to one func with 3 params.

if maxQueryLength == 0 {
maxQueryLength = defaultMaxQueryLength
maxQueryLength = defaultMongoDBMaxQueryLength
}

return query(q, maxQueryLength)
}

func query(q string, maxQueryLength int32) (string, bool) {
if maxQueryLength < 0 {
return string([]rune(q)), false
}

runes := []rune(q)
Expand All @@ -46,3 +63,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
}
8 changes: 1 addition & 7 deletions agent/utils/truncate/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
assert.Equal(t, expected.query, query)
assert.Equal(t, expected.truncated, truncated)
}
Expand Down
Loading