Skip to content

Commit fa3c82d

Browse files
authored
[receiver/sqlserver] Enable direct connection to SQL Server instances (#31915)
**Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> This PR enables direct connection to SQL Server instances, and also allows the SQL Server receiver to run on non-Windows platforms. User-facing changes: 1. More (optional) configuration options. If the receiver wants to connect directly to the SQL Server instance (rather than relying on Windows Performance Counters), all of these options must be specified. 2. Add a new metric, `sqlserver.database_io.read_latency`. This is disabled by default, and way randomly chosen as a single metric to try to restrict this PR's size. I plan on following this PR up with more metrics from the same query, as well as more queries and metrics. Note: Existing users should see no change in functionality with this PR. Using existing configurations with this PR should have no performance or functional impact. **Link to tracking Issue:** <Issue number if applicable> Resolves #30297 **Testing:** <Describe what testing was performed and which tests were added.> Tested on MacOS and Windows. Manual tested with direct connection to a docker container on both platforms, as well as ran all tests in package. Everything is passing. **Working demo for testing:** Docker container start command: ``` docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=StrongPassw0rd" -p 1433:1433 -d mcr.microsoft.com/mssql/server:2022-latest ``` Collector config: ``` sqlserver: password: "StrongPassw0rd" port: 1433 server: 0.0.0.0 username: sa resource_attributes: sqlserver.instance.name: enabled: true sqlserver.computer.name: enabled: true metrics: sqlserver.database_io.read_latency: enabled: true ``` **Documentation:** <Describe the documentation added.> Updated README
1 parent 003b0d0 commit fa3c82d

15 files changed

+446
-13
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: sqlserverreceiver
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Enable direct connection to SQL Server
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [30297]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
Directly connecting to SQL Server will enable the receiver to gather more metrics
20+
for observing the SQL Server instance. The first metric added with this update is
21+
`sqlserver.database.io.read_latency`.
22+
23+
# If your change doesn't affect end users or the exported elements of any package,
24+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
25+
# Optional: The change log or logs in which this entry should be included.
26+
# e.g. '[user]' or '[user, api]'
27+
# Include 'user' if the change is relevant to end users.
28+
# Include 'api' if there is a change to a library API.
29+
# Default: '[user]'
30+
change_logs: []

receiver/sqlserverreceiver/README.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,45 @@
1313
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib
1414
<!-- end autogenerated section -->
1515

16-
The `sqlserver` receiver grabs metrics about a Microsoft SQL Server instance using the Windows Performance Counters.
17-
Because of this, it is a Windows only receiver.
16+
The `sqlserver` receiver grabs metrics about a Microsoft SQL Server instance. The receiver works by either using the
17+
Windows Performance Counters, or by directly connecting to the instance and querying it. Windows Performance Counters
18+
are only available when running on Windows.
1819

1920
Make sure to run the collector as administrator in order to collect all performance counters for metrics.
2021

2122
## Configuration
2223

2324
The following settings are optional:
2425
- `collection_interval` (default = `10s`): The internal at which metrics should be emitted by this receiver.
26+
- `instance_name` (optional): The instance name identifies the specific SQL Server instance being monitored.
27+
If unspecified, metrics will be scraped from all instances. If configured, the `computer_name` must also be set
28+
when running on Windows.
29+
30+
Direct connection options (optional, but all must be specified to enable):
31+
- `username`: The username used to connect to the SQL Server instance.
32+
- `password`: The password used to connect to the SQL Server instance.
33+
- `server`: IP Address or hostname of SQL Server instance to connect to.
34+
- `port`: Port of the SQL Server instance to connect to.
2535

26-
To collect from a SQL Server with a named instance, both `computer_name` and `instance_name` are required. For a default SQL Server setup, these settings are optional.
36+
Windows-specific options:
2737
- `computer_name` (optional): The computer name identifies the SQL Server name or IP address of the computer being monitored.
28-
- `instance_name` (optional): The instance name identifies the specific SQL Server instance being monitored.
38+
If specified, `instance_name` is also required to be defined. This option is ignored in non-Windows environments.
2939

3040
Example:
3141

3242
```yaml
3343
receivers:
3444
sqlserver:
3545
collection_interval: 10s
46+
sqlserver/1:
47+
collection_interval: 5s
48+
username: sa
49+
password: securepassword
50+
server: 0.0.0.0
51+
port: 1433
3652
```
3753
38-
When a named instance is used, a computer name and a instance name must be specified.
54+
When a named instance is used on Windows, a computer name and a instance name must be specified.
3955
Example with named instance:
4056
4157
```yaml

receiver/sqlserverreceiver/documentation.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,34 @@ Number of users connected to the SQL Server.
178178
| ---- | ----------- | ---------- |
179179
| {connections} | Gauge | Int |
180180
181+
## Optional Metrics
182+
183+
The following metrics are not emitted by default. Each of them can be enabled by applying the following configuration:
184+
185+
```yaml
186+
metrics:
187+
<metric_name>:
188+
enabled: true
189+
```
190+
191+
### sqlserver.database.io.read_latency
192+
193+
Total time that the users waited for reads issued on this file.
194+
195+
This metric is only available when the receiver is configured to directly connect to SQL Server.
196+
197+
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
198+
| ---- | ----------- | ---------- | ----------------------- | --------- |
199+
| s | Sum | Double | Cumulative | true |
200+
201+
#### Attributes
202+
203+
| Name | Description | Values |
204+
| ---- | ----------- | ------ |
205+
| physical_filename | The physical filename of the file being monitored. | Any Str |
206+
| logical_filename | The logical filename of the file being monitored. | Any Str |
207+
| file_type | The type of file being monitored. | Any Str |
208+
181209
## Resource Attributes
182210
183211
| Name | Description | Values | Enabled |

receiver/sqlserverreceiver/factory.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ func createDefaultConfig() component.Config {
3838

3939
func setupQueries(cfg *Config) []string {
4040
var queries []string
41-
// TODO: Only add query if metrics are enabled
42-
queries = append(queries, getSQLServerDatabaseIOQuery(cfg.InstanceName))
41+
42+
if cfg.MetricsBuilderConfig.Metrics.SqlserverDatabaseIoReadLatency.Enabled {
43+
queries = append(queries, getSQLServerDatabaseIOQuery(cfg.InstanceName))
44+
}
45+
4346
return queries
4447
}
4548

receiver/sqlserverreceiver/factory_others_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func TestCreateMetricsReceiverOtherOS(t *testing.T) {
3030
cfg.Server = "0.0.0.0"
3131
cfg.Port = 1433
3232
cfg.InstanceName = "instanceName"
33+
cfg.Metrics.SqlserverDatabaseIoReadLatency.Enabled = true
3334
require.NoError(t, cfg.Validate())
3435

3536
require.True(t, directDBConnectionEnabled(cfg))

receiver/sqlserverreceiver/factory_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func TestCreateMetricsReceiver(t *testing.T) {
8787
cfg.Server = "0.0.0.0"
8888
cfg.Port = 1433
8989
require.NoError(t, cfg.Validate())
90+
cfg.Metrics.SqlserverDatabaseIoReadLatency.Enabled = true
9091

9192
require.True(t, directDBConnectionEnabled(cfg))
9293
require.Equal(t, "server=0.0.0.0;user id=sa;password=password;port=1433", getDBConnectionString(cfg))

receiver/sqlserverreceiver/internal/metadata/generated_config.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/sqlserverreceiver/internal/metadata/generated_config_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/sqlserverreceiver/internal/metadata/generated_metrics.go

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/sqlserverreceiver/internal/metadata/generated_metrics_test.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)