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

fix(inputs.postgresql_extensible): Use same timestamp for each gather #15401

Merged
merged 1 commit into from
May 31, 2024
Merged
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
15 changes: 8 additions & 7 deletions plugins/inputs/postgresql_extensible/postgresql_extensible.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,21 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
dbVersion = 0
}

// set default timestamp to Now and use for all generated metrics during
// the same Gather call
timestamp := time.Now()

// We loop in order to process each query
// Query is not run if Database version does not match the query version.
for _, q := range p.Query {
if q.MinVersion <= dbVersion && (q.MaxVersion == 0 || q.MaxVersion > dbVersion) {
acc.AddError(p.gatherMetricsFromQuery(acc, q))
acc.AddError(p.gatherMetricsFromQuery(acc, q, timestamp))
}
}
return nil
}

func (p *Postgresql) gatherMetricsFromQuery(acc telegraf.Accumulator, q query) error {
func (p *Postgresql) gatherMetricsFromQuery(acc telegraf.Accumulator, q query, timestamp time.Time) error {
rows, err := p.service.DB.Query(q.Sqlquery)
if err != nil {
return err
Expand All @@ -139,7 +143,7 @@ func (p *Postgresql) gatherMetricsFromQuery(acc telegraf.Accumulator, q query) e
}

for rows.Next() {
if err := p.accRow(acc, rows, columns, q); err != nil {
if err := p.accRow(acc, rows, columns, q, timestamp); err != nil {
return err
}
}
Expand All @@ -150,7 +154,7 @@ type scanner interface {
Scan(dest ...interface{}) error
}

func (p *Postgresql) accRow(acc telegraf.Accumulator, row scanner, columns []string, q query) error {
func (p *Postgresql) accRow(acc telegraf.Accumulator, row scanner, columns []string, q query, timestamp time.Time) error {
// this is where we'll store the column name with its *interface{}
columnMap := make(map[string]*interface{})

Expand Down Expand Up @@ -188,9 +192,6 @@ func (p *Postgresql) accRow(acc telegraf.Accumulator, row scanner, columns []str
"db": dbname.String(),
}

// set default timestamp to Now
timestamp := time.Now()

fields := make(map[string]interface{})
for col, val := range columnMap {
p.Log.Debugf("Column: %s = %T: %v\n", col, *val, *val)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func TestAccRow(t *testing.T) {
}
for _, tt := range tests {
q := query{Measurement: "pgTEST", additionalTags: make(map[string]bool)}
require.NoError(t, p.accRow(&acc, tt.fields, columns, q))
require.NoError(t, p.accRow(&acc, tt.fields, columns, q, time.Now()))
require.Len(t, acc.Metrics, 1)
metric := acc.Metrics[0]
require.Equal(t, tt.dbName, metric.Tags["db"])
Expand Down
Loading