diff --git a/plugins/inputs/postgresql_extensible/README.md b/plugins/inputs/postgresql_extensible/README.md index 9da2b8984..d79b1bbd3 100644 --- a/plugins/inputs/postgresql_extensible/README.md +++ b/plugins/inputs/postgresql_extensible/README.md @@ -68,22 +68,33 @@ to use them. # default, all rows inserted with current time. By setting a timestamp column, # the row will be inserted with that column's value. # + # The min_version field specifies minimal database version this query + # will run on. + # + # The max_version field when set specifies maximal database version + # this query will NOT run on. + # + # Database version in `minversion` and `maxversion` is represented as + # a single integer without last component, for example: + # 9.6.2 -> 906 + # 15.2 -> 1500 + # # Structure : # [[inputs.postgresql_extensible.query]] # sqlquery string - # version string + # min_version int + # max_version int # withdbname boolean # tagvalue string (coma separated) # timestamp string [[inputs.postgresql_extensible.query]] sqlquery="SELECT * FROM pg_stat_database where datname" - version=901 - withdbname=false + min_version=901 tagvalue="" [[inputs.postgresql_extensible.query]] script="your_sql-filepath.sql" - version=901 - withdbname=false + min_version=901 + max_version=1300 tagvalue="" ``` diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible.go b/plugins/inputs/postgresql_extensible/postgresql_extensible.go index 32fee3fcc..8d65e6338 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible.go @@ -37,7 +37,9 @@ type Postgresql struct { type query []struct { Sqlquery string Script string - Version int + Version int `deprecated:"1.28.0;use minVersion to specify minimal DB version this query supports"` + MinVersion int `toml:"min_version"` + MaxVersion int `toml:"max_version"` Withdbname bool `deprecated:"1.22.4;use the sqlquery option to specify database to use"` Tagvalue string Measurement string @@ -59,6 +61,9 @@ func (p *Postgresql) Init() error { return err } } + if p.Query[i].MinVersion == 0 { + p.Query[i].MinVersion = p.Query[i].Version + } } p.Service.IsPgBouncer = !p.PreparedStatements return nil @@ -120,7 +125,9 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error { } sqlQuery += queryAddon - if p.Query[i].Version <= dbVersion { + maxVer := p.Query[i].MaxVersion + + if p.Query[i].MinVersion <= dbVersion && (maxVer == 0 || maxVer > dbVersion) { p.gatherMetricsFromQuery(acc, sqlQuery, p.Query[i].Tagvalue, p.Query[i].Timestamp, measName) } } diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go b/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go index be4d87b61..11f69cb9a 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go @@ -63,7 +63,7 @@ func TestPostgresqlGeneratesMetricsIntegration(t *testing.T) { acc := queryRunner(t, query{{ Sqlquery: "select * from pg_stat_database", - Version: 901, + MinVersion: 901, Withdbname: false, Tagvalue: "", }}) @@ -163,7 +163,7 @@ func TestPostgresqlQueryOutputTestsIntegration(t *testing.T) { for q, assertions := range examples { acc := queryRunner(t, query{{ Sqlquery: q, - Version: 901, + MinVersion: 901, Withdbname: false, Tagvalue: "", Timestamp: "ts", @@ -180,7 +180,7 @@ func TestPostgresqlFieldOutputIntegration(t *testing.T) { acc := queryRunner(t, query{{ Sqlquery: "select * from pg_stat_database", - Version: 901, + MinVersion: 901, Withdbname: false, Tagvalue: "", }}) @@ -238,7 +238,7 @@ func TestPostgresqlFieldOutputIntegration(t *testing.T) { func TestPostgresqlSqlScript(t *testing.T) { q := query{{ Script: "testdata/test.sql", - Version: 901, + MinVersion: 901, Withdbname: false, Tagvalue: "", }} diff --git a/plugins/inputs/postgresql_extensible/sample.conf b/plugins/inputs/postgresql_extensible/sample.conf index 46a2e6d50..e7d86b57f 100644 --- a/plugins/inputs/postgresql_extensible/sample.conf +++ b/plugins/inputs/postgresql_extensible/sample.conf @@ -35,20 +35,31 @@ # default, all rows inserted with current time. By setting a timestamp column, # the row will be inserted with that column's value. # + # The min_version field specifies minimal database version this query + # will run on. + # + # The max_version field when set specifies maximal database version + # this query will NOT run on. + # + # Database version in `minversion` and `maxversion` is represented as + # a single integer without last component, for example: + # 9.6.2 -> 906 + # 15.2 -> 1500 + # # Structure : # [[inputs.postgresql_extensible.query]] # sqlquery string - # version string + # min_version int + # max_version int # withdbname boolean # tagvalue string (coma separated) # timestamp string [[inputs.postgresql_extensible.query]] sqlquery="SELECT * FROM pg_stat_database where datname" - version=901 - withdbname=false + min_version=901 tagvalue="" [[inputs.postgresql_extensible.query]] script="your_sql-filepath.sql" - version=901 - withdbname=false + min_version=901 + max_version=1300 tagvalue=""