diff --git a/etc/telegraf.conf b/etc/telegraf.conf index a13162b52..9621b59f1 100644 --- a/etc/telegraf.conf +++ b/etc/telegraf.conf @@ -4346,8 +4346,7 @@ # ## If you are using AzureDB, setting this to true will gather resource utilization metrics # # azuredb = false # -# ## If you would like to exclude some of the metrics queries, list them here -# ## Possible choices: +# ## Possible queries: # ## - PerformanceCounters # ## - WaitStatsCategorized # ## - DatabaseIO @@ -4363,7 +4362,11 @@ # ## - AzureDBResourceGovernance # ## - SqlRequests # ## - ServerProperties -# exclude_query = [ 'Schedulers' ] +# ## A list of queries to include. If not specified, all the above listed queries are used. +# # include_query = [] +# +# ## A list of queries to explicitly ignore. +# exclude_query = [ 'Schedulers' , 'SqlRequests'] # # Gather timeseries from Google Cloud Platform v3 monitoring API diff --git a/plugins/inputs/sqlserver/README.md b/plugins/inputs/sqlserver/README.md index 1b71165fb..23922c169 100644 --- a/plugins/inputs/sqlserver/README.md +++ b/plugins/inputs/sqlserver/README.md @@ -54,8 +54,7 @@ GO ## If you are using AzureDB, setting this to true will gather resource utilization metrics # azuredb = true - ## If you would like to exclude some of the metrics queries, list them here - ## Possible choices: + ## Possible queries: ## - PerformanceCounters ## - WaitStatsCategorized ## - DatabaseIO @@ -70,6 +69,10 @@ GO ## - AzureDBResourceGovernance ## - SqlRequests ## - ServerProperties + ## A list of queries to include. If not specified, all the above listed queries are used. + # include_query = [] + + ## A list of queries to explicitly ignore. exclude_query = [ 'Schedulers' , 'SqlRequests'] ``` diff --git a/plugins/inputs/sqlserver/sqlserver.go b/plugins/inputs/sqlserver/sqlserver.go index 67703d19e..c69db611a 100644 --- a/plugins/inputs/sqlserver/sqlserver.go +++ b/plugins/inputs/sqlserver/sqlserver.go @@ -7,6 +7,7 @@ import ( _ "github.com/denisenkom/go-mssqldb" // go-mssqldb initialization "github.com/influxdata/telegraf" + "github.com/influxdata/telegraf/filter" "github.com/influxdata/telegraf/plugins/inputs" ) @@ -15,6 +16,7 @@ type SQLServer struct { Servers []string `toml:"servers"` QueryVersion int `toml:"query_version"` AzureDB bool `toml:"azuredb"` + IncludeQuery []string `toml:"include_query"` ExcludeQuery []string `toml:"exclude_query"` queries MapQuery isInitialized bool @@ -52,8 +54,7 @@ const sampleConfig = ` ## If you are using AzureDB, setting this to true will gather resource utilization metrics # azuredb = false - ## If you would like to exclude some of the metrics queries, list them here - ## Possible choices: + ## Possible queries: ## - PerformanceCounters ## - WaitStatsCategorized ## - DatabaseIO @@ -69,7 +70,11 @@ const sampleConfig = ` ## - AzureDBResourceGovernance ## - SqlRequests ## - ServerProperties - exclude_query = [ 'Schedulers' ] + ## A list of queries to include. If not specified, all the above listed queries are used. + # include_query = [] + + ## A list of queries to explicitly ignore. + exclude_query = [ 'Schedulers' , 'SqlRequests'] ` // SampleConfig return the sample configuration @@ -86,7 +91,7 @@ type scanner interface { Scan(dest ...interface{}) error } -func initQueries(s *SQLServer) { +func initQueries(s *SQLServer) error { s.queries = make(MapQuery) queries := s.queries // If this is an AzureDB instance, grab some extra metrics @@ -117,18 +122,29 @@ func initQueries(s *SQLServer) { queries["PerformanceMetrics"] = Query{Script: sqlPerformanceMetrics, ResultByRow: false} } - for _, query := range s.ExcludeQuery { - delete(queries, query) + filterQueries, err := filter.NewIncludeExcludeFilter(s.IncludeQuery, s.ExcludeQuery) + if err != nil { + return err + } + + for query := range queries { + if !filterQueries.Match(query) { + delete(queries, query) + } } // Set a flag so we know that queries have already been initialized s.isInitialized = true + return nil } // Gather collect data from SQL Server func (s *SQLServer) Gather(acc telegraf.Accumulator) error { if !s.isInitialized { - initQueries(s) + if err := initQueries(s); err != nil { + acc.AddError(err) + return err + } } if len(s.Servers) == 0 { diff --git a/plugins/inputs/sqlserver/sqlserver_test.go b/plugins/inputs/sqlserver/sqlserver_test.go index b493fb13c..8097b070b 100644 --- a/plugins/inputs/sqlserver/sqlserver_test.go +++ b/plugins/inputs/sqlserver/sqlserver_test.go @@ -1,16 +1,47 @@ package sqlserver import ( - "github.com/stretchr/testify/assert" "strconv" "strings" "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" ) +func TestSqlServer_QueriesInclusionExclusion(t *testing.T) { + cases := []map[string]interface{}{ + { + "IncludeQuery": []string{}, + "ExcludeQuery": []string{"WaitStatsCategorized", "DatabaseIO", "ServerProperties", "MemoryClerk", "Schedulers"}, + "queries": []string{"PerformanceCounters", "SqlRequests"}, + "queriesTotal": 2, + }, + { + "IncludeQuery": []string{"PerformanceCounters", "SqlRequests"}, + "ExcludeQuery": []string{"SqlRequests", "WaitStatsCategorized", "DatabaseIO"}, + "queries": []string{"PerformanceCounters"}, + "queriesTotal": 1, + }, + } + + for _, test := range cases { + s := SQLServer{ + QueryVersion: 2, + IncludeQuery: test["IncludeQuery"].([]string), + ExcludeQuery: test["ExcludeQuery"].([]string), + } + initQueries(&s) + assert.Equal(t, len(s.queries), test["queriesTotal"].(int)) + for _, query := range test["queries"].([]string) { + assert.Contains(t, s.queries, query) + } + } +} + func TestSqlServer_ParseMetrics(t *testing.T) { var acc testutil.Accumulator