feat(inputs.postgresql_extensible): Introduce max_version for query (#13620)

This commit is contained in:
Maxim Ivanov 2023-07-21 08:56:49 +00:00 committed by GitHub
parent ab99bf32b6
commit 22100d4914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 16 deletions

View File

@ -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=""
```

View File

@ -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)
}
}

View File

@ -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: "",
}}

View File

@ -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=""