mirror of
https://github.com/prometheus-community/postgres_exporter
synced 2025-04-19 13:35:32 +00:00
55 lines
997 B
Go
55 lines
997 B
Go
package servers
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/lib/pq"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
func parseFingerprint(url string) (string, error) {
|
|
dsn, err := pq.ParseURL(url)
|
|
if err != nil {
|
|
dsn = url
|
|
}
|
|
|
|
pairs := strings.Split(dsn, " ")
|
|
kv := make(map[string]string, len(pairs))
|
|
for _, pair := range pairs {
|
|
splitted := strings.SplitN(pair, "=", 2)
|
|
if len(splitted) != 2 {
|
|
return "", fmt.Errorf("malformed dsn %q", dsn)
|
|
}
|
|
kv[splitted[0]] = splitted[1]
|
|
}
|
|
|
|
var fingerprint string
|
|
|
|
if host, ok := kv["host"]; ok {
|
|
fingerprint += host
|
|
} else {
|
|
fingerprint += "localhost"
|
|
}
|
|
|
|
if port, ok := kv["port"]; ok {
|
|
fingerprint += ":" + port
|
|
} else {
|
|
fingerprint += ":5432"
|
|
}
|
|
|
|
return fingerprint, nil
|
|
}
|
|
|
|
func loggableDSN(dsn string) string {
|
|
pDSN, err := url.Parse(dsn)
|
|
if err != nil {
|
|
return "could not parse DATA_SOURCE_NAME"
|
|
}
|
|
// Blank user info if not nil
|
|
if pDSN.User != nil {
|
|
pDSN.User = url.UserPassword(pDSN.User.Username(), "PASSWORD_REMOVED")
|
|
}
|
|
|
|
return pDSN.String()
|
|
}
|