-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(query_perf_monitoring): Implement query performance monitoring m…
- Loading branch information
Showing
52 changed files
with
3,278 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/query-performance-monitoring/common-utils/common_helpers.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package commonutils | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
"math/big" | ||
"regexp" | ||
"strings" | ||
"time" | ||
|
||
"github.com/newrelic/nri-postgresql/src/collection" | ||
) | ||
|
||
// re is a regular expression that matches single-quoted strings, numbers, or double-quoted strings | ||
var re = regexp.MustCompile(`'[^']*'|\d+|".*?"`) | ||
|
||
func GetQuotedStringFromArray(array []string) string { | ||
var quotedNames = make([]string, 0) | ||
for _, name := range array { | ||
quotedNames = append(quotedNames, fmt.Sprintf("'%s'", name)) | ||
} | ||
return strings.Join(quotedNames, ",") | ||
} | ||
|
||
func GetDatabaseListInString(dbList collection.DatabaseList) string { | ||
var databaseNames = make([]string, 0) | ||
for dbName := range dbList { | ||
databaseNames = append(databaseNames, dbName) | ||
} | ||
if len(databaseNames) == 0 { | ||
return "" | ||
} | ||
return GetQuotedStringFromArray(databaseNames) | ||
} | ||
|
||
func AnonymizeQueryText(query string) string { | ||
anonymizedQuery := re.ReplaceAllString(query, "?") | ||
return anonymizedQuery | ||
} | ||
|
||
// This function is used to generate a unique plan ID for a query | ||
func GeneratePlanID(queryID string) *string { | ||
randomInt, err := rand.Int(rand.Reader, big.NewInt(RandomIntRange)) | ||
if err != nil { | ||
return nil | ||
} | ||
currentTime := time.Now().Format(TimeFormat) | ||
result := fmt.Sprintf("%s-%d-%s", queryID, randomInt.Int64(), currentTime) | ||
return &result | ||
} |
56 changes: 56 additions & 0 deletions
56
src/query-performance-monitoring/common-utils/common_helpers_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package commonutils_test | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
"time" | ||
|
||
"github.com/newrelic/nri-postgresql/src/collection" | ||
commonutils "github.com/newrelic/nri-postgresql/src/query-performance-monitoring/common-utils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetQuotedStringFromArray(t *testing.T) { | ||
input := []string{"db1", "db2", "db3"} | ||
expected := "'db1','db2','db3'" | ||
result := commonutils.GetQuotedStringFromArray(input) | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestGetDatabaseListInString(t *testing.T) { | ||
dbListKeys := []string{"db1"} | ||
sort.Strings(dbListKeys) // Sort the keys to ensure consistent order | ||
dbList := collection.DatabaseList{} | ||
for _, key := range dbListKeys { | ||
dbList[key] = collection.SchemaList{} | ||
} | ||
expected := "'db1'" | ||
result := commonutils.GetDatabaseListInString(dbList) | ||
assert.Equal(t, expected, result) | ||
|
||
// Test with empty database list | ||
dbList = collection.DatabaseList{} | ||
expected = "" | ||
result = commonutils.GetDatabaseListInString(dbList) | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestAnonymizeQueryText(t *testing.T) { | ||
query := "SELECT * FROM users WHERE id = 1 AND name = 'John'" | ||
expected := "SELECT * FROM users WHERE id = ? AND name = ?" | ||
result := commonutils.AnonymizeQueryText(query) | ||
assert.Equal(t, expected, result) | ||
query = "SELECT * FROM employees WHERE id = 10 OR name <> 'John Doe' OR name != 'John Doe' OR age < 30 OR age <= 30 OR salary > 50000OR salary >= 50000 OR department LIKE 'Sales%' OR department ILIKE 'sales%'OR join_date BETWEEN '2023-01-01' AND '2023-12-31' OR department IN ('HR', 'Engineering', 'Marketing') OR department IS NOT NULL OR department IS NULL;" | ||
expected = "SELECT * FROM employees WHERE id = ? OR name <> ? OR name != ? OR age < ? OR age <= ? OR salary > ?OR salary >= ? OR department LIKE ? OR department ILIKE ?OR join_date BETWEEN ? AND ? OR department IN (?, ?, ?) OR department IS NOT NULL OR department IS NULL;" | ||
result = commonutils.AnonymizeQueryText(query) | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestGeneratePlanID(t *testing.T) { | ||
queryID := "query123" | ||
result := commonutils.GeneratePlanID(queryID) | ||
assert.NotNil(t, result) | ||
assert.Contains(t, *result, queryID) | ||
assert.Contains(t, *result, "-") | ||
assert.Contains(t, *result, time.Now().Format(commonutils.TimeFormat)[:8]) // Check date part | ||
} |
24 changes: 24 additions & 0 deletions
24
src/query-performance-monitoring/common-utils/constants.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package commonutils | ||
|
||
import "errors" | ||
|
||
const MaxQueryThreshold = 30 | ||
const MaxIndividualQueryThreshold = 10 | ||
const PublishThreshold = 100 | ||
const RandomIntRange = 1000000 | ||
const TimeFormat = "20060102150405" | ||
const VersionRegex = "PostgreSQL (\\d+)\\." | ||
|
||
var ErrParseVersion = errors.New("unable to parse PostgreSQL version from string") | ||
var ErrUnsupportedVersion = errors.New("unsupported PostgreSQL version") | ||
var ErrUnExpectedError = errors.New("unexpected error") | ||
|
||
var ErrVersionFetchError = errors.New("no rows returned from version query") | ||
var ErrInvalidModelType = errors.New("invalid model type") | ||
var ErrNotEligible = errors.New("not Eligible to fetch metrics") | ||
|
||
const PostgresVersion12 = 12 | ||
const PostgresVersion11 = 11 | ||
const PostgresVersion13 = 13 | ||
const PostgresVersion14 = 14 | ||
const VersionIndex = 2 |
Oops, something went wrong.