diff --git a/CHANGELOG.md b/CHANGELOG.md index 932a09cccc..e85e24b130 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This changelog will be used to generate documentation on [release notes page](ht - [Make BaseSDK use W3C Trace Context based correlation by default. Set TelemetryConfiguration.EnableW3CCorrelation=false to disable this.](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1193) - [Removed TelemetryConfiguration.EnableW3CCorrelation. Users should do Activity.DefaultIdFormat = ActivityIdFormat.Hierarchical; Activity.ForceDefaultIdFormat = true; to disable W3C Format](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1198) - [Enable sampling based on upstream sampling decision for adaptive sampling](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1200) +- [Set tracestate if available on requests and dependencies](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1207) ## Version 2.11.0-beta1 - [Performance fixes: Support Head Sampling; Remove NewGuid(); Sampling Flags; etc... ](https://github.com/microsoft/ApplicationInsights-dotnet/pull/1158) diff --git a/Test/Microsoft.ApplicationInsights.Test/Shared/Extensibility/OperationCorrelationTelemetryInitializerTests.cs b/Test/Microsoft.ApplicationInsights.Test/Shared/Extensibility/OperationCorrelationTelemetryInitializerTests.cs index 33ebbef215..c3ec3e16c1 100644 --- a/Test/Microsoft.ApplicationInsights.Test/Shared/Extensibility/OperationCorrelationTelemetryInitializerTests.cs +++ b/Test/Microsoft.ApplicationInsights.Test/Shared/Extensibility/OperationCorrelationTelemetryInitializerTests.cs @@ -86,7 +86,7 @@ public void InitializePopulatesOperationContextFromActivityWhenW3CIsDisabled() { Activity parent = new Activity("parent"); - // Setting parentid like this forces Activity to use Hierrachial ID Format + // Setting parentid like this forces Activity to use Hierarchical ID Format parent.SetParentId("parent"); parent.Start(); @@ -347,5 +347,71 @@ public void InitializeWithActivityNotRecordedDoesNotOverrideSampledInIfSet() Assert.AreEqual(SamplingDecision.SampledIn, request.ProactiveSamplingDecision); currentActivity.Stop(); } + + [TestMethod] + public void InitializeOnActivityWithTracestate() + { + Activity parent = new Activity("parent") + { + TraceStateString = "some=state" + }; + parent.Start(); + + var telemetry = new DependencyTelemetry(); + (new OperationCorrelationTelemetryInitializer()).Initialize(telemetry); + + Assert.IsTrue(telemetry.Properties.ContainsKey("tracestate")); + Assert.AreEqual("some=state", telemetry.Properties["tracestate"]); + } + + [TestMethod] + public void InitializeOnActivityWithTracestateW3COff() + { + ActivityFormatHelper.DisableW3CFormatInActivity(); + + Activity parent = new Activity("parent") + { + TraceStateString = "some=state" + }; + parent.Start(); + + var telemetry = new DependencyTelemetry(); + (new OperationCorrelationTelemetryInitializer()).Initialize(telemetry); + + Assert.IsFalse(telemetry.Properties.ContainsKey("tracestate")); + } + + [TestMethod] + public void InitializeOnActivityWithTracestateWhenPropertyAlreadyExists() + { + Activity parent = new Activity("parent") + { + TraceStateString = "some=state" + }; + parent.Start(); + + var telemetry = new DependencyTelemetry(); + telemetry.Properties.Add("tracestate", "123"); + (new OperationCorrelationTelemetryInitializer()).Initialize(telemetry); + + Assert.IsTrue(telemetry.Properties.ContainsKey("tracestate")); + Assert.AreEqual("123", telemetry.Properties["tracestate"]); + } + + + [TestMethod] + public void InitializeOnActivityWithTracestateNotOperationTelemetry() + { + Activity parent = new Activity("parent") + { + TraceStateString = "some=state" + }; + parent.Start(); + + var telemetry = new TraceTelemetry(); + + // does not throw + (new OperationCorrelationTelemetryInitializer()).Initialize(telemetry); + } } } \ No newline at end of file diff --git a/src/Microsoft.ApplicationInsights/Extensibility/OperationCorrelationTelemetryInitializer.cs b/src/Microsoft.ApplicationInsights/Extensibility/OperationCorrelationTelemetryInitializer.cs index 6759115e95..89ea56deb5 100644 --- a/src/Microsoft.ApplicationInsights/Extensibility/OperationCorrelationTelemetryInitializer.cs +++ b/src/Microsoft.ApplicationInsights/Extensibility/OperationCorrelationTelemetryInitializer.cs @@ -1,6 +1,5 @@ namespace Microsoft.ApplicationInsights.Extensibility { - using System; using System.Diagnostics; using Microsoft.ApplicationInsights; @@ -15,6 +14,8 @@ /// public class OperationCorrelationTelemetryInitializer : ITelemetryInitializer { + private const string TracestatePropertyKey = "tracestate"; + /// /// Initializes/Adds operation context to the existing telemetry item. /// @@ -44,6 +45,15 @@ public void Initialize(ITelemetry telemetryItem) { itemOperationContext.ParentId = W3CUtilities.FormatTelemetryId(itemOperationContext.Id, currentActivity.SpanId.ToHexString()); } + + // we are going to set tracestate property on requests and dependencies only + if (!string.IsNullOrEmpty(currentActivity.TraceStateString) && + telemetryItem is OperationTelemetry && + telemetryProp != null && + !telemetryProp.Properties.ContainsKey(TracestatePropertyKey)) + { + telemetryProp.Properties.Add(TracestatePropertyKey, currentActivity.TraceStateString); + } } else {