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

Merged
merged 22 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
07c966d
Working AWS Redshift PoC.
jaffinito Dec 6, 2024
c16edf3
Correct attribute name for Redshift server address
nr-ahemsath Dec 10, 2024
651c811
Remove new attribute definition that turned out to be unnecessary
nr-ahemsath Dec 10, 2024
2a36a03
Merge branch 'main' into feature/netstandard-odbc-instrumentation
nr-ahemsath Dec 16, 2024
644555a
Remove references to System.Data.Odbc from wrapper and extensions (#2…
nr-ahemsath Dec 17, 2024
e353db6
ODBC integration tests (#2941)
nr-ahemsath Jan 13, 2025
af4be95
Merge branch 'main' into feature/netstandard-odbc-instrumentation
nr-ahemsath Jan 14, 2025
33dac84
Add unit tests for OdbcConnectionStringParser
nr-ahemsath Jan 14, 2025
e45ad0d
Refactoring and cleanup
nr-ahemsath Jan 14, 2025
54736ec
Merge branch 'main' into feature/netstandard-odbc-instrumentation
nr-ahemsath Jan 15, 2025
7d79957
Increase test coverage
nr-ahemsath Jan 15, 2025
2665f28
Fix unit test bugs
nr-ahemsath Jan 15, 2025
9528a60
Merge branch 'main' into feature/netstandard-odbc-instrumentation
nr-ahemsath Jan 15, 2025
3168149
Remove cruft
nr-ahemsath Jan 15, 2025
82aaafe
One more test case to increase coverage
nr-ahemsath Jan 15, 2025
7ac9299
Merge branch 'main' into feature/netstandard-odbc-instrumentation
nr-ahemsath Jan 16, 2025
235bbb3
Add system.data.odbc to dotty
nr-ahemsath Jan 16, 2025
cecbecf
Merge branch 'main' into feature/netstandard-odbc-instrumentation
nr-ahemsath Jan 22, 2025
4d13184
Remove unnecessary try/catch and guard logic
nr-ahemsath Jan 22, 2025
4367887
Merge branch 'main' into feature/netstandard-odbc-instrumentation
nr-ahemsath Jan 22, 2025
2ad2d4a
Merge branch 'main' into feature/netstandard-odbc-instrumentation
nr-ahemsath Jan 24, 2025
c929702
Merge branch 'main' into feature/netstandard-odbc-instrumentation
nr-ahemsath Jan 24, 2025
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 @@ -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
nr-ahemsath marked this conversation as resolved.
Show resolved Hide resolved
{
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
nr-ahemsath marked this conversation as resolved.
Show resolved Hide resolved
{
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;
nr-ahemsath marked this conversation as resolved.
Show resolved Hide resolved
}
}
catch
{
return null;
}

return "default";
}

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

try
nr-ahemsath marked this conversation as resolved.
Show resolved Hide resolved
{
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;
nr-ahemsath marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ SPDX-License-Identifier: Apache-2.0
<exactMethodMatcher methodName="ExecuteNonQuery" />
<exactMethodMatcher methodName="ExecuteScalar" />
</match>

<!-- .NET Core ODBC driver -->
<match assemblyName="System.Data.Odbc" className="System.Data.Odbc.OdbcCommand">
<exactMethodMatcher methodName="ExecuteReader" parameters="System.Data.CommandBehavior" />
<exactMethodMatcher methodName="ExecuteNonQuery" />
<exactMethodMatcher methodName="ExecuteScalar" />
</match>
</tracerFactory>

<tracerFactory name="OleDbCommandTracer">
Expand Down
Loading
Loading