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
# # 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

View File

@ -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']
```

View File

@ -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 {

View File

@ -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