Skip to content

Commit

Permalink
Fix practical issues with AWS instance role handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfittl committed Apr 8, 2016
1 parent ae1587b commit 5701773
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
6 changes: 3 additions & 3 deletions dbstats/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func GetActivity(logger *util.Logger, db *sql.DB, postgresVersion PostgresVersio
return nil, err
}

if !query.IsZero() {
normalizedQuery, err := pg_query.Normalize(*query.Ptr())
if query.Valid && query.String != "<insufficient privilege>" {
normalizedQuery, err := pg_query.Normalize(query.String)
if err != nil {
logger.PrintVerbose("Failed to normalize query: %s", err)
logger.PrintVerbose("Failed to normalize query, excluding from statistics: %s", err)
} else {
row.NormalizedQuery = null.StringFrom(normalizedQuery)
}
Expand Down
10 changes: 1 addition & 9 deletions logs/amazon_rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/pganalyze/collector/config"
"github.com/pganalyze/collector/explain"
Expand All @@ -28,13 +26,7 @@ func getFromAmazonRds(config config.DatabaseConfig) (result []Line, explains []e
// Get interesting files (last written to in the last 10 minutes)
// Remember markers for each file

creds := credentials.NewStaticCredentials(config.AwsAccessKeyID, config.AwsSecretAccessKey, "")

sess := session.New(&aws.Config{Credentials: creds, Region: aws.String(config.AwsRegion)})
//sess.Handlers.Send.PushFront(func(r *request.Request) {
// Log every request made and its payload
// fmt.Printf("Request: %s/%s, Payload: %s\n", r.ClientInfo.ServiceName, r.Operation, r.Params)
//})
sess := util.GetAwsSession(config)

rdsSvc := rds.New(sess)

Expand Down
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func collectStatistics(db database, collectionOpts collectionOpts, logger *util.
"data": {compressedJSON.String()},
"data_compressor": {"zlib"},
"api_key": {db.config.APIKey},
"submitter": {"pganalyze-collector 0.9.0rc5"},
"submitter": {"pganalyze-collector 0.9.0rc6"},
"no_reset": {"true"},
"query_source": {"pg_stat_statements"},
"collected_at": {fmt.Sprintf("%d", time.Now().Unix())},
Expand Down Expand Up @@ -256,6 +256,7 @@ func collectAllDatabases(databases []database, globalCollectionOpts collectionOp
db.connection, err = establishConnection(db, logger, globalCollectionOpts)
if err != nil {
prefixedLogger.PrintError("Error: Failed to connect to database: %s", err)
return
}

newState, err := collectStatistics(db, globalCollectionOpts, prefixedLogger)
Expand Down Expand Up @@ -286,7 +287,17 @@ func validateConnectionCount(connection *sql.DB, logger *util.Logger, globalColl

func connectToDb(config config.DatabaseConfig, logger *util.Logger, globalCollectionOpts collectionOpts) (*sql.DB, error) {
connectString := config.GetPqOpenString()
connectString += " application_name=" + globalCollectionOpts.collectorApplicationName

if strings.HasPrefix(connectString, "postgres://") || strings.HasPrefix(connectString, "postgresql://") {
if strings.Contains(connectString, "?") {
connectString += "&"
} else {
connectString += "?"
}
} else {
connectString += " "
}
connectString += "application_name=" + globalCollectionOpts.collectorApplicationName

// logger.PrintVerbose("sql.Open(\"postgres\", \"%s\")", connectString)

Expand Down
19 changes: 2 additions & 17 deletions systemstats/amazon_rds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import (
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
//"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"

"github.com/aws/aws-sdk-go/service/rds"

"github.com/pganalyze/collector/config"
Expand Down Expand Up @@ -66,19 +63,7 @@ type AmazonRdsInfo struct {

// GetFromAmazonRds - Gets system information about an Amazon RDS instance
func getFromAmazonRds(config config.DatabaseConfig) (system *SystemSnapshot) {
var creds *credentials.Credentials

if config.AwsAccessKeyID != "" {
creds = credentials.NewStaticCredentials(config.AwsAccessKeyID, config.AwsSecretAccessKey, "")
} else {
creds = credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{})
}

sess := session.New(&aws.Config{Credentials: creds, Region: aws.String(config.AwsRegion)})
//sess.Handlers.Send.PushFront(func(r *request.Request) {
// Log every request made and its payload
// fmt.Printf("Request: %s/%s, Payload: %s\n", r.ClientInfo.ServiceName, r.Operation, r.Params)
//})
sess := util.GetAwsSession(config)

rdsSvc := rds.New(sess)

Expand Down
29 changes: 29 additions & 0 deletions util/amazon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package util

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/pganalyze/collector/config"
)

func GetAwsSession(config config.DatabaseConfig) *session.Session {
var creds *credentials.Credentials

if config.AwsAccessKeyID != "" {
creds = credentials.NewStaticCredentials(config.AwsAccessKeyID, config.AwsSecretAccessKey, "")
} else {
creds = credentials.NewCredentials(&ec2rolecreds.EC2RoleProvider{
Client: ec2metadata.New(session.New()),
})
}

return session.New(&aws.Config{Credentials: creds, Region: aws.String(config.AwsRegion)})
}

//sess.Handlers.Send.PushFront(func(r *request.Request) {
// Log every request made and its payload
// fmt.Printf("Request: %s/%s, Payload: %s\n", r.ClientInfo.ServiceName, r.Operation, r.Params)
//})

0 comments on commit 5701773

Please sign in to comment.