Skip to content
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

Add ability to set sql file as query source for postgresql_extensible input #6361

Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion plugins/inputs/postgresql_extensible/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ The example below has two queries are specified, with the following parameters:
# Be careful that if the withdbname is set to false you don't have to define
# the where clause (aka with the dbname)
#
# The script option can be used to speciafy the .sql file path,
glinton marked this conversation as resolved.
Show resolved Hide resolved
# in case if script and sqlquery options specified at same time, sqlquery will be used
#
# the tagvalue field is used to define custom tags (separated by comas).
# the query is expected to return columns which match the names of the
# defined tags. The values in these columns must be of a string-type,
Expand All @@ -61,7 +64,7 @@ The example below has two queries are specified, with the following parameters:
withdbname=false
tagvalue=""
[[inputs.postgresql_extensible.query]]
sqlquery="SELECT * FROM pg_stat_bgwriter"
script="your_sql-filepath.sql"
version=901
withdbname=false
tagvalue=""
Expand Down
29 changes: 28 additions & 1 deletion plugins/inputs/postgresql_extensible/postgresql_extensible.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package postgresql_extensible
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

// register in driver.
Expand All @@ -25,6 +27,7 @@ type Postgresql struct {

type query []struct {
Sqlquery string
Script string
Version int
Withdbname bool
Tagvalue string
Expand Down Expand Up @@ -75,7 +78,10 @@ var sampleConfig = `
## field is used to define custom tags (separated by commas)
## The optional "measurement" value can be used to override the default
## output measurement name ("postgresql").
#
##
## The script option can be used to speciafy the .sql file path,
## in case if script and sqlquery options specified at same time, sqlquery will be used
##
## Structure :
## [[inputs.postgresql_extensible.query]]
## sqlquery string
Expand Down Expand Up @@ -108,6 +114,20 @@ func (p *Postgresql) IgnoredColumns() map[string]bool {
return ignoredColumns
}

func ReadQueryFromFile(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()

qyery, err := ioutil.ReadAll(file)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/qyery/query/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling still needs fixed.

if err != nil {
return "", err
}
return string(qyery), err
}

func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
var (
err error
Expand All @@ -130,7 +150,14 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
// Query is not run if Database version does not match the query version.
for i := range p.Query {
sql_query = p.Query[i].Sqlquery
if sql_query == "" {
sql_query, err = ReadQueryFromFile(p.Query[i].Script)
glinton marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}
}
tag_value = p.Query[i].Tagvalue

if p.Query[i].Measurement != "" {
meas_name = p.Query[i].Measurement
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestPostgresqlFieldOutput(t *testing.T) {
}

acc := queryRunner(t, query{{
Sqlquery: "select * from pg_stat_database",
Script: "select * from pg_stat_database",
glinton marked this conversation as resolved.
Show resolved Hide resolved
Version: 901,
Withdbname: false,
Tagvalue: "",
Expand Down Expand Up @@ -201,6 +201,30 @@ func TestPostgresqlFieldOutput(t *testing.T) {
}
}

func TestPostgresqlSqlScript(t *testing.T) {
q := query{{
Script: "testdata/test.sql",
Version: 901,
Withdbname: false,
Tagvalue: "",
}}
p := &Postgresql{
Service: postgresql.Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
),
IsPgBouncer: false,
},
Databases: []string{"postgres"},
Query: q,
}
var acc testutil.Accumulator
p.Start(&acc)

require.NoError(t, acc.GatherError(p.Gather))
}

func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/postgresql_extensible/testdata/test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select * from pg_stat_database