Remove package level vars from sqlserver and mysql input plugins (#6468)
This commit is contained in:
committed by
Daniel Nelson
parent
8eb8643a3a
commit
c26aeb871d
@@ -12,10 +12,12 @@ import (
|
||||
|
||||
// SQLServer struct
|
||||
type SQLServer struct {
|
||||
Servers []string `toml:"servers"`
|
||||
QueryVersion int `toml:"query_version"`
|
||||
AzureDB bool `toml:"azuredb"`
|
||||
ExcludeQuery []string `toml:"exclude_query"`
|
||||
Servers []string `toml:"servers"`
|
||||
QueryVersion int `toml:"query_version"`
|
||||
AzureDB bool `toml:"azuredb"`
|
||||
ExcludeQuery []string `toml:"exclude_query"`
|
||||
queries MapQuery
|
||||
isInitialized bool
|
||||
}
|
||||
|
||||
// Query struct
|
||||
@@ -28,14 +30,9 @@ type Query struct {
|
||||
// MapQuery type
|
||||
type MapQuery map[string]Query
|
||||
|
||||
var queries MapQuery
|
||||
const defaultServer = "Server=.;app name=telegraf;log=1;"
|
||||
|
||||
// Initialized flag
|
||||
var isInitialized = false
|
||||
|
||||
var defaultServer = "Server=.;app name=telegraf;log=1;"
|
||||
|
||||
var sampleConfig = `
|
||||
const sampleConfig = `
|
||||
## Specify instances to monitor with a list of connection strings.
|
||||
## All connection parameters are optional.
|
||||
## By default, the host is localhost, listening on default port, TCP 1433.
|
||||
@@ -89,8 +86,8 @@ type scanner interface {
|
||||
}
|
||||
|
||||
func initQueries(s *SQLServer) {
|
||||
queries = make(MapQuery)
|
||||
|
||||
s.queries = make(MapQuery)
|
||||
queries := s.queries
|
||||
// If this is an AzureDB instance, grab some extra metrics
|
||||
if s.AzureDB {
|
||||
queries["AzureDBResourceStats"] = Query{Script: sqlAzureDBResourceStats, ResultByRow: false}
|
||||
@@ -124,12 +121,12 @@ func initQueries(s *SQLServer) {
|
||||
}
|
||||
|
||||
// Set a flag so we know that queries have already been initialized
|
||||
isInitialized = true
|
||||
s.isInitialized = true
|
||||
}
|
||||
|
||||
// Gather collect data from SQL Server
|
||||
func (s *SQLServer) Gather(acc telegraf.Accumulator) error {
|
||||
if !isInitialized {
|
||||
if !s.isInitialized {
|
||||
initQueries(s)
|
||||
}
|
||||
|
||||
@@ -140,7 +137,7 @@ func (s *SQLServer) Gather(acc telegraf.Accumulator) error {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for _, serv := range s.Servers {
|
||||
for _, query := range queries {
|
||||
for _, query := range s.queries {
|
||||
wg.Add(1)
|
||||
go func(serv string, query Query) {
|
||||
defer wg.Done()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sqlserver
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -14,7 +15,7 @@ func TestSqlServer_ParseMetrics(t *testing.T) {
|
||||
|
||||
var acc testutil.Accumulator
|
||||
|
||||
queries = make(MapQuery)
|
||||
queries := make(MapQuery)
|
||||
queries["PerformanceCounters"] = Query{Script: mockPerformanceCounters, ResultByRow: true}
|
||||
queries["WaitStatsCategorized"] = Query{Script: mockWaitStatsCategorized, ResultByRow: false}
|
||||
queries["CPUHistory"] = Query{Script: mockCPUHistory, ResultByRow: false}
|
||||
@@ -81,6 +82,64 @@ func TestSqlServer_ParseMetrics(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSqlServer_MultipleInstance(t *testing.T) {
|
||||
// Invoke Gather() from two separate configurations and
|
||||
// confirm they don't interfere with each other
|
||||
if testing.Short() {
|
||||
t.Skip("Skipping integration test in short mode")
|
||||
}
|
||||
testServer := "Server=127.0.0.1;Port=1433;User Id=SA;Password=ABCabc01;app name=telegraf;log=1"
|
||||
s := &SQLServer{
|
||||
Servers: []string{testServer},
|
||||
ExcludeQuery: []string{"MemoryClerk"},
|
||||
}
|
||||
s2 := &SQLServer{
|
||||
Servers: []string{testServer},
|
||||
ExcludeQuery: []string{"DatabaseSize"},
|
||||
}
|
||||
|
||||
var acc, acc2 testutil.Accumulator
|
||||
err := s.Gather(&acc)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, s.isInitialized, true)
|
||||
assert.Equal(t, s2.isInitialized, false)
|
||||
|
||||
err = s2.Gather(&acc2)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, s.isInitialized, true)
|
||||
assert.Equal(t, s2.isInitialized, true)
|
||||
|
||||
// acc includes size metrics, and excludes memory metrics
|
||||
assert.False(t, acc.HasMeasurement("Memory breakdown (%)"))
|
||||
assert.True(t, acc.HasMeasurement("Log size (bytes)"))
|
||||
|
||||
// acc2 includes memory metrics, and excludes size metrics
|
||||
assert.True(t, acc2.HasMeasurement("Memory breakdown (%)"))
|
||||
assert.False(t, acc2.HasMeasurement("Log size (bytes)"))
|
||||
}
|
||||
|
||||
func TestSqlServer_MultipleInit(t *testing.T) {
|
||||
|
||||
s := &SQLServer{}
|
||||
s2 := &SQLServer{
|
||||
ExcludeQuery: []string{"DatabaseSize"},
|
||||
}
|
||||
|
||||
initQueries(s)
|
||||
_, ok := s.queries["DatabaseSize"]
|
||||
// acc includes size metrics
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, s.isInitialized, true)
|
||||
assert.Equal(t, s2.isInitialized, false)
|
||||
|
||||
initQueries(s2)
|
||||
_, ok = s2.queries["DatabaseSize"]
|
||||
// acc2 excludes size metrics
|
||||
assert.False(t, ok)
|
||||
assert.Equal(t, s.isInitialized, true)
|
||||
assert.Equal(t, s2.isInitialized, true)
|
||||
}
|
||||
|
||||
const mockPerformanceMetrics = `measurement;servername;type;Point In Time Recovery;Available physical memory (bytes);Average pending disk IO;Average runnable tasks;Average tasks;Buffer pool rate (bytes/sec);Connection memory per connection (bytes);Memory grant pending;Page File Usage (%);Page lookup per batch request;Page split per batch request;Readahead per page read;Signal wait (%);Sql compilation per batch request;Sql recompilation per batch request;Total target memory ratio
|
||||
Performance metrics;WIN8-DEV;Performance metrics;0;6353158144;0;0;7;2773;415061;0;25;229371;130;10;18;188;52;14`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user