Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .chloggen/sqlserver-receiver-host-extraction.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. receiver/filelog)
component: receiver/sqlserver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Fixed issue where Host variable was not being set by msdsn.Parse() in certain connection string formats. Added fallback mechanism to manually extract host from connection strings when the primary parser fails to set the Host field."

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

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
The parseDataSource() function now checks if the Host field is empty after parsing and falls back to manual extraction using extractHostFromDataSource(). This handles cases where msdsn.Parse() from the go-mssqldb library doesn't properly set the Host field for certain DSN formats. The fallback supports URL-style (sqlserver://...), ADO-style (server=...), and ODBC-style connection strings.

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]

83 changes: 82 additions & 1 deletion receiver/sqlserverreceiver/service_instance_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@

// parseDataSource extracts server and port from SQL Server connection string
// Uses the microsoft/go-mssqldb library's built-in parser for accurate parsing
// Falls back to manual extraction if the Host field is not set by the parser
func parseDataSource(dataSource string) (string, int, error) {
if dataSource == "" {
return "", 0, errors.New("datasource is empty")
Expand All @@ -78,11 +79,91 @@
return "", 0, fmt.Errorf("failed to parse datasource: %w", err)
}

host := config.Host

// Fallback: if Host is not set by the parser, extract it manually
if host == "" {
host = extractHostFromDataSource(dataSource)
}

// Apply default port if not specified
port := int(config.Port)
if port == 0 {
port = defaultSQLServerPort
}

return config.Host, port, nil
return host, port, nil
}

// extractHostFromDataSource manually extracts the host/server from various DSN formats
// Handles ADO-style, ODBC-style, and URL-style connection strings
func extractHostFromDataSource(dataSource string) string {
// Try URL format first (sqlserver://user:password@host:port)
if strings.Contains(dataSource, "://") {
parts := strings.SplitN(dataSource, "://", 2)
if len(parts) == 2 {
// Extract authority part (everything before next /)
authority := parts[1]
if idx := strings.Index(authority, "/"); idx != -1 {
authority = authority[:idx]
}
// Remove user:password@ prefix
if idx := strings.LastIndex(authority, "@"); idx != -1 {
authority = authority[idx+1:]
}
// Remove port suffix if present
if idx := strings.LastIndex(authority, ":"); idx != -1 {
// Check if what follows is a port (digits)
potential := authority[idx+1:]
if len(potential) > 0 && isNumeric(potential) {

Check failure on line 118 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / scoped-tests-matrix (windows-2025)

emptyStringTest: replace `len(potential) > 0` with `potential != ""` (gocritic)

Check failure on line 118 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / lint-matrix (windows, receiver-3)

emptyStringTest: replace `len(potential) > 0` with `potential != ""` (gocritic)

Check failure on line 118 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / lint-matrix (linux, receiver-3)

emptyStringTest: replace `len(potential) > 0` with `potential != ""` (gocritic)
authority = authority[:idx]
}
}
if authority != "" {
return authority
}
}
}

// Try ADO/ODBC format (key=value;key=value)
// Look for 'server', 'data source', 'address', or 'network address' keys
parts := strings.Split(dataSource, ";")

Check failure on line 130 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / scoped-tests-matrix (windows-2025)

stringsseq: Ranging over SplitSeq is more efficient (modernize)

Check failure on line 130 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / lint-matrix (windows, receiver-3)

stringsseq: Ranging over SplitSeq is more efficient (modernize)

Check failure on line 130 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / lint-matrix (linux, receiver-3)

stringsseq: Ranging over SplitSeq is more efficient (modernize)
for _, part := range parts {
kv := strings.SplitN(part, "=", 2)
if len(kv) == 2 {
key := strings.TrimSpace(strings.ToLower(kv[0]))
value := strings.TrimSpace(kv[1])

// Remove quotes if present
if len(value) >= 2 && strings.HasPrefix(value, "\"") && strings.HasSuffix(value, "\"") {
value = value[1 : len(value)-1]
}

if key == "server" || key == "data source" || key == "address" || key == "network address" {
// Extract just the hostname (without port)
if idx := strings.LastIndex(value, ":"); idx != -1 {
// Check if what follows is a port (digits)
potential := value[idx+1:]
if len(potential) > 0 && isNumeric(potential) {

Check failure on line 147 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / scoped-tests-matrix (windows-2025)

emptyStringTest: replace `len(potential) > 0` with `potential != ""` (gocritic)

Check failure on line 147 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / lint-matrix (windows, receiver-3)

emptyStringTest: replace `len(potential) > 0` with `potential != ""` (gocritic)

Check failure on line 147 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / lint-matrix (linux, receiver-3)

emptyStringTest: replace `len(potential) > 0` with `potential != ""` (gocritic)
value = value[:idx]
}
}
if value != "" {
return value
}
}
}
}

return ""
}

// isNumeric checks if a string contains only digits
func isNumeric(s string) bool {
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return len(s) > 0

Check failure on line 168 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / scoped-tests-matrix (windows-2025)

emptyStringTest: replace `len(s) > 0` with `s != ""` (gocritic)

Check failure on line 168 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / lint-matrix (windows, receiver-3)

emptyStringTest: replace `len(s) > 0` with `s != ""` (gocritic)

Check failure on line 168 in receiver/sqlserverreceiver/service_instance_id.go

View workflow job for this annotation

GitHub Actions / lint-matrix (linux, receiver-3)

emptyStringTest: replace `len(s) > 0` with `s != ""` (gocritic)
}
Loading
Loading