Skip to content

Commit 93bb0e6

Browse files
Merge pull request #14 from form3tech-oss/ap-fix-fingerprintparse
fix: dsn fingerprint parsing
2 parents 0b09fbb + f9fbced commit 93bb0e6

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

cmd/postgres_exporter/postgres_exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ type Exporter struct {
415415
userQueriesPath string
416416
constantLabels prometheus.Labels
417417
duration prometheus.Gauge
418-
errors prometheus.Gauge
418+
errors prometheus.Gauge
419419
psqlUp prometheus.Gauge
420420
userQueriesError *prometheus.GaugeVec
421421
totalScrapes prometheus.Counter

cmd/postgres_exporter/postgres_exporter_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ func (s *FunctionalSuite) TestParseFingerprint(c *C) {
229229
url: "postgresql://userDsn:passwordDsn%3D@localhost:55432/?sslmode=disabled",
230230
fingerprint: "localhost:55432",
231231
},
232+
{
233+
url: "postgresql://userDsn:passwordDsn%3D@localhost:55432/foo?sslmode=disabled&options=-c%20statement_timeout%3D3min%20-c%20statement_timeout%3D1min",
234+
fingerprint: "localhost:55432",
235+
},
232236
{
233237
url: "port=1234",
234238
fingerprint: "localhost:1234",

cmd/postgres_exporter/util.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"fmt"
1818
"math"
1919
"net/url"
20+
"regexp"
2021
"strconv"
2122
"strings"
2223
"time"
@@ -177,7 +178,12 @@ func parseFingerprint(url string) (string, error) {
177178
dsn = url
178179
}
179180

180-
pairs := strings.Split(dsn, " ")
181+
re, _ := regexp.Compile(`[a-z_]+=(?:(?:'[^']+')|(?:[^\s]+))`)
182+
pairs := re.FindAllString(dsn, -1)
183+
if pairs == nil {
184+
return "", fmt.Errorf("malformed dsn %q", dsn)
185+
}
186+
181187
kv := make(map[string]string, len(pairs))
182188
for _, pair := range pairs {
183189
splitted := strings.SplitN(pair, "=", 2)

0 commit comments

Comments
 (0)