Skip to content
This repository has been archived by the owner on Jul 5, 2020. It is now read-only.

SQL Collection fix #1292

Merged
merged 4 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## Version 2.11.2
- [Fix Sql dependency collection bug in .NET Core 3.0 with Microsoft.Data.SqlClient.](/~https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/1291)

## Version 2.11.1
- [Fix Sql dependency parent id to match W3CTraceContext format](/~https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/1277)
- [Fix EventCounters so that it appear as CustomMetrics as opposed to PerformanceCounters.](/~https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/1280)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,53 @@ public void TracksCommandExecuted(string beforeCommand, string afterCommand)
Assert.True(Math.Abs((start - dependencyTelemetry.Timestamp).TotalMilliseconds) <= 16);
}

[Theory]
[InlineData(SqlClientDiagnosticSourceListener.SqlBeforeExecuteCommand, SqlClientDiagnosticSourceListener.SqlAfterExecuteCommand)]
[InlineData(SqlClientDiagnosticSourceListener.SqlMicrosoftBeforeExecuteCommand, SqlClientDiagnosticSourceListener.SqlMicrosoftAfterExecuteCommand)]
public void TracksCommandExecutedSP(string beforeCommand, string afterCommand)
{
var operationId = Guid.NewGuid();
var sqlConnection = new SqlConnection(TestConnectionString);
var sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = "SP_GetOrders";
sqlCommand.CommandType = CommandType.StoredProcedure;

var beforeExecuteEventData = new
{
OperationId = operationId,
Command = sqlCommand,
Timestamp = (long?)1000000L
};

this.fakeSqlClientDiagnosticSource.Write(
beforeCommand,
beforeExecuteEventData);
var start = DateTimeOffset.UtcNow;

var afterExecuteEventData = new
{
OperationId = operationId,
Command = sqlCommand,
Timestamp = 2000000L
};

this.fakeSqlClientDiagnosticSource.Write(
afterCommand,
afterExecuteEventData);

var dependencyTelemetry = (DependencyTelemetry)this.sendItems.Single();

Assert.Equal(beforeExecuteEventData.OperationId.ToString("N"), dependencyTelemetry.Id);
Assert.Equal(sqlCommand.CommandText, dependencyTelemetry.Data);
Assert.Equal("(localdb)\\MSSQLLocalDB | master | SP_GetOrders", dependencyTelemetry.Name);
Assert.Equal("(localdb)\\MSSQLLocalDB | master", dependencyTelemetry.Target);
Assert.Equal(RemoteDependencyConstants.SQL, dependencyTelemetry.Type);
Assert.True((bool)dependencyTelemetry.Success);
Assert.True(dependencyTelemetry.Duration > TimeSpan.Zero);
Assert.True(dependencyTelemetry.Duration < TimeSpan.FromMilliseconds(500));
Assert.True(Math.Abs((start - dependencyTelemetry.Timestamp).TotalMilliseconds) <= 16);
}

[Theory]
[InlineData(SqlClientDiagnosticSourceListener.SqlBeforeExecuteCommand, SqlClientDiagnosticSourceListener.SqlAfterExecuteCommand)]
[InlineData(SqlClientDiagnosticSourceListener.SqlMicrosoftBeforeExecuteCommand, SqlClientDiagnosticSourceListener.SqlMicrosoftAfterExecuteCommand)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal static class SqlClientDiagnosticFetcherTypes
//// http://github.com/dotnet/corefx/blob/master/src/System.Data.SqlClient/src/System/Data/SqlClient/SqlClientDiagnosticListenerExtensions.cs
//// /~https://github.com/dotnet/SqlClient/blob/master/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlClientDiagnosticListenerExtensions.cs

#region "System.Data fetchers"
#region "System.Data fetchers"

/// <summary> Fetchers for execute command before event. </summary>
internal static class CommandBefore
Expand Down Expand Up @@ -41,6 +41,7 @@ internal static class CommandError
public static readonly PropertyFetcher Command = new PropertyFetcher(nameof(Command));
public static readonly PropertyFetcher Exception = new PropertyFetcher(nameof(Exception));
public static readonly PropertyFetcher Timestamp = new PropertyFetcher(nameof(Timestamp));
public static readonly PropertyFetcher Number = new PropertyFetcher(nameof(Number));
}

