Skip to content

Commit 40daa1b

Browse files
port is optional for sqlserver
1 parent 5b07290 commit 40daa1b

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

internal/sqlquery/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ func (c Config) Validate() error {
3939
if c.DataSourceConfig.Host == "" {
4040
return errors.New("'datasource_config.host' or 'datasource' must be specified")
4141
}
42-
if c.DataSourceConfig.Port == 0 {
43-
return errors.New("'datasource_config.port' or 'datasource' must be specified")
42+
if c.Driver != "sqlserver" {
43+
if c.DataSourceConfig.Port == 0 {
44+
return errors.New("'datasource_config.port' or 'datasource' must be specified")
45+
}
4446
}
4547
if c.DataSourceConfig.Database == "" {
4648
return errors.New("'datasource_config.database' or 'datasource' must be specified")

receiver/sqlqueryreceiver/config_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,42 @@ func TestLoadConfig(t *testing.T) {
146146
id: component.NewIDWithName(metadata.Type, ""),
147147
errorMessage: "'datasource' must be specified",
148148
},
149+
{
150+
fname: "config-invalid-missing-datasource-config-port-sql-server.yaml",
151+
id: component.NewIDWithName(metadata.Type, ""),
152+
expected: &Config{
153+
Config: sqlquery.Config{
154+
ControllerConfig: scraperhelper.ControllerConfig{
155+
CollectionInterval: 10 * time.Second,
156+
InitialDelay: time.Second,
157+
},
158+
Driver: "sqlserver",
159+
DataSourceConfig: sqlquery.DataSourceConfig{
160+
Host: "localhost",
161+
Database: "mydb",
162+
Username: "me",
163+
Password: "s3cr3t",
164+
},
165+
Queries: []sqlquery.Query{
166+
{
167+
SQL: "select count(*) as count, type from mytable group by type",
168+
Metrics: []sqlquery.MetricCfg{
169+
{
170+
MetricName: "val.count",
171+
ValueColumn: "count",
172+
AttributeColumns: []string{"type"},
173+
Monotonic: false,
174+
ValueType: sqlquery.MetricValueTypeInt,
175+
DataType: sqlquery.MetricTypeSum,
176+
Aggregation: sqlquery.MetricAggregationCumulative,
177+
StaticAttributes: map[string]string{"foo": "bar"},
178+
},
179+
},
180+
},
181+
},
182+
},
183+
},
184+
},
149185
{
150186
fname: "config-invalid-missing-datasource-config-port.yaml",
151187
id: component.NewIDWithName(metadata.Type, ""),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
sqlquery:
2+
collection_interval: 10s
3+
driver: sqlserver
4+
datasource_config:
5+
host: localhost
6+
database: mydb
7+
username: me
8+
password: s3cr3t
9+
queries:
10+
- sql: "select count(*) as count, type from mytable group by type"
11+
metrics:
12+
- metric_name: val.count
13+
value_column: "count"
14+
attribute_columns: ["type"]
15+
data_type: sum
16+
value_type: int
17+
monotonic: false
18+
aggregation: cumulative
19+
static_attributes:
20+
foo: bar

receiver/sqlqueryreceiver/testdata/config-invalid-missing-datasource-config-port.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ sqlquery:
33
driver: mysql
44
datasource_config:
55
host: localhost
6+
database: mydb
7+
username: me
8+
password: s3cr3t
69
queries:
710
- sql: "select count(*) as count, type from mytable group by type"
811
metrics:
912
- metric_name: val.count
1013
value_column: "count"
1114
attribute_columns: ["type"]
12-
data_type: gauge
15+
data_type: sum
1316
value_type: int
1417
monotonic: false
1518
aggregation: cumulative
19+
static_attributes:
20+
foo: bar

0 commit comments

Comments
 (0)