Skip to content
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

feature: Add .NET 8+ support for System.Data.Odbc #2948

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,8 @@
},
{
"packageName": "stackexchange.redis"
},
{
"packageName": "system.data.odbc"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ public interface IAttributeDefinitions
AttributeDefinition<string, string> MessagingDestinationPublishName { get; }
}


public class AttributeDefinitionService : ConfigurationBasedService, IAttributeDefinitionService
{
public IAttributeDefinitions AttributeDefs { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public static class StringSeparators
public const char PathSeparatorChar = '/';
public static readonly char[] PathSeparator = { PathSeparatorChar };

public const char BackslashChar = '\\';
public static readonly char[] Backslash = { BackslashChar };

public const char CommaChar = ',';
public static readonly char[] Comma = { CommaChar };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ private static IConnectionStringParser GetConnectionParser(DatastoreVendor vendo
return new IbmDb2ConnectionStringParser(connectionString);
case DatastoreVendor.Redis:
return new StackExchangeRedisConnectionStringParser(connectionString);
case DatastoreVendor.ODBC:
return new OdbcConnectionStringParser(connectionString);
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using NewRelic.Agent.Extensions.Providers.Wrapper;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using NewRelic.Agent.Helpers;

namespace NewRelic.Agent.Extensions.Parsing.ConnectionString
{
Expand Down Expand Up @@ -35,7 +35,7 @@ private string ParseHost()
var host = ConnectionStringParserHelper.GetKeyValuePair(_connectionStringBuilder, _hostKeys)?.Value;
if (host == null) return null;

var endOfHostname = host.IndexOf(':');
var endOfHostname = host.IndexOf(StringSeparators.ColonChar);
return endOfHostname == -1 ? host : host.Substring(0, endOfHostname);
}

Expand All @@ -44,8 +44,8 @@ private string ParsePortPathOrId()
var host = ConnectionStringParserHelper.GetKeyValuePair(_connectionStringBuilder, _hostKeys)?.Value;
if (host == null) return null;

if (host.Contains(':'))
return host.Substring(host.IndexOf(':') + 1);
if (host.Contains(StringSeparators.ColonChar))
return host.Substring(host.IndexOf(StringSeparators.ColonChar) + 1);

return "default";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using NewRelic.Agent.Extensions.Providers.Wrapper;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using NewRelic.Agent.Helpers;

namespace NewRelic.Agent.Extensions.Parsing.ConnectionString
{
Expand Down Expand Up @@ -38,8 +38,8 @@ private string ParseHost()
// Example of want we would need to process: win-database.pdx.vm.datanerd.us,1433\SQLEXPRESS
try
{
var splitIndex = host.IndexOf(',');
if (splitIndex == -1) splitIndex = host.IndexOf('\\');
var splitIndex = host.IndexOf(StringSeparators.CommaChar);
if (splitIndex == -1) splitIndex = host.IndexOf(StringSeparators.BackslashChar);
host = splitIndex == -1 ? host : host.Substring(0, splitIndex);
}
catch
Expand All @@ -56,11 +56,11 @@ private string ParsePortPathOrId()

try
{
if (portPathOrId.IndexOf(',') != -1)
if (portPathOrId.IndexOf(StringSeparators.CommaChar) != -1)
{
var startOfValue = portPathOrId.IndexOf(',') + 1;
var endOfValue = portPathOrId.Contains('\\')
? portPathOrId.IndexOf('\\')
var startOfValue = portPathOrId.IndexOf(StringSeparators.CommaChar) + 1;
var endOfValue = portPathOrId.Contains(StringSeparators.BackslashChar)
? portPathOrId.IndexOf(StringSeparators.BackslashChar)
: portPathOrId.Length;
return (startOfValue > 0) ? portPathOrId.Substring(startOfValue, endOfValue - startOfValue) : null;
}
Expand All @@ -80,9 +80,9 @@ private string ParseInstanceName()

try
{
if (instanceName.IndexOf('\\') != -1)
if (instanceName.IndexOf(StringSeparators.BackslashChar) != -1)
{
var startOfValue = instanceName.IndexOf('\\') + 1;
var startOfValue = instanceName.IndexOf(StringSeparators.BackslashChar) + 1;
var endOfValue = instanceName.Length;
return (startOfValue > 0) ? instanceName.Substring(startOfValue, endOfValue - startOfValue) : null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using NewRelic.Agent.Extensions.Providers.Wrapper;
using System.Collections.Generic;
using System.Data.Common;
using NewRelic.Agent.Helpers;

namespace NewRelic.Agent.Extensions.Parsing.ConnectionString
{
Expand All @@ -24,7 +24,7 @@ public ConnectionInfo GetConnectionInfo(string utilizationHostName)
{
var host = ConnectionStringParserHelper.GetKeyValuePair(_connectionStringBuilder, _hostKeys)?.Value;

var hasMultipleHosts = host != null && host.IndexOf(',') != -1;
var hasMultipleHosts = host != null && host.IndexOf(StringSeparators.CommaChar) != -1;
if (hasMultipleHosts)
host = null;
else if (host != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using NewRelic.Agent.Helpers;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;

namespace NewRelic.Agent.Extensions.Parsing.ConnectionString
{
public class OdbcConnectionStringParser : IConnectionStringParser
{
private static readonly List<string> _hostKeys = new List<string> { "server", "data source", "hostname" };
private static readonly List<string> _portKeys = new List<string> { "port" };
private static readonly List<string> _databaseNameKeys = new List<string> { "database" };

private readonly DbConnectionStringBuilder _connectionStringBuilder;

public OdbcConnectionStringParser(string connectionString)
{
_connectionStringBuilder = new DbConnectionStringBuilder { ConnectionString = connectionString };
}

public ConnectionInfo GetConnectionInfo(string utilizationHostName)
{
var host = ParseHost();
if (host != null) host = ConnectionStringParserHelper.NormalizeHostname(host, utilizationHostName);
var portPathOrId = ParsePortPathOrId();
var databaseName = ConnectionStringParserHelper.GetKeyValuePair(_connectionStringBuilder, _databaseNameKeys)?.Value;
var instanceName = ParseInstanceName();
return new ConnectionInfo(host, portPathOrId, databaseName, instanceName);
}

private string ParseHost()
{
var host = ConnectionStringParserHelper.GetKeyValuePair(_connectionStringBuilder, _hostKeys)?.Value;
if (host == null) return null;

// Example of want we would need to process: win-database.pdx.vm.datanerd.us,1433\SQLEXPRESS
try
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try/catch block seems unnecessary. Looks like the code is already handling all the edge cases that are possible.

{
var splitIndex = host.IndexOf(StringSeparators.CommaChar);
if (splitIndex == -1) splitIndex = host.IndexOf(StringSeparators.BackslashChar);
host = splitIndex == -1 ? host : host.Substring(0, splitIndex);
}
catch
{
return null;
}
var endOfHostname = host.IndexOf(StringSeparators.ColonChar);
return endOfHostname == -1 ? host : host.Substring(0, endOfHostname);
}

private string ParsePortPathOrId()
{
// Some ODBC drivers use the "port" key to specify the port number
var portPathOrId = ConnectionStringParserHelper.GetKeyValuePair(_connectionStringBuilder, _portKeys)?.Value;
if (!string.IsNullOrWhiteSpace(portPathOrId))
{
return portPathOrId;

}

// Some ODBC drivers include the port in the "server" or "data source" key
portPathOrId = ConnectionStringParserHelper.GetKeyValuePair(_connectionStringBuilder, _hostKeys)?.Value;
if (portPathOrId == null) return null;

try
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here. I'm not seeing any execution path where an exception could be thrown.

{
if (portPathOrId.IndexOf(StringSeparators.ColonChar) != -1)
{
var startOfValue = portPathOrId.IndexOf(StringSeparators.ColonChar) + 1;
var endOfValue = portPathOrId.Length;
return (startOfValue > 0) ? portPathOrId.Substring(startOfValue, endOfValue - startOfValue) : null;
}
if (portPathOrId.IndexOf(StringSeparators.CommaChar) != -1)
{
var startOfValue = portPathOrId.IndexOf(StringSeparators.CommaChar) + 1;
var endOfValue = portPathOrId.Contains(StringSeparators.BackslashChar)
? portPathOrId.IndexOf(StringSeparators.BackslashChar)
: portPathOrId.Length;
return (startOfValue > 0) ? portPathOrId.Substring(startOfValue, endOfValue - startOfValue) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ternary here (and on line 74 above) isn't necessary -- startOfValue will always be > 0.

}
}
catch
{
return null;
}

return "default";
}

private string ParseInstanceName()
{
var instanceName = ConnectionStringParserHelper.GetKeyValuePair(_connectionStringBuilder, _hostKeys)?.Value;
if (instanceName == null) return null;

try
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary try/catch here also

{
if (instanceName.IndexOf(StringSeparators.BackslashChar) != -1)
{
var startOfValue = instanceName.IndexOf(StringSeparators.BackslashChar) + 1;
var endOfValue = instanceName.Length;
return (startOfValue > 0) ? instanceName.Substring(startOfValue, endOfValue - startOfValue) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ternary here is also unnecessary.

}
}
catch
{
return null;
}

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Data.Common;
using System.Linq;
using NewRelic.Agent.Helpers;
using NewRelic.Agent.Extensions.Providers.Wrapper;

namespace NewRelic.Agent.Extensions.Parsing.ConnectionString
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

using NewRelic.Agent.Extensions.Providers.Wrapper;
using System.Collections.Generic;
using System.Data.Common;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Linq;
using NewRelic.Agent.Helpers;
using NewRelic.Agent.Extensions.Providers.Wrapper;

namespace NewRelic.Agent.Extensions.Parsing.ConnectionString
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Globalization;
using System.Text.RegularExpressions;
#if NETFRAMEWORK
using System.Data.Odbc;
using System.Data.OleDb;
#endif
using NewRelic.Agent.Api;
Expand All @@ -19,6 +20,9 @@ public static class SqlWrapperHelper
{
private const string NullQueryParameterValue = "Null";

private static Regex _getDriverFromConnectionStringRegex = new Regex(@"DRIVER\=\{(.+?)\}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
private static ConcurrentDictionary<string, DatastoreVendor> _vendorNameCache = new ConcurrentDictionary<string, DatastoreVendor>();

/// <summary>
/// Gets the name of the datastore being used by a dbCommand.
/// </summary>
Expand All @@ -29,20 +33,34 @@ public static DatastoreVendor GetVendorName(IDbCommand command)
{

#if NETFRAMEWORK
// If this is an OdbcCommand, the only way to give the data store name is by looking at the connection driver

var odbcCommand = command as OdbcCommand;
if (odbcCommand != null && odbcCommand.Connection != null)
return ExtractVendorNameFromString(odbcCommand.Connection.Driver);
// If this is an OleDbCommand, the only way to give the data store name is by looking at the connection provider
var oleCommand = command as OleDbCommand;
if (oleCommand != null && oleCommand.Connection != null)
return ExtractVendorNameFromString(oleCommand.Connection.Provider);

// If this is an OleDbCommand, the only way to give the data store name is by looking at the connection provider
var oleCommand = command as OleDbCommand;
if (oleCommand != null && oleCommand.Connection != null)
return ExtractVendorNameFromString(oleCommand.Connection.Provider);
#endif
return GetVendorName(command.GetType().Name);
}

public static DatastoreVendor GetVendorNameFromOdbcConnectionString(string connectionString)
{
// Example connection string: DRIVER={SQL Server Native Client 11.0};Server=127.0.0.1;Database=NewRelic;Trusted_Connection=no;UID=sa;PWD=password;Encrypt=no;
if (_vendorNameCache.TryGetValue(connectionString, out DatastoreVendor vendor))
{
return vendor;
}

var match = _getDriverFromConnectionStringRegex.Match(connectionString);
if (match.Success)
{
var driver = match.Groups[1].Value;
vendor = ExtractVendorNameFromString(driver);
_vendorNameCache[connectionString] = vendor;
return vendor;
}
return DatastoreVendor.ODBC;
}

public static DatastoreVendor GetVendorName(string typeName)
{

Expand All @@ -61,7 +79,7 @@ public static DatastoreVendor GetVendorName(string typeName)
{ "OracleCommand", DatastoreVendor.Oracle },
{ "OracleDatabase", DatastoreVendor.Oracle },
{ "NpgsqlCommand", DatastoreVendor.Postgres },
{ "DB2Command", DatastoreVendor.IBMDB2 },
{ "DB2Command", DatastoreVendor.IBMDB2 }
};

/// <summary>
Expand All @@ -71,6 +89,9 @@ public static DatastoreVendor GetVendorName(string typeName)
/// <returns></returns>
private static DatastoreVendor ExtractVendorNameFromString(string text)
{
// Note that, because of the ordering of the following checks, an ODBC connection string for a known DB vendor should result in that vendor
// being returned, instead of ODBC
// example: Driver={ODBC Driver 17 for SQL Server};Server=myServerAddress;Database=myDataBase;
if (text.IndexOf("SQL Server", StringComparison.OrdinalIgnoreCase) != -1 || text.IndexOf("SQLServer", StringComparison.OrdinalIgnoreCase) != -1)
return DatastoreVendor.MSSQL;

Expand All @@ -86,6 +107,11 @@ private static DatastoreVendor ExtractVendorNameFromString(string text)
if (text.IndexOf("DB2", StringComparison.OrdinalIgnoreCase) != -1 || text.IndexOf("IBM", StringComparison.OrdinalIgnoreCase) != -1)
return DatastoreVendor.IBMDB2;

// This works for redshift since the driver reports as "Amazon Redshift (x64)"
// Other drivers may not report as expected
if (text.IndexOf("ODBC", StringComparison.OrdinalIgnoreCase) != -1 || text.IndexOf("Redshift", StringComparison.OrdinalIgnoreCase) != -1)
return DatastoreVendor.ODBC;

return DatastoreVendor.Other;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public enum DatastoreVendor
CosmosDB,
Elasticsearch,
DynamoDB,
ODBC,
Other
}

Expand Down
Loading
Loading