feat(inputs.sqlserver): introduce timeout for query execution (#12319)
This commit is contained in:
parent
41c9af5bb8
commit
e44129869c
|
|
@ -134,6 +134,11 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
||||||
"Server=192.168.1.10;Port=1433;User Id=<user>;Password=<pw>;app name=telegraf;log=1;",
|
"Server=192.168.1.10;Port=1433;User Id=<user>;Password=<pw>;app name=telegraf;log=1;",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
## Timeout for query execution operation
|
||||||
|
## Note that the timeout for queries is per query not per gather.
|
||||||
|
## 0 value means no timeout
|
||||||
|
# query_timeout = "0s"
|
||||||
|
|
||||||
## Authentication method
|
## Authentication method
|
||||||
## valid methods: "connection_string", "AAD"
|
## valid methods: "connection_string", "AAD"
|
||||||
# auth_method = "connection_string"
|
# auth_method = "connection_string"
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,11 @@
|
||||||
"Server=192.168.1.10;Port=1433;User Id=<user>;Password=<pw>;app name=telegraf;log=1;",
|
"Server=192.168.1.10;Port=1433;User Id=<user>;Password=<pw>;app name=telegraf;log=1;",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
## Timeout for query execution operation
|
||||||
|
## Note that the timeout for queries is per query not per gather.
|
||||||
|
## 0 value means no timeout
|
||||||
|
# query_timeout = "0s"
|
||||||
|
|
||||||
## Authentication method
|
## Authentication method
|
||||||
## valid methods: "connection_string", "AAD"
|
## valid methods: "connection_string", "AAD"
|
||||||
# auth_method = "connection_string"
|
# auth_method = "connection_string"
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
package sqlserver
|
package sqlserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
@ -14,6 +15,7 @@ import (
|
||||||
mssql "github.com/denisenkom/go-mssqldb"
|
mssql "github.com/denisenkom/go-mssqldb"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
|
"github.com/influxdata/telegraf/config"
|
||||||
"github.com/influxdata/telegraf/filter"
|
"github.com/influxdata/telegraf/filter"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"github.com/influxdata/telegraf/plugins/inputs"
|
||||||
)
|
)
|
||||||
|
|
@ -24,6 +26,7 @@ var sampleConfig string
|
||||||
// SQLServer struct
|
// SQLServer struct
|
||||||
type SQLServer struct {
|
type SQLServer struct {
|
||||||
Servers []string `toml:"servers"`
|
Servers []string `toml:"servers"`
|
||||||
|
QueryTimeout config.Duration `toml:"query_timeout"`
|
||||||
AuthMethod string `toml:"auth_method"`
|
AuthMethod string `toml:"auth_method"`
|
||||||
QueryVersion int `toml:"query_version" deprecated:"1.16.0;use 'database_type' instead"`
|
QueryVersion int `toml:"query_version" deprecated:"1.16.0;use 'database_type' instead"`
|
||||||
AzureDB bool `toml:"azuredb" deprecated:"1.16.0;use 'database_type' instead"`
|
AzureDB bool `toml:"azuredb" deprecated:"1.16.0;use 'database_type' instead"`
|
||||||
|
|
@ -294,7 +297,14 @@ func (s *SQLServer) Stop() {
|
||||||
|
|
||||||
func (s *SQLServer) gatherServer(pool *sql.DB, query Query, acc telegraf.Accumulator, connectionString string) error {
|
func (s *SQLServer) gatherServer(pool *sql.DB, query Query, acc telegraf.Accumulator, connectionString string) error {
|
||||||
// execute query
|
// execute query
|
||||||
rows, err := pool.Query(query.Script)
|
ctx := context.Background()
|
||||||
|
// Use the query timeout if any
|
||||||
|
if s.QueryTimeout > 0 {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(s.QueryTimeout))
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
rows, err := pool.QueryContext(ctx, query.Script)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
serverName, databaseName := getConnectionIdentifiers(connectionString)
|
serverName, databaseName := getConnectionIdentifiers(connectionString)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue