fix(secretstores): Handle array of secrets correctly (#12919)

This commit is contained in:
Sven Rebhan 2023-04-03 15:03:38 +02:00 committed by GitHub
parent 3213af612e
commit f8a991b829
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 127 additions and 110 deletions

View File

@ -26,7 +26,7 @@ import (
var sampleConfig string var sampleConfig string
type Mysql struct { type Mysql struct {
Servers []config.Secret `toml:"servers"` Servers []*config.Secret `toml:"servers"`
PerfEventsStatementsDigestTextLimit int64 `toml:"perf_events_statements_digest_text_limit"` PerfEventsStatementsDigestTextLimit int64 `toml:"perf_events_statements_digest_text_limit"`
PerfEventsStatementsLimit int64 `toml:"perf_events_statements_limit"` PerfEventsStatementsLimit int64 `toml:"perf_events_statements_limit"`
PerfEventsStatementsTimeLimit int64 `toml:"perf_events_statements_time_limit"` PerfEventsStatementsTimeLimit int64 `toml:"perf_events_statements_time_limit"`
@ -80,7 +80,8 @@ func (m *Mysql) Init() error {
// Default to localhost if nothing specified. // Default to localhost if nothing specified.
if len(m.Servers) == 0 { if len(m.Servers) == 0 {
m.Servers = append(m.Servers, config.NewSecret([]byte(localhost))) s := config.NewSecret([]byte(localhost))
m.Servers = append(m.Servers, &s)
} }
// Register the TLS configuration. Due to the registry being a global // Register the TLS configuration. Due to the registry being a global
@ -106,7 +107,7 @@ func (m *Mysql) Init() error {
for i, server := range m.Servers { for i, server := range m.Servers {
s, err := server.Get() s, err := server.Get()
if err != nil { if err != nil {
return fmt.Errorf("getting server %d failed", i) return fmt.Errorf("getting server %d failed: %w", i, err)
} }
dsn := string(s) dsn := string(s)
config.ReleaseSecret(s) config.ReleaseSecret(s)
@ -140,7 +141,7 @@ func (m *Mysql) Gather(acc telegraf.Accumulator) error {
// Loop through each server and collect metrics // Loop through each server and collect metrics
for _, server := range m.Servers { for _, server := range m.Servers {
wg.Add(1) wg.Add(1)
go func(s config.Secret) { go func(s *config.Secret) {
defer wg.Done() defer wg.Done()
acc.AddError(m.gatherServer(s, acc)) acc.AddError(m.gatherServer(s, acc))
}(server) }(server)
@ -413,7 +414,7 @@ const (
` `
) )
func (m *Mysql) gatherServer(server config.Secret, acc telegraf.Accumulator) error { func (m *Mysql) gatherServer(server *config.Secret, acc telegraf.Accumulator) error {
s, err := server.Get() s, err := server.Get()
if err != nil { if err != nil {
return err return err

View File

@ -37,8 +37,9 @@ func TestMysqlDefaultsToLocalIntegration(t *testing.T) {
defer container.Terminate() defer container.Terminate()
dsn := fmt.Sprintf("root@tcp(%s:%s)/", container.Address, container.Ports[servicePort]) dsn := fmt.Sprintf("root@tcp(%s:%s)/", container.Address, container.Ports[servicePort])
s := config.NewSecret([]byte(dsn))
m := &Mysql{ m := &Mysql{
Servers: []config.Secret{config.NewSecret([]byte(dsn))}, Servers: []*config.Secret{&s},
} }
require.NoError(t, m.Init()) require.NoError(t, m.Init())
@ -72,8 +73,9 @@ func TestMysqlMultipleInstancesIntegration(t *testing.T) {
defer container.Terminate() defer container.Terminate()
dsn := fmt.Sprintf("root@tcp(%s:%s)/?tls=false", container.Address, container.Ports[servicePort]) dsn := fmt.Sprintf("root@tcp(%s:%s)/?tls=false", container.Address, container.Ports[servicePort])
s := config.NewSecret([]byte(dsn))
m := &Mysql{ m := &Mysql{
Servers: []config.Secret{config.NewSecret([]byte(dsn))}, Servers: []*config.Secret{&s},
IntervalSlow: config.Duration(30 * time.Second), IntervalSlow: config.Duration(30 * time.Second),
GatherGlobalVars: true, GatherGlobalVars: true,
MetricVersion: 2, MetricVersion: 2,
@ -87,8 +89,9 @@ func TestMysqlMultipleInstancesIntegration(t *testing.T) {
// acc should have global variables // acc should have global variables
require.True(t, acc.HasMeasurement("mysql_variables")) require.True(t, acc.HasMeasurement("mysql_variables"))
s2 := config.NewSecret([]byte(dsn))
m2 := &Mysql{ m2 := &Mysql{
Servers: []config.Secret{config.NewSecret([]byte(dsn))}, Servers: []*config.Secret{&s2},
MetricVersion: 2, MetricVersion: 2,
} }
require.NoError(t, m2.Init()) require.NoError(t, m2.Init())
@ -205,8 +208,9 @@ func TestMysqlDNSAddTimeout(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := config.NewSecret([]byte(tt.input))
m := &Mysql{ m := &Mysql{
Servers: []config.Secret{config.NewSecret([]byte(tt.input))}, Servers: []*config.Secret{&s},
} }
require.NoError(t, m.Init()) require.NoError(t, m.Init())
equal, err := m.Servers[0].EqualTo([]byte(tt.output)) equal, err := m.Servers[0].EqualTo([]byte(tt.output))

View File

@ -20,10 +20,10 @@ func TestAzureSQLIntegration_Database_ResourceStats_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBResourceStats"}, IncludeQuery: []string{"AzureSQLDBResourceStats"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
@ -66,10 +66,10 @@ func TestAzureSQLIntegration_Database_ResourceGovernance_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBResourceGovernance"}, IncludeQuery: []string{"AzureSQLDBResourceGovernance"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
@ -128,10 +128,10 @@ func TestAzureSQLIntegration_Database_WaitStats_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBWaitStats"}, IncludeQuery: []string{"AzureSQLDBWaitStats"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
@ -166,10 +166,10 @@ func TestAzureSQLIntegration_Database_DatabaseIO_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBDatabaseIO"}, IncludeQuery: []string{"AzureSQLDBDatabaseIO"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
@ -213,10 +213,10 @@ func TestAzureSQLIntegration_Database_ServerProperties_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBServerProperties"}, IncludeQuery: []string{"AzureSQLDBServerProperties"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
@ -255,10 +255,10 @@ func TestAzureSQLIntegration_Database_OsWaitstats_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBOsWaitstats"}, IncludeQuery: []string{"AzureSQLDBOsWaitstats"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
@ -294,10 +294,10 @@ func TestAzureSQLIntegration_Database_MemoryClerks_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBMemoryClerks"}, IncludeQuery: []string{"AzureSQLDBMemoryClerks"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
@ -328,10 +328,10 @@ func TestAzureSQLIntegration_Database_PerformanceCounters_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBPerformanceCounters"}, IncludeQuery: []string{"AzureSQLDBPerformanceCounters"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
@ -365,10 +365,10 @@ func TestAzureSQLIntegration_Database_Requests_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBRequests"}, IncludeQuery: []string{"AzureSQLDBRequests"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
@ -424,10 +424,10 @@ func TestAzureSQLIntegration_Database_Schedulers_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_DB_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLDBSchedulers"}, IncludeQuery: []string{"AzureSQLDBSchedulers"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",

View File

@ -19,10 +19,10 @@ func TestAzureSQLIntegration_Managed_ResourceStats_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLMIResourceStats"}, IncludeQuery: []string{"AzureSQLMIResourceStats"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLManagedInstance", DatabaseType: "AzureSQLManagedInstance",
@ -53,10 +53,10 @@ func TestAzureSQLIntegration_Managed_ResourceGovernance_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLMIResourceGovernance"}, IncludeQuery: []string{"AzureSQLMIResourceGovernance"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLManagedInstance", DatabaseType: "AzureSQLManagedInstance",
@ -95,10 +95,10 @@ func TestAzureSQLIntegration_Managed_DatabaseIO_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLMIDatabaseIO"}, IncludeQuery: []string{"AzureSQLMIDatabaseIO"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLManagedInstance", DatabaseType: "AzureSQLManagedInstance",
@ -138,10 +138,10 @@ func TestAzureSQLIntegration_Managed_ServerProperties_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLMIServerProperties"}, IncludeQuery: []string{"AzureSQLMIServerProperties"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLManagedInstance", DatabaseType: "AzureSQLManagedInstance",
@ -186,10 +186,10 @@ func TestAzureSQLIntegration_Managed_OsWaitStats_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLMIOsWaitstats"}, IncludeQuery: []string{"AzureSQLMIOsWaitstats"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLManagedInstance", DatabaseType: "AzureSQLManagedInstance",
@ -224,10 +224,10 @@ func TestAzureSQLIntegration_Managed_MemoryClerks_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLMIMemoryClerks"}, IncludeQuery: []string{"AzureSQLMIMemoryClerks"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLManagedInstance", DatabaseType: "AzureSQLManagedInstance",
@ -257,10 +257,10 @@ func TestAzureSQLIntegration_Managed_PerformanceCounters_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLMIPerformanceCounters"}, IncludeQuery: []string{"AzureSQLMIPerformanceCounters"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLManagedInstance", DatabaseType: "AzureSQLManagedInstance",
@ -293,10 +293,10 @@ func TestAzureSQLIntegration_Managed_Requests_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLMIRequests"}, IncludeQuery: []string{"AzureSQLMIRequests"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLManagedInstance", DatabaseType: "AzureSQLManagedInstance",
@ -352,10 +352,10 @@ func TestAzureSQLIntegration_Managed_Schedulers_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_MI_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLMISchedulers"}, IncludeQuery: []string{"AzureSQLMISchedulers"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLManagedInstance", DatabaseType: "AzureSQLManagedInstance",

View File

@ -19,10 +19,10 @@ func TestAzureSQLIntegration_ElasticPool_ResourceStats_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLPoolResourceStats"}, IncludeQuery: []string{"AzureSQLPoolResourceStats"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLPool", DatabaseType: "AzureSQLPool",
@ -62,10 +62,10 @@ func TestAzureSQLIntegration_ElasticPool_ResourceGovernance_Query(t *testing.T)
} }
connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLPoolResourceGovernance"}, IncludeQuery: []string{"AzureSQLPoolResourceGovernance"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLPool", DatabaseType: "AzureSQLPool",
@ -126,10 +126,10 @@ func TestAzureSQLIntegration_ElasticPool_DatabaseIO_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLPoolDatabaseIO"}, IncludeQuery: []string{"AzureSQLPoolDatabaseIO"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLPool", DatabaseType: "AzureSQLPool",
@ -171,10 +171,10 @@ func TestAzureSQLIntegration_ElasticPool_OsWaitStats_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLPoolOsWaitStats"}, IncludeQuery: []string{"AzureSQLPoolOsWaitStats"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLPool", DatabaseType: "AzureSQLPool",
@ -209,10 +209,10 @@ func TestAzureSQLIntegration_ElasticPool_MemoryClerks_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLPoolMemoryClerks"}, IncludeQuery: []string{"AzureSQLPoolMemoryClerks"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLPool", DatabaseType: "AzureSQLPool",
@ -242,10 +242,10 @@ func TestAzureSQLIntegration_ElasticPool_PerformanceCounters_Query(t *testing.T)
} }
connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLPoolPerformanceCounters"}, IncludeQuery: []string{"AzureSQLPoolPerformanceCounters"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLPool", DatabaseType: "AzureSQLPool",
@ -277,10 +277,10 @@ func TestAzureSQLIntegration_ElasticPool_Schedulers_Query(t *testing.T) {
} }
connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING") connectionString := os.Getenv("AZURESQL_POOL_CONNECTION_STRING")
serversList := []config.Secret{config.NewSecret([]byte(connectionString))} sl := config.NewSecret([]byte(connectionString))
server := &SQLServer{ server := &SQLServer{
Servers: serversList, Servers: []*config.Secret{&sl},
IncludeQuery: []string{"AzureSQLPoolSchedulers"}, IncludeQuery: []string{"AzureSQLPoolSchedulers"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
DatabaseType: "AzureSQLPool", DatabaseType: "AzureSQLPool",

View File

@ -25,7 +25,7 @@ var sampleConfig string
// SQLServer struct // SQLServer struct
type SQLServer struct { type SQLServer struct {
Servers []config.Secret `toml:"servers"` Servers []*config.Secret `toml:"servers"`
QueryTimeout config.Duration `toml:"query_timeout"` QueryTimeout config.Duration `toml:"query_timeout"`
AuthMethod string `toml:"auth_method"` AuthMethod string `toml:"auth_method"`
QueryVersion int `toml:"query_version" deprecated:"1.16.0;use 'database_type' instead"` QueryVersion int `toml:"query_version" deprecated:"1.16.0;use 'database_type' instead"`
@ -450,7 +450,8 @@ func (s *SQLServer) getDatabaseTypeToLog() string {
func (s *SQLServer) Init() error { func (s *SQLServer) Init() error {
if len(s.Servers) == 0 { if len(s.Servers) == 0 {
s.Log.Warn("Warning: Server list is empty.") srv := config.NewSecret([]byte(defaultServer))
s.Servers = append(s.Servers, &srv)
} }
return nil return nil
@ -547,7 +548,6 @@ func (s *SQLServer) refreshToken() (*adal.Token, error) {
func init() { func init() {
inputs.Add("sqlserver", func() telegraf.Input { inputs.Add("sqlserver", func() telegraf.Input {
return &SQLServer{ return &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(defaultServer))},
AuthMethod: "connection_string", AuthMethod: "connection_string",
} }
}) })

View File

@ -116,13 +116,15 @@ func TestSqlServerIntegration_MultipleInstance(t *testing.T) {
t.Skip("Skipping as unable to open tcp connection with host '127.0.0.1:1433") t.Skip("Skipping as unable to open tcp connection with host '127.0.0.1:1433")
testServer := "Server=127.0.0.1;Port=1433;User Id=SA;Password=ABCabc01;app name=telegraf;log=1" testServer := "Server=127.0.0.1;Port=1433;User Id=SA;Password=ABCabc01;app name=telegraf;log=1"
sl := config.NewSecret([]byte(testServer))
s := &SQLServer{ s := &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(testServer))}, Servers: []*config.Secret{&sl},
ExcludeQuery: []string{"MemoryClerk"}, ExcludeQuery: []string{"MemoryClerk"},
Log: testutil.Logger{}, Log: testutil.Logger{},
} }
sl2 := config.NewSecret([]byte(testServer))
s2 := &SQLServer{ s2 := &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(testServer))}, Servers: []*config.Secret{&sl2},
ExcludeQuery: []string{"DatabaseSize"}, ExcludeQuery: []string{"DatabaseSize"},
Log: testutil.Logger{}, Log: testutil.Logger{},
} }
@ -153,13 +155,16 @@ func TestSqlServerIntegration_MultipleInstanceWithHealthMetric(t *testing.T) {
t.Skip("Skipping as unable to open tcp connection with host '127.0.0.1:1433") t.Skip("Skipping as unable to open tcp connection with host '127.0.0.1:1433")
testServer := "Server=127.0.0.1;Port=1433;User Id=SA;Password=ABCabc01;app name=telegraf;log=1" testServer := "Server=127.0.0.1;Port=1433;User Id=SA;Password=ABCabc01;app name=telegraf;log=1"
sl := config.NewSecret([]byte(testServer))
s := &SQLServer{ s := &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(testServer))}, Servers: []*config.Secret{&sl},
ExcludeQuery: []string{"MemoryClerk"}, ExcludeQuery: []string{"MemoryClerk"},
Log: testutil.Logger{}, Log: testutil.Logger{},
} }
sl2 := config.NewSecret([]byte(testServer))
s2 := &SQLServer{ s2 := &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(testServer))}, Servers: []*config.Secret{&sl2},
ExcludeQuery: []string{"DatabaseSize"}, ExcludeQuery: []string{"DatabaseSize"},
HealthMetric: true, HealthMetric: true,
Log: testutil.Logger{}, Log: testutil.Logger{},
@ -194,19 +199,20 @@ func TestSqlServer_HealthMetric(t *testing.T) {
fakeServer1 := "localhost\\fakeinstance1;Database=fakedb1;Password=ABCabc01;" fakeServer1 := "localhost\\fakeinstance1;Database=fakedb1;Password=ABCabc01;"
fakeServer2 := "localhost\\fakeinstance2;Database=fakedb2;Password=ABCabc01;" fakeServer2 := "localhost\\fakeinstance2;Database=fakedb2;Password=ABCabc01;"
fs1 := config.NewSecret([]byte(fakeServer1))
fs2 := config.NewSecret([]byte(fakeServer2))
s1 := &SQLServer{ s1 := &SQLServer{
Servers: []config.Secret{ Servers: []*config.Secret{&fs1, &fs2},
config.NewSecret([]byte(fakeServer1)),
config.NewSecret([]byte(fakeServer2)),
},
IncludeQuery: []string{"DatabaseSize", "MemoryClerk"}, IncludeQuery: []string{"DatabaseSize", "MemoryClerk"},
HealthMetric: true, HealthMetric: true,
AuthMethod: "connection_string", AuthMethod: "connection_string",
Log: testutil.Logger{}, Log: testutil.Logger{},
} }
sl2 := config.NewSecret([]byte(fakeServer1))
s2 := &SQLServer{ s2 := &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(fakeServer1))}, Servers: []*config.Secret{&sl2},
IncludeQuery: []string{"DatabaseSize"}, IncludeQuery: []string{"DatabaseSize"},
AuthMethod: "connection_string", AuthMethod: "connection_string",
Log: testutil.Logger{}, Log: testutil.Logger{},
@ -347,14 +353,17 @@ func TestSqlServerIntegration_AGQueriesApplicableForDatabaseTypeSQLServer(t *tes
} }
testServer := os.Getenv("AZURESQL_POOL_CONNECTION_STRING") testServer := os.Getenv("AZURESQL_POOL_CONNECTION_STRING")
sl := config.NewSecret([]byte(testServer))
s := &SQLServer{ s := &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(testServer))}, Servers: []*config.Secret{&sl},
DatabaseType: "SQLServer", DatabaseType: "SQLServer",
IncludeQuery: []string{"SQLServerAvailabilityReplicaStates", "SQLServerDatabaseReplicaStates"}, IncludeQuery: []string{"SQLServerAvailabilityReplicaStates", "SQLServerDatabaseReplicaStates"},
Log: testutil.Logger{}, Log: testutil.Logger{},
} }
sl2 := config.NewSecret([]byte(testServer))
s2 := &SQLServer{ s2 := &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(testServer))}, Servers: []*config.Secret{&sl2},
DatabaseType: "AzureSQLDB", DatabaseType: "AzureSQLDB",
IncludeQuery: []string{"SQLServerAvailabilityReplicaStates", "SQLServerDatabaseReplicaStates"}, IncludeQuery: []string{"SQLServerAvailabilityReplicaStates", "SQLServerDatabaseReplicaStates"},
Log: testutil.Logger{}, Log: testutil.Logger{},
@ -398,14 +407,17 @@ func TestSqlServerIntegration_AGQueryFieldsOutputBasedOnSQLServerVersion(t *test
testServer2019 := os.Getenv("AZURESQL_POOL_CONNECTION_STRING_2019") testServer2019 := os.Getenv("AZURESQL_POOL_CONNECTION_STRING_2019")
testServer2012 := os.Getenv("AZURESQL_POOL_CONNECTION_STRING_2012") testServer2012 := os.Getenv("AZURESQL_POOL_CONNECTION_STRING_2012")
sl2019 := config.NewSecret([]byte(testServer2019))
sl2012 := config.NewSecret([]byte(testServer2012))
s2019 := &SQLServer{ s2019 := &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(testServer2019))}, Servers: []*config.Secret{&sl2019},
DatabaseType: "SQLServer", DatabaseType: "SQLServer",
IncludeQuery: []string{"SQLServerAvailabilityReplicaStates", "SQLServerDatabaseReplicaStates"}, IncludeQuery: []string{"SQLServerAvailabilityReplicaStates", "SQLServerDatabaseReplicaStates"},
Log: testutil.Logger{}, Log: testutil.Logger{},
} }
s2012 := &SQLServer{ s2012 := &SQLServer{
Servers: []config.Secret{config.NewSecret([]byte(testServer2012))}, Servers: []*config.Secret{&sl2012},
DatabaseType: "SQLServer", DatabaseType: "SQLServer",
IncludeQuery: []string{"SQLServerAvailabilityReplicaStates", "SQLServerDatabaseReplicaStates"}, IncludeQuery: []string{"SQLServerAvailabilityReplicaStates", "SQLServerDatabaseReplicaStates"},
Log: testutil.Logger{}, Log: testutil.Logger{},