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

Remove references to System.Data.Odbc from wrapper and extensions #2928

Merged
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 @@ -15,7 +15,6 @@

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
<PackageReference Include="System.Data.Odbc" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// 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.Data.Odbc;
using System.Text.RegularExpressions;
#if NETFRAMEWORK
using System.Data.OleDb;
#endif
Expand All @@ -19,6 +20,9 @@ public static class SqlWrapperHelper
{
private const string NullQueryParameterValue = "Null";

private static Regex _getDriverFromConnectionStringRegex = new Regex(@"DRIVER\=\{(.+?)\}");
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 @@ -28,21 +32,35 @@ public static class SqlWrapperHelper
public static DatastoreVendor GetVendorName(IDbCommand command)
{

// 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 NETFRAMEWORK
// 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 (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=MssqlPassw0rd;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; // or maybe Other?
}

public static DatastoreVendor GetVendorName(string typeName)
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,47 @@
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Data.Odbc;
using NewRelic.Agent.Api;
using NewRelic.Agent.Extensions.Providers.Wrapper;
using NewRelic.Agent.Extensions.Parsing;
using NewRelic.Agent.Extensions.Parsing.ConnectionString;
using System.Data;

namespace NewRelic.Providers.Wrapper.Sql
{
public class OdbcCommandWrapper : IWrapper
{
public const string WrapperName = "OdbcCommandTracer";
public bool IsTransactionRequired => true;
public class OdbcCommandWrapper : IWrapper
{
public const string WrapperName = "OdbcCommandTracer";
public bool IsTransactionRequired => true;

public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo)
{
return new CanWrapResponse(methodInfo.RequestedWrapperName.Equals(WrapperName, StringComparison.OrdinalIgnoreCase));
}
public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo)
{
return new CanWrapResponse(methodInfo.RequestedWrapperName.Equals(WrapperName, StringComparison.OrdinalIgnoreCase));
}

public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
{
{
var odbcCommand = (OdbcCommand)instrumentedMethodCall.MethodCall.InvocationTarget;
if (odbcCommand == null)
return Delegates.NoOp;
public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
{
{
if (instrumentedMethodCall.MethodCall.InvocationTarget is not IDbCommand odbcCommand)
{
return Delegates.NoOp;
}

var sql = odbcCommand.CommandText ?? string.Empty;
var vendor = SqlWrapperHelper.GetVendorName(odbcCommand);
var sql = odbcCommand.CommandText;

object GetConnectionInfo() => ConnectionInfoParser.FromConnectionString(vendor, odbcCommand.Connection.ConnectionString, agent.Configuration.UtilizationHostName);
var connectionInfo = (ConnectionInfo)transaction.GetOrSetValueFromCache(odbcCommand.Connection.ConnectionString, GetConnectionInfo);
var vendor = SqlWrapperHelper.GetVendorNameFromOdbcConnectionString(odbcCommand.Connection.ConnectionString);

var parsedStatement = transaction.GetParsedDatabaseStatement(vendor, odbcCommand.CommandType, sql);
object GetConnectionInfo() => ConnectionInfoParser.FromConnectionString(vendor, odbcCommand.Connection.ConnectionString, agent.Configuration.UtilizationHostName);
var connectionInfo = (ConnectionInfo)transaction.GetOrSetValueFromCache(odbcCommand.Connection.ConnectionString, GetConnectionInfo);

var queryParameters = SqlWrapperHelper.GetQueryParameters(odbcCommand, agent);
var parsedStatement = transaction.GetParsedDatabaseStatement(vendor, odbcCommand.CommandType, sql);

var segment = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall, parsedStatement, connectionInfo, sql, queryParameters);
var queryParameters = SqlWrapperHelper.GetQueryParameters(odbcCommand, agent);

return Delegates.GetDelegateFor(segment);
}
}
}
var segment = transaction.StartDatastoreSegment(instrumentedMethodCall.MethodCall, parsedStatement, connectionInfo, sql, queryParameters);

return Delegates.GetDelegateFor(segment);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Data.Odbc" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\NewRelic.Agent.Extensions\NewRelic.Agent.Extensions.csproj" />
</ItemGroup>
Expand Down
Loading