/// <summary> Fetchers for connection open/close before events. </summary>
Expand Down Expand Up @@ -68,6 +69,7 @@ internal static class ConnectionError
public static readonly PropertyFetcher Connection = new PropertyFetcher(nameof(Connection));
public static readonly PropertyFetcher Exception = new PropertyFetcher(nameof(Exception));
public static readonly PropertyFetcher Timestamp = new PropertyFetcher(nameof(Timestamp));
public static readonly PropertyFetcher Number = new PropertyFetcher(nameof(Number));
}

/// <summary> Fetchers for transaction commit before events. </summary>
Expand Down Expand Up @@ -118,6 +120,7 @@ internal static class TransactionRollbackError
public static readonly PropertyFetcher Connection = new PropertyFetcher(nameof(Connection));
public static readonly PropertyFetcher Exception = new PropertyFetcher(nameof(Exception));
public static readonly PropertyFetcher Timestamp = new PropertyFetcher(nameof(Timestamp));
public static readonly PropertyFetcher Number = new PropertyFetcher(nameof(Number));
}

/// <summary> Fetchers for transaction commit error events. </summary>
Expand All @@ -127,9 +130,10 @@ internal static class TransactionCommitError
public static readonly PropertyFetcher Connection = new PropertyFetcher(nameof(Connection));
public static readonly PropertyFetcher Exception = new PropertyFetcher(nameof(Exception));
public static readonly PropertyFetcher Timestamp = new PropertyFetcher(nameof(Timestamp));
public static readonly PropertyFetcher Number = new PropertyFetcher(nameof(Number));
}
#endregion

#region "Microsoft.Data fetchers"

/// <summary> Fetchers for execute command before event. </summary>
Expand Down Expand Up @@ -160,6 +164,7 @@ internal static class CommandErrorMicrosoft
public static readonly PropertyFetcher Command = new PropertyFetcher(nameof(Command));
public static readonly PropertyFetcher Exception = new PropertyFetcher(nameof(Exception));
public static readonly PropertyFetcher Timestamp = new PropertyFetcher(nameof(Timestamp));
public static readonly PropertyFetcher Number = new PropertyFetcher(nameof(Number));
}

/// <summary> Fetchers for connection open/close before events. </summary>
Expand Down Expand Up @@ -187,6 +192,7 @@ internal static class ConnectionErrorMicrosoft
public static readonly PropertyFetcher Connection = new PropertyFetcher(nameof(Connection));
public static readonly PropertyFetcher Exception = new PropertyFetcher(nameof(Exception));
public static readonly PropertyFetcher Timestamp = new PropertyFetcher(nameof(Timestamp));
public static readonly PropertyFetcher Number = new PropertyFetcher(nameof(Number));
}

/// <summary> Fetchers for transaction commit before events. </summary>
Expand Down Expand Up @@ -237,6 +243,7 @@ internal static class TransactionRollbackErrorMicrosoft
public static readonly PropertyFetcher Connection = new PropertyFetcher(nameof(Connection));
public static readonly PropertyFetcher Exception = new PropertyFetcher(nameof(Exception));
public static readonly PropertyFetcher Timestamp = new PropertyFetcher(nameof(Timestamp));
public static readonly PropertyFetcher Number = new PropertyFetcher(nameof(Number));
}

/// <summary> Fetchers for transaction commit error events. </summary>
Expand All @@ -246,6 +253,7 @@ internal static class TransactionCommitErrorMicrosoft
public static readonly PropertyFetcher Connection = new PropertyFetcher(nameof(Connection));
public static readonly PropertyFetcher Exception = new PropertyFetcher(nameof(Exception));
public static readonly PropertyFetcher Timestamp = new PropertyFetcher(nameof(Timestamp));
public static readonly PropertyFetcher Number = new PropertyFetcher(nameof(Number));
}
#endregion
}
Expand Down
Loading