Skip to content

Add --strip-settings-suffix flag for #619 #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}