Skip to content

Commit

Permalink
Merge pull request #40 from abhinav1602/queryAnalysis-dataFixes
Browse files Browse the repository at this point in the history
rename package as per standard
  • Loading branch information
tanushangural authored Jan 8, 2025
2 parents bad0f47 + ebf23ea commit ddbcfdb
Show file tree
Hide file tree
Showing 31 changed files with 49 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/on_push_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main
- master
- renovate/**
- epic_queryAnalysis
- epic_queryanalysis
pull_request:

jobs:
Expand Down
4 changes: 2 additions & 2 deletions src/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/newrelic/nri-mssql/src/instance"
"github.com/newrelic/nri-mssql/src/inventory"
"github.com/newrelic/nri-mssql/src/metrics"
"github.com/newrelic/nri-mssql/src/queryAnalysis"
"github.com/newrelic/nri-mssql/src/queryanalysis"
)

const (
Expand Down Expand Up @@ -97,7 +97,7 @@ func main() {
i.Clear()

if args.EnableQueryPerformance {
queryAnalysis.QueryPerformanceMain(i, args)
queryanalysis.QueryPerformanceMain(i, args)
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package config

import "github.com/newrelic/nri-mssql/src/queryAnalysis/models"
import "github.com/newrelic/nri-mssql/src/queryanalysis/models"

var Queries = []models.QueryDetailsDto{
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func (sc SQLConnection) Queryx(query string) (*sqlx.Rows, error) {
// CreateConnectionURL tags in args and creates the connection string.
// All args should be validated before calling this.
func CreateConnectionURL(args *args.ArgumentList) string {

connectionString := ""

connectionURL := &url.URL{
Expand Down Expand Up @@ -83,7 +82,6 @@ func CreateConnectionURL(args *args.ArgumentList) string {
} else {
log.Warn("Could not successfully parse ExtraConnectionURLArgs.", err.Error())
}

}

if args.EnableSSL {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)

var ErrMockCloseFailure = errors.New("mock close failure")

func Test_SQLConnection_Close(t *testing.T) {
conn, mock := CreateMockSQL(t)

mock.ExpectClose().WillReturnError(errors.New("error"))
mock.ExpectClose().WillReturnError(ErrMockCloseFailure)
conn.Close()

if err := mock.ExpectationsWereMet(); err != nil {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"

"github.com/newrelic/infra-integrations-sdk/v3/integration"
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
)

// instanceNameQuery gets the instance name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/newrelic/infra-integrations-sdk/v3/integration"
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
"github.com/stretchr/testify/assert"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package queryAnalysis
package queryanalysis

import (
"github.com/newrelic/infra-integrations-sdk/v3/integration"
"github.com/newrelic/infra-integrations-sdk/v3/log"
"github.com/newrelic/nri-mssql/src/args"
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryAnalysis/retrymechanism"
"github.com/newrelic/nri-mssql/src/queryAnalysis/utils"
"github.com/newrelic/nri-mssql/src/queryAnalysis/validation"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/retrymechanism"
"github.com/newrelic/nri-mssql/src/queryanalysis/utils"
"github.com/newrelic/nri-mssql/src/queryanalysis/validation"
)

// queryPerformanceMain runs all types of analyses
// queryPerformanceMain runs all types of analyzes
func QueryPerformanceMain(integration *integration.Integration, arguments args.ArgumentList) {

// Create a new connection
sqlConnection, err := connection.NewConnection(&arguments)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (
"github.com/newrelic/infra-integrations-sdk/v3/log"

"github.com/newrelic/nri-mssql/src/args"
"github.com/newrelic/nri-mssql/src/queryAnalysis/config"
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryAnalysis/instance"
"github.com/newrelic/nri-mssql/src/queryAnalysis/models"
"github.com/newrelic/nri-mssql/src/queryanalysis/config"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/instance"
"github.com/newrelic/nri-mssql/src/queryanalysis/models"
)

var ErrUnknownQueryType = errors.New("unknown query type")
var ErrCreatingInstanceEntity = errors.New("error creating instance entity")

func LoadQueries(arguments args.ArgumentList) ([]models.QueryDetailsDto, error) {
var queries []models.QueryDetailsDto = config.Queries
queries := config.Queries

for i := range queries {
switch queries[i].Type {
Expand Down Expand Up @@ -107,9 +107,8 @@ func BindQueryResults(arguments args.ArgumentList,
func GenerateAndInjestExecutionPlan(arguments args.ArgumentList,
integration *integration.Integration,
sqlConnection *connection.SQLConnection,
queryId models.HexString) {

hexQueryID := string(queryId)
queryID models.HexString) {
hexQueryID := string(queryID)
executionPlanQuery := fmt.Sprintf(config.ExecutionPlanQueryTemplate, min(config.IndividualQueryCountMax, arguments.QueryCountThreshold),
arguments.QueryResponseTimeThreshold, hexQueryID, arguments.FetchInterval, config.TextTruncateLimit)

Expand Down Expand Up @@ -168,6 +167,19 @@ func IngestQueryMetricsInBatches(results []interface{},
return nil
}

func convertResultToMap(result interface{}) (map[string]interface{}, error) {
data, err := json.Marshal(result)
if err != nil {
return nil, fmt.Errorf("error marshaling result: %w", err)
}

var resultMap map[string]interface{}
if err := json.Unmarshal(data, &resultMap); err != nil {
return nil, fmt.Errorf("error unmarshaling to map: %w", err)
}
return resultMap, nil
}

// IngestQueryMetrics processes and ingests query metrics into the New Relic entity
func IngestQueryMetrics(results []interface{}, queryDetailsDto models.QueryDetailsDto, integration *integration.Integration, sqlConnection *connection.SQLConnection) error {

Expand All @@ -178,14 +190,10 @@ func IngestQueryMetrics(results []interface{}, queryDetailsDto models.QueryDetai

for _, result := range results {
// Convert the result into a map[string]interface{} for dynamic key-value access
var resultMap map[string]interface{}
data, err := json.Marshal(result)
if err != nil {
log.Error("error marshaling to JSON: %w", err)
}
err = json.Unmarshal(data, &resultMap)
resultMap, err := convertResultToMap(result)
if err != nil {
log.Error("error unmarshaling to map: %w", err)
log.Error("failed to convert result: %v", err)
continue
}

// Create a new metric set with the query name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"time"

"github.com/newrelic/infra-integrations-sdk/v3/data/metric"
"github.com/newrelic/nri-mssql/src/queryAnalysis/config"
"github.com/newrelic/nri-mssql/src/queryanalysis/config"
"github.com/stretchr/testify/assert"

"github.com/newrelic/infra-integrations-sdk/v3/integration"
"github.com/newrelic/nri-mssql/src/args"
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryAnalysis/models"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/models"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package validation

import (
"github.com/newrelic/infra-integrations-sdk/v3/log"
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryAnalysis/models"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/models"
)

// GetDatabaseDetails gets the details of user databases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
"github.com/stretchr/testify/assert"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/jmoiron/sqlx"
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
"github.com/stretchr/testify/assert"
"gopkg.in/DATA-DOG/go-sqlmock.v1"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package validation

import (
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
)

func checkPermissions(sqlConnection *connection.SQLConnection) (bool, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package validation

import (
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
)

func checkSQLServerLoginEnabled(sqlConnection *connection.SQLConnection) (bool, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package validation

import (
"github.com/newrelic/infra-integrations-sdk/v3/log"
"github.com/newrelic/nri-mssql/src/queryAnalysis/connection"
"github.com/newrelic/nri-mssql/src/queryAnalysis/models"
"github.com/newrelic/nri-mssql/src/queryanalysis/connection"
"github.com/newrelic/nri-mssql/src/queryanalysis/models"
)

const versionCompatibility = 90
Expand Down

0 comments on commit ddbcfdb

Please sign in to comment.