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

feat: Instrument OpenAsync() for SQL libraries #1725

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions src/Agent/NewRelic/Agent/Core/Utilities/ExtensionsLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public static void Initialize(string installPathExtensionsDirectory)
{ "DataReaderWrapperAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },

{ "OpenConnectionTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "OpenConnectionTracerAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "OpenConnectionWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "OpenConnectionWrapperAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },

//The NewRelic.Providers.Wrapper.SerilogLogging.dll depends on the Serilog.dll; therefore, it should
//only be loaded by the agent when Serilog is used otherwise assembly load exception will occur.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ SPDX-License-Identifier: Apache-2.0
<match assemblyName="MySql.Data" className="MySql.Data.MySqlClient.MySqlConnection">
<exactMethodMatcher methodName="Open"/>
</match>

<!-- Up until 8.0.33, OpenAsync was not actually async -->
<match assemblyName="MySql.Data" className="MySql.Data.MySqlClient.MySqlConnection" maxVersion="8.0.33">
<exactMethodMatcher methodName="OpenAsync"/>
</match>

<!-- MySqlConnector 0.x -->
<match assemblyName="MySqlConnector" className="MySql.Data.MySqlClient.MySqlConnection">
<exactMethodMatcher methodName="Open" />
Expand All @@ -390,5 +394,60 @@ SPDX-License-Identifier: Apache-2.0
</match>
</tracerFactory>

<!-- DbConnection.OpenAsync() -->
<tracerFactory name="OpenConnectionTracerAsync">
<!-- built in MS SQL driver (framework) -->
<match assemblyName="System.Data" className="System.Data.SqlClient.SqlConnection">
<exactMethodMatcher methodName="OpenAsync" parameters="System.Threading.CancellationToken" />
</match>

<!-- built in MS SQL driver (core / nuget) -->
<match assemblyName="System.Data.SqlClient" className="System.Data.SqlClient.SqlConnection">
<exactMethodMatcher methodName="OpenAsync" parameters="System.Threading.CancellationToken" />
</match>

<!-- MS SQL flagship data access driver -->
<match assemblyName="Microsoft.Data.SqlClient" className="Microsoft.Data.SqlClient.SqlConnection">
<exactMethodMatcher methodName="OpenAsync" parameters="System.Threading.CancellationToken" />
</match>

<!-- Oracle vendor driver -->
<match assemblyName="Oracle.DataAccess" className="Oracle.DataAccess.Client.OracleConnection">
<exactMethodMatcher methodName="OpenAsync"/>
</match>

<!-- Oracle vendor driver for ManagedDataAccess -->
<match assemblyName="Oracle.ManagedDataAccess" className="Oracle.ManagedDataAccess.Client.OracleConnection">
<exactMethodMatcher methodName="OpenAsync"/>
</match>

<!-- MySql (official) driver -->
<!-- Up until 8.0.33, OpenAsync was not actually async -->
<match assemblyName="MySql.Data" className="MySql.Data.MySqlClient.MySqlConnection" minVersion="8.0.33">
<exactMethodMatcher methodName="OpenAsync" parameters="System.Threading.CancellationToken"/>
</match>

<!-- MySqlConnector 0.x -->
<match assemblyName="MySqlConnector" className="MySql.Data.MySqlClient.MySqlConnection">
<exactMethodMatcher methodName="OpenAsync" parameters="System.Nullable`1[MySqlConnector.Protocol.Serialization.IOBehavior],System.Threading.CancellationToken" />
</match>

<!-- MySqlConnector 1.x -->
<match assemblyName="MySqlConnector" className="MySqlConnector.MySqlConnection">
<exactMethodMatcher methodName="OpenAsync" parameters="System.Nullable`1[MySqlConnector.Protocol.Serialization.IOBehavior],System.Threading.CancellationToken" />
</match>

<!-- IBM DB2 driver -->
<match assemblyName="IBM.Data.DB2" className="IBM.Data.DB2.DB2Connection">
<exactMethodMatcher methodName="OpenAsync" parameters="System.Threading.CancellationToken" />
</match>

<!-- Npgsql Postgres data provider -->
<match assemblyName="Npgsql" className="Npgsql.NpgsqlConnection">
<exactMethodMatcher methodName="OpenAsync" parameters="System.Threading.CancellationToken"/>
</match>
</tracerFactory>


</instrumentation>
</extension>
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,67 @@
using NewRelic.Agent.Extensions.Providers.Wrapper;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace NewRelic.Providers.Wrapper.Sql
{
public class OpenConnectionWrapper : IWrapper
public class OpenConnectionWrapper : OpenConnectionWrapperBase
{
public static readonly string[] WrapperNames =
private static readonly string[] _tracerNames =
{
"OpenConnectionTracer",
"OpenConnectionWrapper"
"OpenConnectionWrapper",
};

public override string[] WrapperNames => _tracerNames;
public override bool ExecuteAsAsync => false;
}

public class OpenConnectionAsyncWrapper : OpenConnectionWrapperBase
{
private static readonly string[] _tracerNames =
{
"OpenConnectionTracerAsync",
"OpenConnectionWrapperAsync"
};
public override string[] WrapperNames => _tracerNames;
public override bool ExecuteAsAsync => true;
}


public abstract class OpenConnectionWrapperBase : IWrapper
{
public abstract string[] WrapperNames { get; }

public abstract bool ExecuteAsAsync { get; }

public bool IsTransactionRequired => true;

public CanWrapResponse CanWrap(InstrumentedMethodInfo methodInfo)
{
return new CanWrapResponse(WrapperNames.Contains(methodInfo.RequestedWrapperName, StringComparer.OrdinalIgnoreCase));
var canWrap = WrapperNames.Contains(methodInfo.RequestedWrapperName, StringComparer.OrdinalIgnoreCase);
if (canWrap && ExecuteAsAsync)
{
var method = methodInfo.Method;
return TaskFriendlySyncContextValidator.CanWrapAsyncMethod(method.Type.Assembly.GetName().Name, method.Type.FullName, method.MethodName);
}

return new CanWrapResponse(canWrap);
}

public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall instrumentedMethodCall, IAgent agent, ITransaction transaction)
{
if (instrumentedMethodCall.IsAsync)
{
transaction.AttachToAsync();
}

var typeName = instrumentedMethodCall.MethodCall.Method.Type.FullName ?? "unknown";
var segment = transaction.StartMethodSegment(instrumentedMethodCall.MethodCall, typeName, instrumentedMethodCall.MethodCall.Method.MethodName, isLeaf: true);

return Delegates.GetDelegateFor(segment);
return ExecuteAsAsync
? Delegates.GetAsyncDelegateFor<Task>(agent, segment)
: Delegates.GetDelegateFor(segment);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public async Task<string> MsSqlAsync(string tableName)

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
connection.Open();
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
{
Expand Down Expand Up @@ -166,7 +166,7 @@ public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bo

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
connection.Open();
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task<string> MsSqlAsync(string tableName)

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
connection.Open();
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
{
Expand Down Expand Up @@ -169,7 +169,7 @@ public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bo

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
connection.Open();
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task<string> MsSqlAsync(string tableName)

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
connection.Open();
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
{
Expand Down Expand Up @@ -169,7 +169,7 @@ public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bo

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
connection.Open();
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private async Task<string> ExecuteCommandAsync(Func<MySqlCommand, Task<string>>
using (var connection = new MySqlConnection(MySqlTestConfiguration.MySqlConnectionString))
using (var command = new MySqlCommand("SELECT _date FROM dates WHERE _date LIKE '2%' ORDER BY _date DESC LIMIT 1", connection))
{
connection.Open();
await connection.OpenAsync();
result = await action(command);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task SingleDateQueryAsync()
using (var connection = new MySqlConnection(MySqlTestConfiguration.MySqlConnectionString))
using (var command = new MySqlCommand("SELECT _date FROM dates WHERE _date LIKE '2%' ORDER BY _date DESC LIMIT 10000", connection))
{
connection.Open();
await connection.OpenAsync();
using (var reader = await command.ExecuteReaderAsync())
{
while (await reader.ReadAsync())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task<string> OracleAsync(string tableName)

using (var connection = new OracleConnection(connectionString))
{
connection.Open();
await connection.OpenAsync();

using (var command = new OracleCommand("SELECT DEGREE FROM user_tables WHERE ROWNUM <= 1", connection))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public async Task<string> MsSqlAsync(string tableName)

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
connection.Open();
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = 'John'", connection))
{
Expand Down Expand Up @@ -153,7 +153,7 @@ public async Task<string> MsSqlAsync_WithParameterizedQuery(string tableName, bo

using (var connection = new SqlConnection(MsSqlConfiguration.MsSqlConnectionString))
{
connection.Open();
await connection.OpenAsync();

using (var command = new SqlCommand("SELECT * FROM NewRelic.dbo.TeamMembers WHERE FirstName = @FN", connection))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Test()
new Assertions.ExpectedMetric { metricName = @"Datastore/allOther", callCount = expectedDatastoreCallCount },
new Assertions.ExpectedMetric { metricName = @"Datastore/Postgres/all", callCount = expectedDatastoreCallCount },
new Assertions.ExpectedMetric { metricName = @"Datastore/Postgres/allOther", callCount = expectedDatastoreCallCount },
new Assertions.ExpectedMetric { metricName = @"DotNet/Npgsql.NpgsqlConnection/Open", callCount = 1},
new Assertions.ExpectedMetric { metricName = @"DotNet/Npgsql.NpgsqlConnection/OpenAsync", callCount = 1},
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/Postgres/{CommonUtils.NormalizeHostname(PostgresConfiguration.PostgresServer)}/{PostgresConfiguration.PostgresPort}", callCount = expectedDatastoreCallCount},
new Assertions.ExpectedMetric { metricName = @"Datastore/operation/Postgres/select", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/Postgres/teammembers/select", callCount = 1 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Test()
new Assertions.ExpectedMetric { metricName = @"Datastore/allOther", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/Postgres/all", callCount = expectedDatastoreCallCount },
new Assertions.ExpectedMetric { metricName = @"Datastore/Postgres/allOther", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"DotNet/Npgsql.NpgsqlConnection/Open", callCount = 1},
new Assertions.ExpectedMetric { metricName = @"DotNet/Npgsql.NpgsqlConnection/OpenAsync", callCount = 1},
new Assertions.ExpectedMetric { metricName = $@"Datastore/instance/Postgres/{CommonUtils.NormalizeHostname(PostgresConfiguration.PostgresServer)}/{PostgresConfiguration.PostgresPort}", callCount = expectedDatastoreCallCount},
new Assertions.ExpectedMetric { metricName = @"Datastore/operation/Postgres/select", callCount = 1 },
new Assertions.ExpectedMetric { metricName = @"Datastore/statement/Postgres/teammembers/select", callCount = 1 },
Expand Down