From be308594171257fdc86d238e2697d1ce39ed58e0 Mon Sep 17 00:00:00 2001 From: Angus Dippenaar Date: Thu, 3 Mar 2022 15:31:53 +0100 Subject: [PATCH] Remove unit from setting because of Aurora Signed-off-by: Angus Dippenaar --- cmd/postgres_exporter/pg_setting.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/postgres_exporter/pg_setting.go b/cmd/postgres_exporter/pg_setting.go index b361ac48..bfe4c6c5 100644 --- a/cmd/postgres_exporter/pg_setting.go +++ b/cmd/postgres_exporter/pg_setting.go @@ -23,6 +23,13 @@ import ( "github.com/prometheus/client_golang/prometheus" ) +var ( + settingUnits = []string{ + "ms", "s", "min", "h", "d", + "B", "kB", "MB", "GB", "TB", + } +) + // Query the pg_settings view containing runtime variables func querySettings(ch chan<- prometheus.Metric, server *Server) error { level.Debug(logger).Log("msg", "Querying pg_setting view", "server", server) @@ -93,9 +100,24 @@ func (s *pgSetting) metric(labels prometheus.Labels) prometheus.Metric { return prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, val) } +// Removes units from any of the setting values. +// This is mostly because of a irregularity regarding AWS RDS Aurora +// https://github.com/prometheus-community/postgres_exporter/issues/619 +func (s *pgSetting) sanitizeValue() { + for _, unit := range settingUnits { + if strings.HasSuffix(s.setting, unit) { + endPos := len(s.setting) - len(unit) - 1 + s.setting = s.setting[:endPos] + return + } + } +} + // TODO: fix linter override // nolint: nakedret func (s *pgSetting) normaliseUnit() (val float64, unit string, err error) { + s.sanitizeValue() + val, err = strconv.ParseFloat(s.setting, 64) if err != nil { return val, unit, fmt.Errorf("Error converting setting %q value %q to float: %s", s.name, s.setting, err)