Fix SQL Dependeny parentid to match w3ctracecontext
This commit is contained in:
Родитель
655b716964
Коммит
99b57bfbc2
|
@ -1,5 +1,8 @@
|
|||
# Changelog
|
||||
|
||||
## Version 2.11.1
|
||||
- [Fix Sql dependency parent id to match W3CTraceContext format](https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/1277)
|
||||
|
||||
## Version 2.11.0
|
||||
- [Fix Sql dependency tracking in .NET Core 3.0 which uses Microsoft.Data.SqlClient instead of System.Data.SqlClient](https://github.com/Microsoft/ApplicationInsights-dotnet-server/issues/1263)
|
||||
- Updated Base SDK to 2.11.0
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace Microsoft.ApplicationInsights.Tests
|
|||
using Microsoft.ApplicationInsights.DependencyCollector.Implementation.SqlClientDiagnostics;
|
||||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
|
||||
using Microsoft.ApplicationInsights.W3C.Internal;
|
||||
using Microsoft.ApplicationInsights.Web.TestFramework;
|
||||
using Xunit;
|
||||
|
||||
|
@ -59,8 +60,14 @@ namespace Microsoft.ApplicationInsights.Tests
|
|||
[Theory]
|
||||
[InlineData(SqlClientDiagnosticSourceListener.SqlBeforeExecuteCommand, SqlClientDiagnosticSourceListener.SqlAfterExecuteCommand)]
|
||||
[InlineData(SqlClientDiagnosticSourceListener.SqlMicrosoftBeforeExecuteCommand, SqlClientDiagnosticSourceListener.SqlMicrosoftAfterExecuteCommand)]
|
||||
public void InitializesTelemetryFromParentActivity(string beforeEventName, string afterEventName)
|
||||
public void InitializesTelemetryFromParentActivityNonW3C(string beforeEventName, string afterEventName)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Disable W3C
|
||||
Activity.DefaultIdFormat = ActivityIdFormat.Hierarchical;
|
||||
Activity.ForceDefaultIdFormat = true;
|
||||
|
||||
var activity = new Activity("Current").AddBaggage("Stuff", "123");
|
||||
activity.Start();
|
||||
|
||||
|
@ -97,6 +104,53 @@ namespace Microsoft.ApplicationInsights.Tests
|
|||
Assert.Equal(activity.Id, dependencyTelemetry.Context.Operation.ParentId);
|
||||
Assert.Equal("123", dependencyTelemetry.Properties["Stuff"]);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
|
||||
}
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(SqlClientDiagnosticSourceListener.SqlBeforeExecuteCommand, SqlClientDiagnosticSourceListener.SqlAfterExecuteCommand)]
|
||||
[InlineData(SqlClientDiagnosticSourceListener.SqlMicrosoftBeforeExecuteCommand, SqlClientDiagnosticSourceListener.SqlMicrosoftAfterExecuteCommand)]
|
||||
public void InitializesTelemetryFromParentActivityW3C(string beforeEventName, string afterEventName)
|
||||
{
|
||||
var activity = new Activity("Current").AddBaggage("Stuff", "123");
|
||||
activity.Start();
|
||||
|
||||
var operationId = Guid.NewGuid();
|
||||
var sqlConnection = new SqlConnection(TestConnectionString);
|
||||
var sqlCommand = sqlConnection.CreateCommand();
|
||||
sqlCommand.CommandText = "select * from orders";
|
||||
|
||||
var beforeExecuteEventData = new
|
||||
{
|
||||
OperationId = operationId,
|
||||
Command = sqlCommand,
|
||||
Timestamp = (long?)1000000L
|
||||
};
|
||||
|
||||
this.fakeSqlClientDiagnosticSource.Write(
|
||||
beforeEventName,
|
||||
beforeExecuteEventData);
|
||||
|
||||
var afterExecuteEventData = new
|
||||
{
|
||||
OperationId = operationId,
|
||||
Command = sqlCommand,
|
||||
Timestamp = 2000000L
|
||||
};
|
||||
|
||||
this.fakeSqlClientDiagnosticSource.Write(
|
||||
afterEventName,
|
||||
afterExecuteEventData);
|
||||
|
||||
var dependencyTelemetry = (DependencyTelemetry)this.sendItems.Single();
|
||||
|
||||
Assert.Equal(activity.TraceId.ToHexString(), dependencyTelemetry.Context.Operation.Id);
|
||||
Assert.Equal(W3CUtilities.FormatTelemetryId(activity.TraceId.ToHexString(), activity.SpanId.ToHexString()), dependencyTelemetry.Context.Operation.ParentId);
|
||||
Assert.Equal("123", dependencyTelemetry.Properties["Stuff"]);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(SqlClientDiagnosticSourceListener.SqlBeforeExecuteCommand, SqlClientDiagnosticSourceListener.SqlAfterExecuteCommand)]
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace Microsoft.ApplicationInsights.DependencyCollector.Implementation.SqlCl
|
|||
using Microsoft.ApplicationInsights.Extensibility;
|
||||
using Microsoft.ApplicationInsights.Extensibility.Implementation;
|
||||
using Microsoft.ApplicationInsights.Extensibility.Implementation.Tracing;
|
||||
using Microsoft.ApplicationInsights.W3C.Internal;
|
||||
using static Microsoft.ApplicationInsights.DependencyCollector.Implementation.SqlClientDiagnostics.SqlClientDiagnosticFetcherTypes;
|
||||
|
||||
internal class SqlClientDiagnosticSourceListener : IObserver<KeyValuePair<string, object>>, IDisposable
|
||||
|
@ -342,12 +343,21 @@ namespace Microsoft.ApplicationInsights.DependencyCollector.Implementation.SqlCl
|
|||
|
||||
if (activity != null)
|
||||
{
|
||||
telemetry.Context.Operation.Id = activity.RootId;
|
||||
// SQL Client does NOT create Activity.
|
||||
// We initialize SQL dependency using Activity from incoming Request
|
||||
// and it is the parent of the SQL dependency
|
||||
|
||||
// SQL Client does NOT create and Activity, i.e.
|
||||
// we initialize SQL dependency using request Activity
|
||||
// and it is a parent of the SQL dependency
|
||||
if (activity.IdFormat == ActivityIdFormat.W3C)
|
||||
{
|
||||
var traceId = activity.TraceId.ToHexString();
|
||||
telemetry.Context.Operation.Id = traceId;
|
||||
telemetry.Context.Operation.ParentId = W3CUtilities.FormatTelemetryId(traceId, activity.SpanId.ToHexString());
|
||||
}
|
||||
else
|
||||
{
|
||||
telemetry.Context.Operation.Id = activity.RootId;
|
||||
telemetry.Context.Operation.ParentId = activity.Id;
|
||||
}
|
||||
|
||||
foreach (var item in activity.Baggage)
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче