Add option for explicitly including queries in sqlserver input plugin (#7150)

This commit is contained in:
Harshit Bansal 2020-03-17 02:16:42 +05:30 committed by GitHub
parent 87f60ccf87
commit a612a4d85f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 13 deletions

View File

@ -4346,8 +4346,7 @@
# ## If you are using AzureDB, setting this to true will gather resource utilization metrics # ## If you are using AzureDB, setting this to true will gather resource utilization metrics
# # azuredb = false # # azuredb = false
# #
# ## If you would like to exclude some of the metrics queries, list them here # ## Possible queries:
# ## Possible choices:
# ## - PerformanceCounters # ## - PerformanceCounters
# ## - WaitStatsCategorized # ## - WaitStatsCategorized
# ## - DatabaseIO # ## - DatabaseIO
@ -4363,7 +4362,11 @@
# ## - AzureDBResourceGovernance # ## - AzureDBResourceGovernance
# ## - SqlRequests # ## - SqlRequests
# ## - ServerProperties # ## - 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 # # Gather timeseries from Google Cloud Platform v3 monitoring API

View File

@ -54,8 +54,7 @@ GO
## If you are using AzureDB, setting this to true will gather resource utilization metrics ## If you are using AzureDB, setting this to true will gather resource utilization metrics
# azuredb = true # azuredb = true
## If you would like to exclude some of the metrics queries, list them here ## Possible queries:
## Possible choices:
## - PerformanceCounters ## - PerformanceCounters
## - WaitStatsCategorized ## - WaitStatsCategorized
## - DatabaseIO ## - DatabaseIO
@ -70,6 +69,10 @@ GO
## - AzureDBResourceGovernance ## - AzureDBResourceGovernance
## - SqlRequests ## - SqlRequests
## - ServerProperties ## - 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'] exclude_query = [ 'Schedulers' , 'SqlRequests']
``` ```

View File

@ -7,6 +7,7 @@ import (
_ "github.com/denisenkom/go-mssqldb" // go-mssqldb initialization _ "github.com/denisenkom/go-mssqldb" // go-mssqldb initialization
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
) )
@ -15,6 +16,7 @@ type SQLServer struct {
Servers []string `toml:"servers"` Servers []string `toml:"servers"`
QueryVersion int `toml:"query_version"` QueryVersion int `toml:"query_version"`
AzureDB bool `toml:"azuredb"` AzureDB bool `toml:"azuredb"`
IncludeQuery []string `toml:"include_query"`
ExcludeQuery []string `toml:"exclude_query"` ExcludeQuery []string `toml:"exclude_query"`
queries MapQuery queries MapQuery
isInitialized bool isInitialized bool
@ -52,8 +54,7 @@ const sampleConfig = `
## If you are using AzureDB, setting this to true will gather resource utilization metrics ## If you are using AzureDB, setting this to true will gather resource utilization metrics
# azuredb = false # azuredb = false
## If you would like to exclude some of the metrics queries, list them here ## Possible queries:
## Possible choices:
## - PerformanceCounters ## - PerformanceCounters
## - WaitStatsCategorized ## - WaitStatsCategorized
## - DatabaseIO ## - DatabaseIO
@ -69,7 +70,11 @@ const sampleConfig = `
## - AzureDBResourceGovernance ## - AzureDBResourceGovernance
## - SqlRequests ## - SqlRequests
## - ServerProperties ## - 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 // SampleConfig return the sample configuration
@ -86,7 +91,7 @@ type scanner interface {
Scan(dest ...interface{}) error Scan(dest ...interface{}) error
} }
func initQueries(s *SQLServer) { func initQueries(s *SQLServer) error {
s.queries = make(MapQuery) s.queries = make(MapQuery)
queries := s.queries queries := s.queries
// If this is an AzureDB instance, grab some extra metrics // 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} queries["PerformanceMetrics"] = Query{Script: sqlPerformanceMetrics, ResultByRow: false}
} }
for _, query := range s.ExcludeQuery { filterQueries, err := filter.NewIncludeExcludeFilter(s.IncludeQuery, s.ExcludeQuery)
delete(queries, query) 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 // Set a flag so we know that queries have already been initialized
s.isInitialized = true s.isInitialized = true
return nil
} }
// Gather collect data from SQL Server // Gather collect data from SQL Server
func (s *SQLServer) Gather(acc telegraf.Accumulator) error { func (s *SQLServer) Gather(acc telegraf.Accumulator) error {
if !s.isInitialized { if !s.isInitialized {
initQueries(s) if err := initQueries(s); err != nil {
acc.AddError(err)
return err
}
} }
if len(s.Servers) == 0 { if len(s.Servers) == 0 {

View File

@ -1,16 +1,47 @@
package sqlserver package sqlserver
import ( import (
"github.com/stretchr/testify/assert"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require" "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) { func TestSqlServer_ParseMetrics(t *testing.T) {
var acc testutil.Accumulator var acc testutil.Accumulator