forked from newrelic/nri-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query performance monitoring undo change remove test query query performance monitoring undo changelog changes Query performance monitoring Query performance monitoring slow query len check in individual queries metrics populate trim query text lint issue fix undo changelog file change test remove OH queries remove OH queries reusing existing DB connection (#22) reusing existing DB connection DB specific metric collection (#23) DB specific metric collection query response time threshold (#24) query response time threshold change in individual query (#25) change in individual query version specific queries (#26) verison specific queries fix database name (#27) fix database name test test min max threshold on limit (#28) min max threshold on limit Feat version specific individual query (#29) * version specific indivual query Feat version specific individual query (#30) Version specific indivual query fix query id , ingestData:false and reuse individual version specific query (#31) fix query id , ingestData:false and reuse individual version specific query database list check (#32) database list check publish only if there are metrics (#33) publish only if there are metrics undo change log change
- Loading branch information
1 parent
c075367
commit a920d40
Showing
18 changed files
with
1,070 additions
and
4 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
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
package commonutils | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/newrelic/nri-postgresql/src/collection" | ||
) | ||
|
||
func GetQuotedStringFromArray(array []string) string { | ||
var quotedDatabaseNames = make([]string, 0) | ||
for _, name := range array { | ||
quotedDatabaseNames = append(quotedDatabaseNames, fmt.Sprintf("'%s'", name)) | ||
} | ||
return strings.Join(quotedDatabaseNames, ",") | ||
} | ||
|
||
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) | ||
} |
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,4 @@ | ||
package commonutils | ||
|
||
const MAX_QUERY_THRESHOLD = 30 | ||
const MAX_INDIVIDUAL_QUERY_THRESHOLD = 10 |
125 changes: 125 additions & 0 deletions
125
src/query-performance-monitoring/common-utils/ingestion-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,125 @@ | ||
package commonutils | ||
|
||
import ( | ||
"crypto/rand" | ||
"fmt" | ||
_ "github.com/lib/pq" | ||
"github.com/newrelic/infra-integrations-sdk/v3/data/metric" | ||
"github.com/newrelic/infra-integrations-sdk/v3/integration" | ||
"github.com/newrelic/infra-integrations-sdk/v3/log" | ||
"github.com/newrelic/nri-postgresql/src/args" | ||
"math/big" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
const publishThreshold = 100 | ||
const randomIntRange = 1000000 | ||
|
||
func SetMetric(metricSet *metric.Set, name string, value interface{}, sourceType string) { | ||
switch sourceType { | ||
case `gauge`: | ||
err := metricSet.SetMetric(name, value, metric.GAUGE) | ||
if err != nil { | ||
return | ||
} | ||
case `attribute`: | ||
err := metricSet.SetMetric(name, value, metric.ATTRIBUTE) | ||
if err != nil { | ||
return | ||
} | ||
default: | ||
err := metricSet.SetMetric(name, value, metric.GAUGE) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
} | ||
|
||
func IngestMetric(metricList []interface{}, eventName string, pgIntegration *integration.Integration, args args.ArgumentList) { | ||
instanceEntity, err := createEntity(pgIntegration, args) | ||
if err != nil { | ||
log.Error("Error creating entity: %v", err) | ||
return | ||
} | ||
|
||
metricCount := 0 | ||
lenOfMetricList := len(metricList) | ||
|
||
for _, model := range metricList { | ||
if model == nil { | ||
continue | ||
} | ||
metricCount += 1 | ||
metricSet := instanceEntity.NewMetricSet(eventName) | ||
|
||
processModel(model, metricSet) | ||
|
||
if metricCount == publishThreshold || metricCount == lenOfMetricList { | ||
metricCount = 0 | ||
if err := publishMetrics(pgIntegration, &instanceEntity, args); err != nil { | ||
log.Error("Error publishing metrics: %v", err) | ||
return | ||
} | ||
} | ||
} | ||
if metricCount > 0 { | ||
if err := publishMetrics(pgIntegration, &instanceEntity, args); err != nil { | ||
log.Error("Error publishing metrics: %v", err) | ||
return | ||
} | ||
} | ||
} | ||
|
||
func createEntity(pgIntegration *integration.Integration, args args.ArgumentList) (*integration.Entity, error) { | ||
return pgIntegration.Entity(fmt.Sprintf("%s:%s", args.Hostname, args.Port), "pg-instance") | ||
} | ||
|
||
func processModel(model interface{}, metricSet *metric.Set) { | ||
modelValue := reflect.ValueOf(model) | ||
if modelValue.Kind() == reflect.Ptr { | ||
modelValue = modelValue.Elem() | ||
} | ||
if !modelValue.IsValid() || modelValue.Kind() != reflect.Struct { | ||
return | ||
} | ||
|
||
modelType := reflect.TypeOf(model) | ||
|
||
for i := 0; i < modelValue.NumField(); i++ { | ||
field := modelValue.Field(i) | ||
fieldType := modelType.Field(i) | ||
metricName := fieldType.Tag.Get("metric_name") | ||
sourceType := fieldType.Tag.Get("source_type") | ||
ingestData := fieldType.Tag.Get("ingest_data") | ||
|
||
if ingestData == "false" { | ||
continue | ||
} | ||
|
||
if field.Kind() == reflect.Ptr && !field.IsNil() { | ||
SetMetric(metricSet, metricName, field.Elem().Interface(), sourceType) | ||
} else if field.Kind() != reflect.Ptr { | ||
SetMetric(metricSet, metricName, field.Interface(), sourceType) | ||
} | ||
} | ||
} | ||
|
||
func publishMetrics(pgIntegration *integration.Integration, instanceEntity **integration.Entity, args args.ArgumentList) error { | ||
if err := pgIntegration.Publish(); err != nil { | ||
return err | ||
} | ||
var err error | ||
*instanceEntity, err = pgIntegration.Entity(fmt.Sprintf("%s:%s", args.Hostname, args.Port), "pg-instance") | ||
return err | ||
} | ||
|
||
func GenerateRandomIntegerString(queryID string) *string { | ||
randomInt, err := rand.Int(rand.Reader, big.NewInt(randomIntRange)) | ||
if err != nil { | ||
return nil | ||
} | ||
currentTime := time.Now().Format("20060102150405") | ||
result := fmt.Sprintf("%s-%d-%s", queryID, randomInt.Int64(), currentTime) | ||
return &result | ||
} |
87 changes: 87 additions & 0 deletions
87
src/query-performance-monitoring/common-utils/query-fetch-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,87 @@ | ||
package commonutils | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/newrelic/infra-integrations-sdk/v3/log" | ||
performancedbconnection "github.com/newrelic/nri-postgresql/src/connection" | ||
"github.com/newrelic/nri-postgresql/src/query-performance-monitoring/queries" | ||
) | ||
|
||
func FetchVersion(conn *performancedbconnection.PGSQLConnection) (int, error) { | ||
var versionStr string | ||
rows, err := conn.Queryx("SELECT version()") | ||
if err != nil { | ||
log.Error("Error executing query: %v", err) | ||
return 0, err | ||
} | ||
defer rows.Close() | ||
|
||
if rows.Next() { | ||
if err := rows.Scan(&versionStr); err != nil { | ||
log.Error("Error scanning version: %v", err) | ||
return 0, err | ||
} | ||
} | ||
re := regexp.MustCompile(`PostgreSQL (\d+)\.`) | ||
matches := re.FindStringSubmatch(versionStr) | ||
if len(matches) < 2 { | ||
log.Error("Unable to parse PostgreSQL version from string: %s", versionStr) | ||
return 0, fmt.Errorf("unable to parse PostgreSQL version from string: %s", versionStr) | ||
Check failure on line 32 in src/query-performance-monitoring/common-utils/query-fetch-helpers.go
|
||
} | ||
|
||
version, err := strconv.Atoi(matches[1]) | ||
log.Debug("version", version) | ||
if err != nil { | ||
log.Error("Error converting version to integer: %v", err) | ||
return 0, err | ||
} | ||
return version, nil | ||
} | ||
|
||
func FetchVersionSpecificSlowQueries(conn *performancedbconnection.PGSQLConnection) (string, error) { | ||
version, err := FetchVersion(conn) | ||
if err != nil { | ||
return "", err | ||
} | ||
switch { | ||
case version == 12: | ||
return queries.SlowQueriesForV12, nil | ||
case version >= 13: | ||
return queries.SlowQueriesForV13AndAbove, nil | ||
default: | ||
return "", fmt.Errorf("unsupported PostgreSQL version %d", version) | ||
Check failure on line 55 in src/query-performance-monitoring/common-utils/query-fetch-helpers.go
|
||
} | ||
} | ||
|
||
func FetchVersionSpecificBlockingQueries(conn *performancedbconnection.PGSQLConnection) (string, error) { | ||
version, err := FetchVersion(conn) | ||
if err != nil { | ||
return "", err | ||
} | ||
switch { | ||
case version == 12, version == 13: | ||
return queries.BlockingQueriesForV12AndV13, nil | ||
case version >= 14: | ||
return queries.BlockingQueriesForV14AndAbove, nil | ||
default: | ||
return "", fmt.Errorf("unsupported PostgreSQL version: %d", version) | ||
Check failure on line 70 in src/query-performance-monitoring/common-utils/query-fetch-helpers.go
|
||
} | ||
} | ||
|
||
func FetchVersionSpecificIndividualQueries(conn *performancedbconnection.PGSQLConnection) (string, error) { | ||
version, err := FetchVersion(conn) | ||
if err != nil { | ||
return "", err | ||
} | ||
switch { | ||
case version == 12: | ||
return queries.IndividualQuerySearchV12, nil | ||
case version >= 13: | ||
return queries.IndividualQuerySearchV13AndAbove, nil | ||
default: | ||
return "", fmt.Errorf("unsupported PostgreSQL version %d", version) | ||
Check failure on line 85 in src/query-performance-monitoring/common-utils/query-fetch-helpers.go
|
||
} | ||
} |
Oops, something went wrong.