Skip to content

Commit

Permalink
Protect the connection string tags extractor from an invalid connecti…
Browse files Browse the repository at this point in the history
…on string (#5956 -> v2) (#5960)

## Summary of changes

This PR fixes an scenario when the connection string could not be parsed
by avoiding the exception.

## Reason for change

Seen in Error tracking:

<img width="1127" alt="image"

src="https://github.com/user-attachments/assets/436de8fe-b6b8-43d7-a894-5554bb4e04eb">

## Implementation details

Wrap the method in a try catch block, and return default on that invalid
connection string.
The caller method already returns `default` in other cases.

## Test coverage

This is an small fix, I didn't write a test case for this... yet.

## Other details

Backport of #5956

Co-authored-by: Andrew Lock <[email protected]>
  • Loading branch information
tonyredondo and andrewlock authored Aug 29, 2024
1 parent 466b242 commit 58ba195
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions tracer/src/Datadog.Trace/Util/DbCommandCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,23 @@ public static TagsCacheItem GetTagsFromDbCommand(IDbCommand command)

private static TagsCacheItem ExtractTagsFromConnectionString(string connectionString)
{
// Parse the connection string
var builder = new DbConnectionStringBuilder { ConnectionString = connectionString };

return new TagsCacheItem(
dbName: GetConnectionStringValue(builder, "Database", "Initial Catalog", "InitialCatalog"),
dbUser: GetConnectionStringValue(builder, "User ID", "UserID"),
outHost: GetConnectionStringValue(builder, "Server", "Data Source", "DataSource", "Network Address", "NetworkAddress", "Address", "Addr", "Host"));
try
{
// Parse the connection string
var builder = new DbConnectionStringBuilder { ConnectionString = connectionString };

// Extract the tags
return new TagsCacheItem(
dbName: GetConnectionStringValue(builder, "Database", "Initial Catalog", "InitialCatalog"),
dbUser: GetConnectionStringValue(builder, "User ID", "UserID"),
outHost: GetConnectionStringValue(builder, "Server", "Data Source", "DataSource", "Network Address", "NetworkAddress", "Address", "Addr", "Host"));
}
catch (Exception)
{
// DbConnectionStringBuilder can throw exceptions if the connection string is invalid
// in this case we should not use the connection string and just return default
return default;
}
}

private static string? GetConnectionStringValue(DbConnectionStringBuilder builder, params string[] names)
Expand Down

0 comments on commit 58ba195

Please sign in to comment.