From 27b98083f4480564f0b62c2bb32042773db47cb6 Mon Sep 17 00:00:00 2001 From: bhsu-ms <72472578+bhsu-ms@users.noreply.github.com> Date: Tue, 27 Jul 2021 14:14:49 -0700 Subject: [PATCH] Change the timeout from all queries to per query (#9471) --- plugins/inputs/sql/README.md | 1 + plugins/inputs/sql/sql.go | 8 +++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/plugins/inputs/sql/README.md b/plugins/inputs/sql/README.md index 9c002df18..cc8a46401 100644 --- a/plugins/inputs/sql/README.md +++ b/plugins/inputs/sql/README.md @@ -22,6 +22,7 @@ generate it using `telegraf --usage `. dsn = "username:password@mysqlserver:3307/dbname?param=value" ## Timeout for any operation + ## Note that the timeout for queries is per query not per gather. # timeout = "5s" ## Connection time limits diff --git a/plugins/inputs/sql/sql.go b/plugins/inputs/sql/sql.go index 383f04c40..c6c4658d8 100644 --- a/plugins/inputs/sql/sql.go +++ b/plugins/inputs/sql/sql.go @@ -30,6 +30,7 @@ const sampleConfig = ` dsn = "username:password@mysqlserver:3307/dbname?param=value" ## Timeout for any operation + ## Note that the timeout for queries is per query not per gather. # timeout = "5s" ## Connection time limits @@ -486,16 +487,13 @@ func (s *SQL) Stop() { func (s *SQL) Gather(acc telegraf.Accumulator) error { var wg sync.WaitGroup - - ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.Timeout)) - defer cancel() - tstart := time.Now() for _, query := range s.Queries { wg.Add(1) - go func(q Query) { defer wg.Done() + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(s.Timeout)) + defer cancel() if err := s.executeQuery(ctx, acc, q, tstart); err != nil { acc.AddError(err) }