From 25d64c0c2368318f024fc6bb5b45533fea10a511 Mon Sep 17 00:00:00 2001 From: Stefan Kurek Date: Tue, 21 Nov 2023 17:24:16 -0500 Subject: [PATCH] Removes query_config_file from mssql config params --- CHANGELOG.md | 2 +- component/prometheus/exporter/mssql/mssql.go | 21 +-- .../prometheus/exporter/mssql/mssql_test.go | 149 ++++++++++++++---- .../components/prometheus.exporter.mssql.md | 13 +- .../integrations/mssql-config.md | 12 +- pkg/integrations/mssql/sql_exporter.go | 33 +--- pkg/integrations/mssql/sql_exporter_test.go | 99 +----------- .../mssql/test/bad_query_config.txt | 1 - 8 files changed, 140 insertions(+), 190 deletions(-) delete mode 100644 pkg/integrations/mssql/test/bad_query_config.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index d50cf8249519..49865da4a410 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -137,7 +137,7 @@ v0.38.0 (2023-11-21) - Allow agent to start with `module.git` config if cached before. (@hainenber) -- Adds new optional config parameters `query_config_file` and `query_config` to `mssql` integration to allow for custom metrics (@StefanKurek) +- Adds new optional config parameter `query_config` to `mssql` integration to allow for custom metrics (@StefanKurek) ### Bugfixes diff --git a/component/prometheus/exporter/mssql/mssql.go b/component/prometheus/exporter/mssql/mssql.go index 906fc5bc6b62..bef73f16a44c 100644 --- a/component/prometheus/exporter/mssql/mssql.go +++ b/component/prometheus/exporter/mssql/mssql.go @@ -3,9 +3,9 @@ package mssql import ( "errors" "fmt" - "os" "time" + "github.com/burningalchemist/sql_exporter/config" "github.com/grafana/agent/component" "github.com/grafana/agent/component/prometheus/exporter" "github.com/grafana/agent/pkg/integrations" @@ -13,6 +13,7 @@ import ( "github.com/grafana/agent/pkg/util" "github.com/grafana/river/rivertypes" config_util "github.com/prometheus/common/config" + "gopkg.in/yaml.v2" ) func init() { @@ -43,7 +44,6 @@ type Arguments struct { MaxIdleConnections int `river:"max_idle_connections,attr,optional"` MaxOpenConnections int `river:"max_open_connections,attr,optional"` Timeout time.Duration `river:"timeout,attr,optional"` - QueryConfigFile string `river:"query_config_file,attr,optional"` QueryConfig rivertypes.OptionalSecret `river:"query_config,attr,optional"` } @@ -66,18 +66,10 @@ func (a *Arguments) Validate() error { return errors.New("timeout must be positive") } - if a.QueryConfigFile != "" { - _, err := os.Stat(a.QueryConfigFile) - - if err == nil { - return nil - } - - if errors.Is(err, os.ErrNotExist) { - return errors.New("query_config_file must be a valid path of a YAML config file") - } else { - return fmt.Errorf("query_config_file file has issues: %w", err) - } + var collectorConfig config.CollectorConfig + err := yaml.UnmarshalStrict([]byte(a.QueryConfig.Value), &collectorConfig) + if err != nil { + return fmt.Errorf("invalid query_config: %s", err) } return nil @@ -89,7 +81,6 @@ func (a *Arguments) Convert() *mssql.Config { MaxIdleConnections: a.MaxIdleConnections, MaxOpenConnections: a.MaxOpenConnections, Timeout: a.Timeout, - QueryConfigFile: a.QueryConfigFile, QueryConfig: util.RawYAML(a.QueryConfig.Value), } } diff --git a/component/prometheus/exporter/mssql/mssql_test.go b/component/prometheus/exporter/mssql/mssql_test.go index 7d297b39136a..4fad4a819780 100644 --- a/component/prometheus/exporter/mssql/mssql_test.go +++ b/component/prometheus/exporter/mssql/mssql_test.go @@ -1,26 +1,24 @@ package mssql import ( - "path/filepath" "testing" "time" + "github.com/burningalchemist/sql_exporter/config" "github.com/grafana/agent/pkg/integrations/mssql" "github.com/grafana/river" "github.com/grafana/river/rivertypes" config_util "github.com/prometheus/common/config" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v2" ) func TestRiverUnmarshal(t *testing.T) { - goodQueryPath, _ := filepath.Abs("../../../../pkg/integrations/mssql/collector_config.yaml") - riverConfig := ` connection_string = "sqlserver://user:pass@localhost:1433" max_idle_connections = 3 max_open_connections = 3 - timeout = "10s" - query_config_file = "` + goodQueryPath + `"` + timeout = "10s"` var args Arguments err := river.Unmarshal([]byte(riverConfig), &args) @@ -31,12 +29,69 @@ func TestRiverUnmarshal(t *testing.T) { MaxIdleConnections: 3, MaxOpenConnections: 3, Timeout: 10 * time.Second, - QueryConfigFile: goodQueryPath, } require.Equal(t, expected, args) } +func TestRiverUnmarshalWithInlineQueryConfig(t *testing.T) { + riverConfig := ` + connection_string = "sqlserver://user:pass@localhost:1433" + max_idle_connections = 3 + max_open_connections = 3 + timeout = "10s" + query_config = "{ collector_name: mssql_standard, metrics: [ { metric_name: mssql_local_time_seconds, type: gauge, help: 'Local time in seconds since epoch (Unix time).', values: [ unix_time ], query: \"SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time\" } ] }"` + + var args Arguments + err := river.Unmarshal([]byte(riverConfig), &args) + require.NoError(t, err) + var collectorConfig config.CollectorConfig + err = yaml.UnmarshalStrict([]byte(args.QueryConfig.Value), &collectorConfig) + require.NoError(t, err) + + require.Equal(t, rivertypes.Secret("sqlserver://user:pass@localhost:1433"), args.ConnectionString) + require.Equal(t, 3, args.MaxIdleConnections) + require.Equal(t, 3, args.MaxOpenConnections) + require.Equal(t, 10*time.Second, args.Timeout) + require.Equal(t, "mssql_standard", collectorConfig.Name) + require.Equal(t, 1, len(collectorConfig.Metrics)) + require.Equal(t, "mssql_local_time_seconds", collectorConfig.Metrics[0].Name) + require.Equal(t, "gauge", collectorConfig.Metrics[0].TypeString) + require.Equal(t, "Local time in seconds since epoch (Unix time).", collectorConfig.Metrics[0].Help) + require.Equal(t, 1, len(collectorConfig.Metrics[0].Values)) + require.Contains(t, collectorConfig.Metrics[0].Values, "unix_time") + require.Equal(t, "SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time", collectorConfig.Metrics[0].QueryLiteral) +} + +func TestRiverUnmarshalWithInlineQueryConfigYaml(t *testing.T) { + riverConfig := ` + connection_string = "sqlserver://user:pass@localhost:1433" + max_idle_connections = 3 + max_open_connections = 3 + timeout = "10s" + query_config = "collector_name: mssql_standard\nmetrics:\n- metric_name: mssql_local_time_seconds\n type: gauge\n help: 'Local time in seconds since epoch (Unix time).'\n values: [unix_time]\n query: \"SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time\""` + + var args Arguments + err := river.Unmarshal([]byte(riverConfig), &args) + require.NoError(t, err) + var collectorConfig config.CollectorConfig + err = yaml.UnmarshalStrict([]byte(args.QueryConfig.Value), &collectorConfig) + require.NoError(t, err) + + require.Equal(t, rivertypes.Secret("sqlserver://user:pass@localhost:1433"), args.ConnectionString) + require.Equal(t, 3, args.MaxIdleConnections) + require.Equal(t, 3, args.MaxOpenConnections) + require.Equal(t, 10*time.Second, args.Timeout) + require.Equal(t, "mssql_standard", collectorConfig.Name) + require.Equal(t, 1, len(collectorConfig.Metrics)) + require.Equal(t, "mssql_local_time_seconds", collectorConfig.Metrics[0].Name) + require.Equal(t, "gauge", collectorConfig.Metrics[0].TypeString) + require.Equal(t, "Local time in seconds since epoch (Unix time).", collectorConfig.Metrics[0].Help) + require.Equal(t, 1, len(collectorConfig.Metrics[0].Values)) + require.Contains(t, collectorConfig.Metrics[0].Values, "unix_time") + require.Equal(t, "SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time", collectorConfig.Metrics[0].QueryLiteral) +} + func TestUnmarshalInvalid(t *testing.T) { invalidRiverConfig := ` connection_string = "sqlserver://user:pass@localhost:1433" @@ -48,11 +103,40 @@ func TestUnmarshalInvalid(t *testing.T) { var invalidArgs Arguments err := river.Unmarshal([]byte(invalidRiverConfig), &invalidArgs) require.Error(t, err) + require.EqualError(t, err, "timeout must be positive") } -func TestArgumentsValidate(t *testing.T) { - goodQueryPath, _ := filepath.Abs("../../../../pkg/integrations/mssql/collector_config.yaml") +func TestUnmarshalInvalidQueryConfigYaml(t *testing.T) { + invalidRiverConfig := ` + connection_string = "sqlserver://user:pass@localhost:1433" + max_idle_connections = 1 + max_open_connections = 1 + timeout = "1s" + query_config = "{ collector_name: mssql_standard, metrics: [ { metric_name: mssql_local_time_seconds, type: gauge, help: 'Local time in seconds since epoch (Unix time).', values: [ unix_time ], query: \"SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time\" }" + ` + + var invalidArgs Arguments + err := river.Unmarshal([]byte(invalidRiverConfig), &invalidArgs) + require.Error(t, err) + require.EqualError(t, err, "invalid query_config: yaml: line 1: did not find expected ',' or ']'") +} + +func TestUnmarshalInvalidProperty(t *testing.T) { + invalidRiverConfig := ` + connection_string = "sqlserver://user:pass@localhost:1433" + max_idle_connections = 1 + max_open_connections = 1 + timeout = "1s" + query_config = "collector_name: mssql_standard\nbad_param: true\nmetrics:\n- metric_name: mssql_local_time_seconds\n type: gauge\n help: 'Local time in seconds since epoch (Unix time).'\n values: [unix_time]\n query: \"SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time\"" + ` + var invalidArgs Arguments + err := river.Unmarshal([]byte(invalidRiverConfig), &invalidArgs) + require.Error(t, err) + require.EqualError(t, err, "invalid query_config: unknown fields in collector: bad_param") +} + +func TestArgumentsValidate(t *testing.T) { tests := []struct { name string args Arguments @@ -65,7 +149,6 @@ func TestArgumentsValidate(t *testing.T) { MaxIdleConnections: 1, MaxOpenConnections: 0, Timeout: 10 * time.Second, - QueryConfigFile: goodQueryPath, }, wantErr: true, }, @@ -76,7 +159,6 @@ func TestArgumentsValidate(t *testing.T) { MaxIdleConnections: 0, MaxOpenConnections: 1, Timeout: 10 * time.Second, - QueryConfigFile: goodQueryPath, }, wantErr: true, }, @@ -87,18 +169,6 @@ func TestArgumentsValidate(t *testing.T) { MaxIdleConnections: 1, MaxOpenConnections: 1, Timeout: 0, - QueryConfigFile: goodQueryPath, - }, - wantErr: true, - }, - { - name: "invalid query_config_file", - args: Arguments{ - ConnectionString: rivertypes.Secret("test"), - MaxIdleConnections: 1, - MaxOpenConnections: 1, - Timeout: 0, - QueryConfigFile: "doesnotexist.YAML", }, wantErr: true, }, @@ -109,7 +179,9 @@ func TestArgumentsValidate(t *testing.T) { MaxIdleConnections: 1, MaxOpenConnections: 1, Timeout: 10 * time.Second, - QueryConfigFile: goodQueryPath, + QueryConfig: rivertypes.OptionalSecret{ + Value: `{ collector_name: mssql_standard, metrics: [ { metric_name: mssql_local_time_seconds, type: gauge, help: 'Local time in seconds since epoch (Unix time).', values: [ unix_time ], query: "SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time" } ] }`, + }, }, wantErr: false, }, @@ -128,22 +200,31 @@ func TestArgumentsValidate(t *testing.T) { } func TestConvert(t *testing.T) { - goodQueryPath, _ := filepath.Abs("../../../../pkg/integrations/mssql/collector_config.yaml") - riverConfig := ` - connection_string = "sqlserver://user:pass@localhost:1433" - query_config_file = "` + goodQueryPath + `"` - var args Arguments - err := river.Unmarshal([]byte(riverConfig), &args) - require.NoError(t, err) + strQueryConfig := `collector_name: mssql_standard +metrics: +- metric_name: mssql_local_time_seconds + type: gauge + help: 'Local time in seconds since epoch (Unix time).' + values: [unix_time] + query: "SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time"` + args := Arguments{ + ConnectionString: rivertypes.Secret("sqlserver://user:pass@localhost:1433"), + MaxIdleConnections: 1, + MaxOpenConnections: 1, + Timeout: 10 * time.Second, + QueryConfig: rivertypes.OptionalSecret{ + Value: strQueryConfig, + }, + } res := args.Convert() expected := mssql.Config{ ConnectionString: config_util.Secret("sqlserver://user:pass@localhost:1433"), - MaxIdleConnections: DefaultArguments.MaxIdleConnections, - MaxOpenConnections: DefaultArguments.MaxOpenConnections, - Timeout: DefaultArguments.Timeout, - QueryConfigFile: goodQueryPath, + MaxIdleConnections: 1, + MaxOpenConnections: 1, + Timeout: 10 * time.Second, + QueryConfig: []byte(strQueryConfig), } require.Equal(t, expected, *res) } diff --git a/docs/sources/flow/reference/components/prometheus.exporter.mssql.md b/docs/sources/flow/reference/components/prometheus.exporter.mssql.md index f2dcccf6c8eb..93fb305f8a5d 100644 --- a/docs/sources/flow/reference/components/prometheus.exporter.mssql.md +++ b/docs/sources/flow/reference/components/prometheus.exporter.mssql.md @@ -34,8 +34,7 @@ Omitted fields take their default values. | `max_idle_connections` | `int` | Maximum number of idle connections to any one target. | `3` | no | | `max_open_connections` | `int` | Maximum number of open connections to any one target. | `3` | no | | `timeout` | `duration` | The query timeout in seconds. | `"10s"` | no | -| `query_config_file` | `string` | MSSQL query to prometheus metric configuration file path. | | no | -| `query_config` | `string` | MSSQL query to prometheus metric configuration as an inline string. | | no | +| `query_config` | `string` | MSSQL query to Prometheus metric configuration as an inline string. | | no | [The sql_exporter examples](https://github.com/burningalchemist/sql_exporter/blob/master/examples/azure-sql-mi/sql_exporter.yml#L21) show the format of the `connection_string` argument: @@ -43,16 +42,14 @@ Omitted fields take their default values. sqlserver://USERNAME_HERE:PASSWORD_HERE@SQLMI_HERE_ENDPOINT.database.windows.net:1433?encrypt=true&hostNameInCertificate=%2A.SQL_MI_DOMAIN_HERE.database.windows.net&trustservercertificate=true ``` -Either `query_config_file` or `query_config` can be specified. -The `query_config_file` argument points to a YAML file defining which MSSQL queries map to custom Prometheus metrics. -The `query_config` argument must be a YAML document as string defining which MSSQL queries map to custom Prometheus metrics. +If specified, the `query_config` argument must be a YAML document as string defining which MSSQL queries map to custom Prometheus metrics. `query_config` is typically loaded by using the exports of another component. For example, - `local.file.LABEL.content` - `remote.http.LABEL.content` - `remote.s3.LABEL.content` -See [sql_exporter](https://github.com/burningalchemist/sql_exporter#collectors) for details on how to create a configuration file. +See [sql_exporter](https://github.com/burningalchemist/sql_exporter#collectors) for details on how to create a configuration. ## Blocks @@ -116,9 +113,9 @@ Replace the following: [scrape]: {{< relref "./prometheus.scrape.md" >}} ## Custom metrics -You can use the optional `query_config_file` or `query_config` parameters to retrieve custom Prometheus metrics for a MSSQL instance. +You can use the optional `query_config` parameter to retrieve custom Prometheus metrics for a MSSQL instance. -If either of these are defined, they will use the new configuration to query your MSSQL instance and create whatever Prometheus metrics are defined. +If this is defined, the new configuration will be used to query your MSSQL instance and create whatever Prometheus metrics are defined. If you want additional metrics on top of the default metrics, the default configuration must be used as a base. The default configuration used by this integration is as follows: diff --git a/docs/sources/static/configuration/integrations/mssql-config.md b/docs/sources/static/configuration/integrations/mssql-config.md index c1b630839988..76edb2fae888 100644 --- a/docs/sources/static/configuration/integrations/mssql-config.md +++ b/docs/sources/static/configuration/integrations/mssql-config.md @@ -89,13 +89,7 @@ Full reference of options: # The timeout for scraping metrics from the MSSQL instance. [timeout: | default = "10s"] - # Path to a YAML configuration file with custom Prometheus metrics and MSSQL queries. - # This field has precedence to the config defined in the query_config block. - # See https://github.com/burningalchemist/sql_exporter#collectors for more details how to specify your configuration file. - [query_config_file: ] - - # Embedded MSSQL query configuration for custom MSSQL Prometheus metrics. - # You can specify your metrics and queries here instead of in an external configuration file. + # Embedded MSSQL query configuration for specifying custom MSSQL Prometheus metrics. # See https://github.com/burningalchemist/sql_exporter#collectors for more details how to specify your metric configurations. query_config: [- ... ] @@ -103,9 +97,9 @@ Full reference of options: ``` ## Custom metrics -You can use the optional `query_config_file` or `query_config` parameters to retrieve custom Prometheus metrics for a MSSQL instance. +You can use the optional `query_config` parameter to retrieve custom Prometheus metrics for a MSSQL instance. -If either of these are defined, they will use the new configuration to query your MSSQL instance and create whatever Prometheus metrics are defined. +If this is defined, the new configuration will be used to query your MSSQL instance and create whatever Prometheus metrics are defined. If you want additional metrics on top of the default metrics, the default configuration must be used as a base. The default configuration used by this integration is as follows: diff --git a/pkg/integrations/mssql/sql_exporter.go b/pkg/integrations/mssql/sql_exporter.go index 603bc16fc9f6..fd9af2278be0 100644 --- a/pkg/integrations/mssql/sql_exporter.go +++ b/pkg/integrations/mssql/sql_exporter.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net/url" - "os" "time" "github.com/go-kit/log" @@ -34,7 +33,6 @@ type Config struct { MaxIdleConnections int `yaml:"max_idle_connections,omitempty"` MaxOpenConnections int `yaml:"max_open_connections,omitempty"` Timeout time.Duration `yaml:"timeout,omitempty"` - QueryConfigFile string `yaml:"query_config_file,omitempty"` QueryConfig util.RawYAML `yaml:"query_config,omitempty"` } @@ -64,20 +62,6 @@ func (c Config) validate() error { return errors.New("timeout must be positive") } - if c.QueryConfigFile != "" { - _, err := os.Stat(c.QueryConfigFile) - - if err == nil { - return nil - } - - if errors.Is(err, os.ErrNotExist) { - return errors.New("query_config_file must be a valid path of a YAML config file") - } else { - return fmt.Errorf("query_config_file file has issues: %w", err) - } - } - return nil } @@ -122,7 +106,7 @@ func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) } // Initialize collectorConfig from config params if needed - customCollectorConfig, err := createCollectorConfig(c.QueryConfigFile, c.QueryConfig) + customCollectorConfig, err := createCollectorConfig(c.QueryConfig) if err != nil { return nil, fmt.Errorf("failed to create mssql target: %w", err) } @@ -158,22 +142,9 @@ func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) ), nil } -func createCollectorConfig(queryConfigFile string, queryConfig util.RawYAML) (*config.CollectorConfig, error) { +func createCollectorConfig(queryConfig util.RawYAML) (*config.CollectorConfig, error) { var customCollectorConfig *config.CollectorConfig - if queryConfigFile != "" { - yamlFile, err := os.ReadFile(queryConfigFile) - if err != nil { - return nil, fmt.Errorf("problem reading query_config_file: %w", err) - } - err = yaml.Unmarshal(yamlFile, &customCollectorConfig) - if err != nil { - return nil, fmt.Errorf("query_config_file file not in correct format: %w", err) - } - - return customCollectorConfig, nil - } - if err := yaml.Unmarshal(queryConfig, &customCollectorConfig); err != nil { return nil, fmt.Errorf("query_config not in correct format: %w", err) } diff --git a/pkg/integrations/mssql/sql_exporter_test.go b/pkg/integrations/mssql/sql_exporter_test.go index 3aee5a870457..90b4840baae3 100644 --- a/pkg/integrations/mssql/sql_exporter_test.go +++ b/pkg/integrations/mssql/sql_exporter_test.go @@ -2,7 +2,6 @@ package mssql import ( "os" - "path/filepath" "testing" "time" @@ -12,7 +11,6 @@ import ( ) func TestConfig_validate(t *testing.T) { - goodQueryPath, _ := filepath.Abs("./collector_config.yaml") strConfig := `--- collector_name: mssql_standard @@ -98,27 +96,6 @@ metrics: }, err: "timeout must be positive", }, - { - name: "bad query config file path", - input: Config{ - ConnectionString: "sqlserver://user:pass@localhost:1433", - MaxIdleConnections: 3, - MaxOpenConnections: 3, - Timeout: 10 * time.Second, - QueryConfigFile: "doesnotexist.YAML", - }, - err: "query_config_file must be a valid path of a YAML config file", - }, - { - name: "good query config file", - input: Config{ - ConnectionString: "sqlserver://user:pass@localhost:1433", - MaxIdleConnections: 3, - MaxOpenConnections: 3, - Timeout: 10 * time.Second, - QueryConfigFile: goodQueryPath, - }, - }, { name: "good query config", input: Config{ @@ -143,9 +120,8 @@ metrics: }) } } -func TestConfig_UnmarshalYaml(t *testing.T) { - goodQueryPath, _ := filepath.Abs("./collector_config.yaml") +func TestConfig_UnmarshalYaml(t *testing.T) { t.Run("only required values", func(t *testing.T) { strConfig := `connection_string: "sqlserver://user:pass@localhost:1433"` @@ -175,7 +151,6 @@ metrics: max_idle_connections: 5 max_open_connections: 6 timeout: 1m -query_config_file: "` + goodQueryPath + `"` + ` query_config: collector_name: mssql_standard metrics: @@ -194,71 +169,14 @@ query_config: MaxIdleConnections: 5, MaxOpenConnections: 6, Timeout: time.Minute, - QueryConfigFile: goodQueryPath, QueryConfig: []byte(strQueryConfig), }, c) }) } func TestConfig_NewIntegration(t *testing.T) { - goodQueryPath, _ := filepath.Abs("./collector_config.yaml") - badQueryPath, _ := filepath.Abs("./test/bad_query_config.txt") - t.Run("integration with valid config", func(t *testing.T) { - c := &Config{ - ConnectionString: "sqlserver://user:pass@localhost:1433", - MaxIdleConnections: 3, - MaxOpenConnections: 3, - Timeout: 10 * time.Second, - QueryConfigFile: goodQueryPath, - } - - i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout)) - require.NoError(t, err) - require.NotNil(t, i) - }) - - t.Run("integration with invalid config", func(t *testing.T) { - c := &Config{ - ConnectionString: "mysql://user:pass@localhost:1433", - MaxIdleConnections: 3, - MaxOpenConnections: 3, - Timeout: 10 * time.Second, - } - - i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout)) - require.Nil(t, i) - require.ErrorContains(t, err, "failed to validate config:") - }) - - t.Run("integration with query config file and query config", func(t *testing.T) { - strConfig := `--- -collector_name: mssql_standard - -metrics: - - metric_name: mssql_local_time_seconds - type: gauge - help: 'Local time in seconds since epoch (Unix time).' - values: [unix_time] - query: | - SELECT DATEDIFF(second, '19700101', GETUTCDATE()) AS unix_time -` - c := &Config{ - ConnectionString: "sqlserver://user:pass@localhost:1433", - MaxIdleConnections: 3, - MaxOpenConnections: 3, - Timeout: 10 * time.Second, - QueryConfigFile: goodQueryPath, - QueryConfig: []byte(strConfig), - } - - i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout)) - require.NoError(t, err) - require.NotNil(t, i) - }) - - t.Run("integration with query config", func(t *testing.T) { - strConfig := `--- + strQueryConfig := `--- collector_name: mssql_standard metrics: @@ -273,7 +191,7 @@ metrics: MaxIdleConnections: 3, MaxOpenConnections: 3, Timeout: 10 * time.Second, - QueryConfig: []byte(strConfig), + QueryConfig: []byte(strQueryConfig), } i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout)) @@ -281,22 +199,21 @@ metrics: require.NotNil(t, i) }) - t.Run("integration with incorrect format for query config file", func(t *testing.T) { + t.Run("integration with invalid config", func(t *testing.T) { c := &Config{ - ConnectionString: "sqlserver://user:pass@localhost:1433", + ConnectionString: "mysql://user:pass@localhost:1433", MaxIdleConnections: 3, MaxOpenConnections: 3, Timeout: 10 * time.Second, - QueryConfigFile: badQueryPath, } i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout)) require.Nil(t, i) - require.ErrorContains(t, err, "failed to create mssql target: query_config_file file not in correct format: ") + require.ErrorContains(t, err, "failed to validate config:") }) t.Run("integration with invalid query config", func(t *testing.T) { - strConfig := `collector_name: mssql_standard + strQueryConfig := `collector_name: mssql_standard metrics: - metric_name: mssql_local_time_seconds @@ -309,7 +226,7 @@ metrics: MaxIdleConnections: 3, MaxOpenConnections: 3, Timeout: 10 * time.Second, - QueryConfig: []byte(strConfig), + QueryConfig: []byte(strQueryConfig), } i, err := c.NewIntegration(log.NewJSONLogger(os.Stdout)) diff --git a/pkg/integrations/mssql/test/bad_query_config.txt b/pkg/integrations/mssql/test/bad_query_config.txt deleted file mode 100644 index 5f20a3621b56..000000000000 --- a/pkg/integrations/mssql/test/bad_query_config.txt +++ /dev/null @@ -1 +0,0 @@ -This is not a valid config file.