-
Notifications
You must be signed in to change notification settings - Fork 70
fix(pkg/instrumentation/databasesql): traces drop server.address for standard DSN formats #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alaotach
wants to merge
5
commits into
open-telemetry:main
Choose a base branch
from
alaotach:fix/databasesql-dsn-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0642a68
fix(pkg/instrumentation/databasesql): traces drop server.address for …
alaotach 4c64234
databasesql: fix libpq IPv6 parsing and reuse dbclient build
alaotach 85ec3b9
Merge branch 'open-telemetry:main' into fix/databasesql-dsn-parsing
alaotach 269b97d
Merge branch 'main' into fix/databasesql-dsn-parsing
alaotach 788f30b
Merge branch 'main' into fix/databasesql-dsn-parsing
alaotach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,19 @@ package db | |
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
| nurl "net/url" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| var dbNamePattern = regexp.MustCompile(`(?i)(^|[\s;?&])(?:dbname|database)\s*=\s*('([^']*)'|"([^"]*)"|[^;\s&]+)`) | ||
|
|
||
| func ParseDbName(dsn string) string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add some unit tests to cover non-happy path scenarios? |
||
| if name := parseDBNameFromKeyValue(dsn); name != "" { | ||
| return name | ||
| } | ||
|
|
||
| var name string | ||
| var err error | ||
| for i := len(dsn) - 1; i >= 0; i-- { | ||
|
|
@@ -30,6 +38,30 @@ func ParseDbName(dsn string) string { | |
| return name | ||
| } | ||
|
|
||
| func parseDBNameFromKeyValue(dsn string) string { | ||
| match := dbNamePattern.FindStringSubmatch(dsn) | ||
| if len(match) == 0 { | ||
| return "" | ||
| } | ||
|
|
||
| value := strings.TrimSpace(match[2]) | ||
| if value == "" { | ||
| return "" | ||
| } | ||
|
|
||
| if len(value) >= 2 { | ||
| if (value[0] == '\'' && value[len(value)-1] == '\'') || (value[0] == '"' && value[len(value)-1] == '"') { | ||
| value = value[1 : len(value)-1] | ||
| } | ||
| } | ||
|
|
||
| name, err := nurl.PathUnescape(value) | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| return name | ||
| } | ||
|
|
||
| func parseDSN(driverName, dsn string) (addr string, err error) { | ||
| // TODO: need a more delegate DFA | ||
| switch driverName { | ||
|
|
@@ -53,6 +85,17 @@ func parseDSN(driverName, dsn string) (addr string, err error) { | |
| } | ||
|
|
||
| func parsePostgres(url string) (addr string, err error) { | ||
| if !strings.Contains(url, "://") { | ||
| host, port, ok := parseConnectionStringPair(url, []string{"host"}) | ||
| if !ok || host == "" { | ||
| return "", errors.New("invalid Postgres DSN") | ||
| } | ||
| if port == "" { | ||
| port = "5432" | ||
| } | ||
| return net.JoinHostPort(strings.Trim(host, "[]"), port), nil | ||
| } | ||
|
Comment on lines
+88
to
+97
|
||
|
|
||
| u, err := nurl.Parse(url) | ||
| if err != nil { | ||
| return "", err | ||
|
|
@@ -97,11 +140,49 @@ func parseMySQL(dsn string) (addr string, err error) { | |
| } | ||
| } | ||
| if i >= 0 && j > i { | ||
| return dsn[i+1 : j], nil | ||
| addr := dsn[i+1 : j] | ||
| if strings.TrimSpace(addr) == "" { | ||
| return "localhost:3306", nil | ||
| } | ||
| return addr, nil | ||
| } | ||
|
|
||
| if strings.Contains(dsn, "@/") || strings.HasPrefix(dsn, "/") { | ||
| return "localhost:3306", nil | ||
| } | ||
| return "", errors.New("invalid MySQL DSN") | ||
| } | ||
|
|
||
| func parseConnectionStringPair(dsn string, keys []string) (host string, port string, ok bool) { | ||
| pairs := strings.Fields(dsn) | ||
| var hostFound bool | ||
|
|
||
| for _, pair := range pairs { | ||
| parts := strings.SplitN(pair, "=", 2) | ||
| if len(parts) != 2 { | ||
| continue | ||
| } | ||
| key := strings.ToLower(strings.TrimSpace(parts[0])) | ||
| value := strings.Trim(strings.TrimSpace(parts[1]), `"'`) | ||
|
|
||
| for _, k := range keys { | ||
| if key == k { | ||
| host = value | ||
| hostFound = true | ||
| break | ||
| } | ||
| } | ||
| if key == "port" { | ||
| port = value | ||
| } | ||
| } | ||
|
|
||
| if !hostFound { | ||
| return "", "", false | ||
| } | ||
| return host, port, true | ||
| } | ||
|
|
||
| func parseClickHouse(dsn string) (addr string, err error) { | ||
| // ClickHouse DSN formats: | ||
| // tcp://host:port?database=dbname&username=user&password=pass | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,11 @@ var ( | |
|
|
||
| func init() { | ||
| sql.Register("testdb", &testDriver{}) | ||
| sql.Register("mysql", &testDriver{}) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the value of these new registrations in the test? |
||
| sql.Register("postgres", &testDriver{}) | ||
| sql.Register("postgresql", &testDriver{}) | ||
| sql.Register("mssql", &testDriver{}) | ||
| sql.Register("sqlserver", &testDriver{}) | ||
| } | ||
|
|
||
| func main() { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regex is too tricky, e.g. from
user:pass@tcp(host)/mydb?timeout=30s&database=othermatchesdatabase=otherwhat about making
parseDSNreturn also the dbname?