Skip to content

Commit 57c8f9b

Browse files
readme, force datasource OR connection fields
1 parent ffe1f8d commit 57c8f9b

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

.chloggen/sqlqueryreceiver-break-up-datasource-into-struct.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ change_type: enhancement
55
component: sqlqueryreceiver
66

77
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8-
note: Break up datasource into a struct with fields `host`, `port`, `database`, `username`, `password`, and `additional_params`.
8+
note: Add SQL connection fields `host`, `port`, `database`, `username`, `password`, and `additional_params`.
99

1010
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
1111
issues: [39760]
1212

1313
# (Optional) One or more lines of additional information to render under the primary note.
1414
# These lines will be padded with 2 spaces and then inserted directly into the document.
1515
# Use pipe (|) for multiline entries.
16-
subtext:
16+
subtext: These options can be used instead of the existing `datasource` configuration option.
1717

1818
# If your change doesn't affect end users or the exported elements of any package,
1919
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

internal/sqlquery/config.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,29 @@ func (c Config) Validate() error {
3131
if c.Driver == "" {
3232
return errors.New("'driver' cannot be empty")
3333
}
34-
if c.DataSource == "" {
34+
35+
// If datasource is set, none of the individual connection parameters should be set
36+
if c.DataSource != "" {
37+
if c.Host != "" {
38+
return errors.New("'host' cannot be set when 'datasource' is specified")
39+
}
40+
if c.Port != 0 {
41+
return errors.New("'port' cannot be set when 'datasource' is specified")
42+
}
43+
if c.Database != "" {
44+
return errors.New("'database' cannot be set when 'datasource' is specified")
45+
}
46+
if c.Username != "" {
47+
return errors.New("'username' cannot be set when 'datasource' is specified")
48+
}
49+
if c.Password != "" {
50+
return errors.New("'password' cannot be set when 'datasource' is specified")
51+
}
52+
if len(c.AdditionalParams) > 0 {
53+
return errors.New("'additional_params' cannot be set when 'datasource' is specified")
54+
}
55+
} else {
56+
// If datasource is not set, host, port, and database are required
3557
if c.Host == "" {
3658
return errors.New("'host' or 'datasource' must be specified")
3759
}

receiver/sqlqueryreceiver/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ The SQL Query Receiver uses custom SQL queries to generate metrics from a databa
2323
2424
## Configuration
2525

26-
The configuration supports the following top-level fields:
26+
**Connection Configuration (choose one approach):**
27+
28+
**Option 1: Individual connection parameters** (cannot be used when `datasource` is set):
2729

28-
- `driver`(required): The name of the database driver: one of _postgres_, _mysql_, _snowflake_, _sqlserver_, _hdb_ (SAP
29-
HANA), _oracle_ (Oracle DB), _tds_ (SapASE/Sybase).
3030
- `host` (optional): The hostname or IP address of the database server.
3131
- For the `sqlserver` driver, an instance appended to the hostname (e.g. `hostname1/instance1`) will be parsed properly into this connection string: `sqlserver://username:password@host:port/instance`.
3232
- `port` (optional for `sqlserver`, otherwise required): The port number of the database server.
@@ -38,7 +38,10 @@ The configuration supports the following top-level fields:
3838
- The `password` field supports sensitive value handling through environment variables or other secure value sources. Special characters (such as #, @, %, etc.) are automatically URL-encoded to ensure proper connection string formatting.
3939
- For `mysql`: No URL encoding is applied
4040
- `additional_params` (optional): Additional driver-specific connection parameters.
41-
- `datasource`(optional): The datasource value passed to [sql.Open](https://pkg.go.dev/database/sql#Open). This value overrides the optional fields listed above and does not perform any special character escaping. This is a driver-specific string usually consisting of at least a database name and connection information. This is sometimes referred to as the "connection string" in driver documentation. Examples:
41+
42+
**Option 2: Datasource string:**
43+
44+
- `datasource`(optional): The datasource value passed to [sql.Open](https://pkg.go.dev/database/sql#Open). This value is used instead of the individual connection parameters listed above and does not perform any special character escaping. This is a driver-specific string usually consisting of at least a database name and connection information. This is sometimes referred to as the "connection string" in driver documentation. Examples:
4245

4346
- [hdb](https://github.com/SAP/go-hdb) - `hdb://<USER>:<PASSWORD>@something.hanacloud.ondemand.com:443?TLSServerName=something.hanacloud.ondemand.com`
4447
- [mysql](https://github.com/go-sql-driver/mysql) - `username:user_password@tcp(localhost:3306)/db_name`
@@ -48,7 +51,10 @@ The configuration supports the following top-level fields:
4851
- [sqlserver](https://github.com/microsoft/go-mssqldb) - `sqlserver://username:user_password@localhost:1433?database=db_name`
4952
- [tds](https://github.com/thda/tds) - `tds://username:user_password@localhost:5000/db_name`
5053

51-
- `queries`(required): A list of queries, where a query is a sql statement and one or more `logs` and/or `metrics` sections (details below).
54+
**Other configuration fields:**
55+
- `driver` (required): The name of the database driver: one of _postgres_, _mysql_, _snowflake_, _sqlserver_, _hdb_ (SAP
56+
HANA), _oracle_ (Oracle DB), _tds_ (SapASE/Sybase).
57+
- `queries` (required): A list of queries, where a query is a sql statement and one or more `logs` and/or `metrics` sections (details below).
5258
- `collection_interval`(optional): The time interval between query executions. Defaults to _10s_.
5359
- `storage` (optional, default `""`): The ID of a [storage][storage_extension] extension to be used to [track processed results](#tracking-processed-results).
5460
- `telemetry` (optional) Defines settings for the component's own telemetry - logs, metrics or traces.

0 commit comments

Comments
 (0)