Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions etc/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 5 additions & 2 deletions plugins/inputs/sqlserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']
```

Expand Down
30 changes: 23 additions & 7 deletions plugins/inputs/sqlserver/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
33 changes: 32 additions & 1 deletion plugins/inputs/sqlserver/sqlserver_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down