Skip to content

Commit

Permalink
Add --strip-settings-suffix flag for prometheus-community#619
Browse files Browse the repository at this point in the history
Adds --strip-settings-suffix flag to make the fixes for AWS Aurora in prometheus-community#619 opt in for users. It should be safe for all users, but this code should also be unnecessary if AWS fixes their bug.

Signed-off-by: Joe Adams <github@joeadams.io>
  • Loading branch information
sysadmind committed Mar 23, 2022
1 parent 7e02b9b commit 7861363
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ This will build the docker image as `prometheuscommunity/postgres_exporter:${bra
* `include-databases`
A list of databases to only include when autoDiscoverDatabases is enabled.

* `strip-settings-suffix`
Whether to strip unit siffixes (i.e. KB) from pg_settings metrics.

* `log.level`
Set logging level: one of `debug`, `info`, `warn`, `error`.

Expand Down
1 change: 1 addition & 0 deletions cmd/postgres_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
excludeDatabases = kingpin.Flag("exclude-databases", "A list of databases to remove when autoDiscoverDatabases is enabled").Default("").Envar("PG_EXPORTER_EXCLUDE_DATABASES").String()
includeDatabases = kingpin.Flag("include-databases", "A list of databases to include when autoDiscoverDatabases is enabled").Default("").Envar("PG_EXPORTER_INCLUDE_DATABASES").String()
metricPrefix = kingpin.Flag("metric-prefix", "A metric prefix can be used to have non-default (not \"pg\") prefixes for each of the metrics").Default("pg").Envar("PG_EXPORTER_METRIC_PREFIX").String()
stripSettingsSuffix = kingpin.Flag("strip-settings-suffix", "Whether to strip unit siffixes (i.e. KB) from pg_settings metrics.").Default("false").Envar("PG_EXPORTER_STRIP_METRICS_SUFFIX").Bool()
logger = log.NewNopLogger()
)

Expand Down
7 changes: 5 additions & 2 deletions cmd/postgres_exporter/pg_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func querySettings(ch chan<- prometheus.Metric, server *Server) error {
return fmt.Errorf("Error retrieving rows on %q: %s %v", server, namespace, err)
}

if *stripSettingsSuffix {
s.stripUnitSuffix()
}

ch <- s.metric(server.labels)
}

Expand Down Expand Up @@ -103,7 +107,7 @@ func (s *pgSetting) metric(labels prometheus.Labels) prometheus.Metric {
// 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() {
func (s *pgSetting) stripUnitSuffix() {
for _, unit := range settingUnits {
if strings.HasSuffix(s.setting, unit) {
endPos := len(s.setting) - len(unit) - 1
Expand All @@ -116,7 +120,6 @@ func (s *pgSetting) sanitizeValue() {
// 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 {
Expand Down
30 changes: 30 additions & 0 deletions cmd/postgres_exporter/pg_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package main

import (
"testing"

"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
. "gopkg.in/check.v1"
Expand Down Expand Up @@ -268,3 +270,31 @@ type fixture struct {
d string
v float64
}

func Test_pgSetting_sanitizeValue(t *testing.T) {
tests := []struct {
name string
setting *pgSetting
want string
}{
{
name: "RDS Shared Buffers",
setting: &pgSetting{
name: "shared_buffers",
setting: "88413056kB",
unit: "8kB",
shortDesc: "Sets the number of shared memory buffers used by the server.",
vartype: "integer",
},
want: "88413056",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setting.stripUnitSuffix()
if got := tt.setting.setting; got != tt.want {
t.Errorf("sanitizeValue() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 7861363

Please sign in to comment.