Skip to content

Commit

Permalink
Fix: telemetry parent id when using W3C activity format (#2145)
Browse files Browse the repository at this point in the history
* Fix telemetry parent id when using W3C activity format

* Add unit tests
  • Loading branch information
ylabade authored Jan 21, 2021
1 parent 7d23f4b commit 5c999c8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

## VNext

[Fix: telemetry parent id when using W3C activity format in TelemetryDiagnosticSourceListener](https://github.com/microsoft/ApplicationInsights-dotnet/issues/2142)

## Version 2.17.0-beta1
- [Fix: Missing Dependencies when using Microsoft.Data.SqlClient v2.0.0](https://github.com/microsoft/ApplicationInsights-dotnet/issues/2032)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public void TestInitialize()
this.configuration.InstrumentationKey = Guid.NewGuid().ToString();
}

[TestCleanup]
public void TestCleanup()
{
Activity.Current = null;
}

#endregion TestInitiliaze

#region Subscribtion tests
Expand Down Expand Up @@ -248,6 +254,51 @@ public void TelemetryDiagnosticSourceListenerInitializedWithDependencyModule()
}
}

[TestMethod]
public void TelemetryDiagnosticSourceListenerCollectsTelemetryFromRawActivityWithoutParent()
{
var inclusionList = new List<string> { "Test.A" };
using (var listener = new DiagnosticListener("Test.A"))
using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
{
dl.Subscribe();

var activity = new Activity("Test.A.Activity1");

listener.StartActivity(activity, null);
listener.StopActivity(activity, null);

var telemetryItem = this.sentItems.Last() as DependencyTelemetry;

Assert.AreEqual(activity.SpanId.ToHexString(), telemetryItem.Id);
Assert.AreEqual(activity.TraceId.ToHexString(), telemetryItem.Context.Operation.Id);
Assert.AreEqual(null, telemetryItem.Context.Operation.ParentId);
}
}

[TestMethod]
public void TelemetryDiagnosticSourceListenerCollectsTelemetryFromRawActivityWithParent()
{
var inclusionList = new List<string> { "Test.A" };
using (var listener = new DiagnosticListener("Test.A"))
using (var dl = new TelemetryDiagnosticSourceListener(this.configuration, inclusionList))
{
dl.Subscribe();

var parentActivity = new Activity("Parent").Start();
var activity = new Activity("Test.A.Activity1");

listener.StartActivity(activity, null);
listener.StopActivity(activity, null);

var telemetryItem = this.sentItems.Last() as DependencyTelemetry;

Assert.AreEqual(activity.SpanId.ToHexString(), telemetryItem.Id);
Assert.AreEqual(activity.TraceId.ToHexString(), telemetryItem.Context.Operation.Id);
Assert.AreEqual(activity.ParentSpanId.ToHexString(), telemetryItem.Context.Operation.ParentId);
}
}

#endregion Collection tests

#region Custom handlers
Expand Down Expand Up @@ -373,7 +424,7 @@ public void MultiHost_OneListnerThenAnotherTracksTelemetry()

#endregion

private void DoOperation(DiagnosticListener listener, string activityName)
private void DoOperation(DiagnosticListener listener, string activityName)
{
Activity activity = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,23 @@ public void OnEvent(KeyValuePair<string, object> evnt, DiagnosticListener diagno
}

// properly fill dependency telemetry operation context
telemetry.Context.Operation.Id = currentActivity.RootId;
telemetry.Context.Operation.ParentId = currentActivity.ParentId;
if (currentActivity.IdFormat == ActivityIdFormat.W3C)
{
telemetry.Context.Operation.Id = currentActivity.TraceId.ToHexString();
if (currentActivity.ParentSpanId != default)
{
telemetry.Context.Operation.ParentId = currentActivity.ParentSpanId.ToHexString();
}

telemetry.Id = currentActivity.SpanId.ToHexString();
}
else
{
telemetry.Id = currentActivity.Id;
telemetry.Context.Operation.Id = currentActivity.RootId;
telemetry.Context.Operation.ParentId = currentActivity.ParentId;
}

telemetry.Timestamp = currentActivity.StartTimeUtc;

telemetry.Properties["DiagnosticSource"] = diagnosticListener.Name;
Expand Down

0 comments on commit 5c999c8

Please sign in to comment.