forked from newrelic/nri-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon_helpers.go
42 lines (36 loc) · 1.04 KB
/
common_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 GetDatabaseListInString(dbMap collection.DatabaseList) string {
if len(dbMap) == 0 {
return ""
}
var quotedNames = make([]string, 0)
for dbName := range dbMap {
quotedNames = append(quotedNames, fmt.Sprintf("'%s'", dbName))
}
return strings.Join(quotedNames, ",")
}
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() (string, error) {
randomInt, err := rand.Int(rand.Reader, big.NewInt(RandomIntRange))
if err != nil {
return "", ErrUnExpectedError
}
currentTime := time.Now().Format(TimeFormat)
result := fmt.Sprintf("%d-%s", randomInt.Int64(), currentTime)
return result, nil
}