Skip to content

Commit

Permalink
Set tracestate if available on requests and dependencies (#1207)
Browse files Browse the repository at this point in the history
* Set tracestate if available on requests and dependencies

* changelo
  • Loading branch information
Liudmila Molkova committed Sep 9, 2019
1 parent c46ebc6 commit d00d98f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Microsoft.ApplicationInsights.Extensibility
{
using System;
using System.Diagnostics;

using Microsoft.ApplicationInsights;
Expand All @@ -15,6 +14,8 @@
/// </summary>
public class OperationCorrelationTelemetryInitializer : ITelemetryInitializer
{
private const string TracestatePropertyKey = "tracestate";

/// <summary>
/// Initializes/Adds operation context to the existing telemetry item.
/// </summary>
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit d00d98f

Please sign in to comment.