From 6d2cae6fa8a36869d121c8128d06cd118239cd4a Mon Sep 17 00:00:00 2001 From: Anthony Regeda Date: Thu, 20 Dec 2018 12:54:24 +0300 Subject: [PATCH] fix-param-env-key fix spelling of env parameter --- cmd/postgres_exporter/postgres_exporter.go | 7 +- .../postgres_exporter_test.go | 75 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/cmd/postgres_exporter/postgres_exporter.go b/cmd/postgres_exporter/postgres_exporter.go index b9dc9eee..93f9192e 100644 --- a/cmd/postgres_exporter/postgres_exporter.go +++ b/cmd/postgres_exporter/postgres_exporter.go @@ -39,7 +39,7 @@ var ( disableSettingsMetrics = kingpin.Flag("disable-settings-metrics", "Do not include pg_settings metrics.").Default("false").OverrideDefaultFromEnvar("PG_EXPORTER_DISABLE_SETTINGS_METRICS").Bool() queriesPath = kingpin.Flag("extend.query-path", "Path to custom queries to run.").Default("").OverrideDefaultFromEnvar("PG_EXPORTER_EXTEND_QUERY_PATH").String() onlyDumpMaps = kingpin.Flag("dumpmaps", "Do not run, simply dump the maps.").Bool() - constantLabelsList = kingpin.Flag("constantLabels", "A list of label=value separated by comma(,).").Default("").OverrideDefaultFromEnvar("PG_EXPORTER_CONTANT_LABELS").String() + constantLabelsList = kingpin.Flag("constantLabels", "A list of label=value separated by comma(,).").Default("").OverrideDefaultFromEnvar("PG_EXPORTER_CONSTANT_LABELS").String() ) // Metric name parts. @@ -927,6 +927,11 @@ func WithConstantLabels(s string) ExporterOpt { func parseConstLabels(s string) prometheus.Labels { labels := make(prometheus.Labels) + s = strings.TrimSpace(s) + if len(s) == 0 { + return labels + } + parts := strings.Split(s, ",") for _, p := range parts { keyValue := strings.Split(strings.TrimSpace(p), "=") diff --git a/cmd/postgres_exporter/postgres_exporter_test.go b/cmd/postgres_exporter/postgres_exporter_test.go index 598c8a62..c51d89a1 100644 --- a/cmd/postgres_exporter/postgres_exporter_test.go +++ b/cmd/postgres_exporter/postgres_exporter_test.go @@ -3,6 +3,7 @@ package main import ( + "reflect" "testing" . "gopkg.in/check.v1" @@ -184,6 +185,80 @@ func (s *FunctionalSuite) TestPostgresVersionParsing(c *C) { } } +func (s *FunctionalSuite) TestParseFingerprint(c *C) { + cases := []struct { + url string + fingerprint string + err string + }{ + { + url: "postgresql://userDsn:passwordDsn@localhost:55432/?sslmode=disabled", + fingerprint: "localhost:55432", + }, + { + url: "port=1234", + fingerprint: "localhost:1234", + }, + { + url: "host=example", + fingerprint: "example:5432", + }, + { + url: "xyz", + err: "malformed dsn \"xyz\"", + }, + } + + for _, cs := range cases { + f, err := parseFingerprint(cs.url) + if cs.err == "" { + c.Assert(err, IsNil) + } else { + c.Assert(err, NotNil) + c.Assert(err.Error(), Equals, cs.err) + } + c.Assert(f, Equals, cs.fingerprint) + } +} + +func (s *FunctionalSuite) TestParseConstLabels(c *C) { + cases := []struct { + s string + labels prometheus.Labels + }{ + { + s: "a=b", + labels: prometheus.Labels{ + "a": "b", + }, + }, + { + s: "", + labels: prometheus.Labels{}, + }, + { + s: "a=b, c=d", + labels: prometheus.Labels{ + "a": "b", + "c": "d", + }, + }, + { + s: "a=b, xyz", + labels: prometheus.Labels{ + "a": "b", + }, + }, + } + + for _, cs := range cases { + labels := parseConstLabels(cs.s) + if !reflect.DeepEqual(labels, cs.labels) { + c.Fatalf("labels not equal (%v -> %v)", labels, cs.labels) + } + } +} + func UnsetEnvironment(c *C, d string) { err := os.Unsetenv(d) c.Assert(err, IsNil)