Skip to content

Conversation

XSAM
Copy link
Member

@XSAM XSAM commented Aug 27, 2025

Description

Adds service.instance.id resource attribute to all metrics and logs emitted by the SQL Server receiver. The attribute format is <host>:<port> which provides a unique identifier for each SQL Server instance.

Key implementation details:

  • Compute service instance ID once during scraper initialization for performance
  • Handle various connection string formats using github.com/microsoft/go-mssqldb library's msdsn.Parse()
  • Replace localhost/127.0.0.1 with actual hostname from os.Hostname()
  • Set default port to 1433 when not specified
  • Instance name is NOT included in service.instance.id (already captured in sqlserver.instance.name)

Link to tracking issue

Fixes #41894

Testing

  • Added comprehensive unit tests for service instance ID computation covering all edge cases
  • Added integration tests verifying the attribute is set correctly for both metrics and logs
  • Tests cover various configuration scenarios:
    • Explicit server and port configuration
    • DataSource connection strings with different formats
    • Localhost replacement scenarios
    • Default port handling
    • Named instances

Documentation

  • Updated metadata.yaml to include the new service.instance.id resource attribute
  • Added inline documentation for the computeServiceInstanceID function

XSAM added 2 commits August 27, 2025 10:43
Adds service.instance.id to all metrics and logs emitted by the SQL Server receiver.
Format: <host>:<port> where port defaults to 1433.
Special handling for localhost/127.0.0.1 which are replaced with os.Hostname().

The service instance ID is computed once during scraper initialization from:
1. DataSource (if provided) - parsed to extract server and port
2. Server/Port config (if provided)
3. os.Hostname():1433 (fallback)

Fixes open-telemetry#41894
- Replace manual regex-based parsing with msdsn.Parse() from github.com/microsoft/go-mssqldb
- Simplify parseDataSource() function significantly
- Update tests to align with msdsn library behavior
- Add isLocalhost() helper function for clarity
- Merge metrics and logs tests in TestScraperWithServiceInstanceID using subtests

The go-mssqldb library provides more robust and accurate parsing of SQL Server
connection strings, handling various formats automatically.
@XSAM XSAM requested review from a team and crobert-1 as code owners August 27, 2025 17:49
@github-actions github-actions bot added receiver/sqlserver Run Windows Enable running windows test on a PR labels Aug 27, 2025
@github-actions github-actions bot requested a review from sincejune August 27, 2025 17:50
@XSAM XSAM changed the title [receiver/sqlserver] Add service.instance.id resource attribute [receiver/sqlserver] Add service.instance.id resource attribute Aug 27, 2025
@XSAM
Copy link
Member Author

XSAM commented Aug 27, 2025

The failed e2e-tests-windows seems unrelated to this PR.

@XSAM XSAM requested a review from crobert-1 September 2, 2025 22:43
@atoulme atoulme merged commit f8c51e9 into open-telemetry:main Sep 3, 2025
217 of 219 checks passed
@github-actions github-actions bot added this to the next release milestone Sep 3, 2025
@XSAM XSAM deleted the feature/41894-sqlserver-service-instance-id branch September 4, 2025 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

receiver/sqlserver Run Windows Enable running windows test on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[receiver/sqlserver] Add service.instance.id resource attribute to metrics and logs

5 participants