Skip to content

Commit

Permalink
Set tracestate and sampling decision even if operation id is set (#1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
Liudmila Molkova committed Sep 10, 2019
1 parent 0d8a3fe commit 78dc7e3
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,21 @@ public void InitializeWithActivityRecorded()
currentActivity.Stop();
}

[TestMethod]
public void InitializeWithActivityRecordedOperationIdSet()
{
var currentActivity = new Activity("test");
currentActivity.ActivityTraceFlags |= ActivityTraceFlags.Recorded;
currentActivity.Start();
var request = new RequestTelemetry();
request.Context.Operation.Id = ActivityTraceId.CreateRandom().ToHexString();

(new OperationCorrelationTelemetryInitializer()).Initialize(request);

Assert.AreEqual(SamplingDecision.SampledIn, request.ProactiveSamplingDecision);
currentActivity.Stop();
}

[TestMethod]
public void InitializeWithActivityNotRecorded()
{
Expand Down Expand Up @@ -413,5 +428,22 @@ public void InitializeOnActivityWithTracestateNotOperationTelemetry()
// does not throw
(new OperationCorrelationTelemetryInitializer()).Initialize(telemetry);
}

[TestMethod]
public void InitializeOnActivityWithTracestateAndOperationIdSet()
{
Activity parent = new Activity("parent")
{
TraceStateString = "some=state"
};
parent.Start();

var telemetry = new DependencyTelemetry();
telemetry.Context.Operation.Id = ActivityTraceId.CreateRandom().ToHexString();
(new OperationCorrelationTelemetryInitializer()).Initialize(telemetry);

Assert.IsTrue(telemetry.Properties.ContainsKey("tracestate"));
Assert.AreEqual("some=state", telemetry.Properties["tracestate"]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,65 +29,72 @@ public void Initialize(ITelemetry telemetryItem)
isActivityAvailable = ActivityExtensions.TryRun(() =>
{
var currentActivity = Activity.Current;
if (currentActivity != null && string.IsNullOrEmpty(itemOperationContext.Id))
if (currentActivity != null)
{
if (currentActivity.IdFormat == ActivityIdFormat.W3C)
// we are going to set tracestate property on requests and dependencies only
if (currentActivity.IdFormat == ActivityIdFormat.W3C &&
!string.IsNullOrEmpty(currentActivity.TraceStateString) &&
telemetryItem is OperationTelemetry &&
telemetryProp != null &&
!telemetryProp.Properties.ContainsKey(TracestatePropertyKey))
{
// Set OperationID to Activity.TraceId
// itemOperationContext.Id = currentActivity.RootId; // check if this can be used
itemOperationContext.Id = currentActivity.TraceId.ToHexString();
// Set OperationParentID to ID of parent, constructed as !traceid.spanid.
// ID for auto collected Request,Dependency are constructed as !traceid.spanid, so parentid must be set to the same format.
// While it is possible to set SpanID as the ID for auto collected Request,Dependency we have to stick to this format
// to maintain compatibility. This limitation may go away in the future.
if (string.IsNullOrEmpty(itemOperationContext.ParentId))
{
itemOperationContext.ParentId = W3CUtilities.FormatTelemetryId(itemOperationContext.Id, currentActivity.SpanId.ToHexString());
}
telemetryProp.Properties.Add(TracestatePropertyKey, currentActivity.TraceStateString);
}
// 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);
}
// update proactive sampling decision if Activity is recorded
// sampling processor may change the decision
if (currentActivity.Recorded &&
telemetryItem is ISupportAdvancedSampling supportSamplingTelemetry &&
supportSamplingTelemetry.ProactiveSamplingDecision == SamplingDecision.None)
{
supportSamplingTelemetry.ProactiveSamplingDecision = SamplingDecision.SampledIn;
}
else
if (string.IsNullOrEmpty(itemOperationContext.Id))
{
itemOperationContext.Id = currentActivity.RootId;
if (currentActivity.IdFormat == ActivityIdFormat.W3C)
{
// Set OperationID to Activity.TraceId
// itemOperationContext.Id = currentActivity.RootId; // check if this can be used
itemOperationContext.Id = currentActivity.TraceId.ToHexString();
if (string.IsNullOrEmpty(itemOperationContext.ParentId))
// Set OperationParentID to ID of parent, constructed as !traceid.spanid.
// ID for auto collected Request,Dependency are constructed as !traceid.spanid, so parentid must be set to the same format.
// While it is possible to set SpanID as the ID for auto collected Request,Dependency we have to stick to this format
// to maintain compatibility. This limitation may go away in the future.
if (string.IsNullOrEmpty(itemOperationContext.ParentId))
{
itemOperationContext.ParentId = W3CUtilities.FormatTelemetryId(itemOperationContext.Id,
currentActivity.SpanId.ToHexString());
}
}
else
{
itemOperationContext.ParentId = currentActivity.Id;
itemOperationContext.Id = currentActivity.RootId;
if (string.IsNullOrEmpty(itemOperationContext.ParentId))
{
itemOperationContext.ParentId = currentActivity.Id;
}
}
}
foreach (var baggage in currentActivity.Baggage)
{
if (telemetryProp != null && !telemetryProp.Properties.ContainsKey(baggage.Key))
foreach (var baggage in currentActivity.Baggage)
{
telemetryProp.Properties.Add(baggage);
if (telemetryProp != null && !telemetryProp.Properties.ContainsKey(baggage.Key))
{
telemetryProp.Properties.Add(baggage);
}
}
}
if (string.IsNullOrEmpty(itemOperationContext.Name))
{
string operationName = currentActivity.GetOperationName();
if (!string.IsNullOrEmpty(operationName))
if (string.IsNullOrEmpty(itemOperationContext.Name))
{
itemOperationContext.Name = operationName;
string operationName = currentActivity.GetOperationName();
if (!string.IsNullOrEmpty(operationName))
{
itemOperationContext.Name = operationName;
}
}
}
if (currentActivity.Recorded &&
telemetryItem is ISupportAdvancedSampling supportSamplingTelemetry &&
supportSamplingTelemetry.ProactiveSamplingDecision == SamplingDecision.None)
{
supportSamplingTelemetry.ProactiveSamplingDecision = SamplingDecision.SampledIn;
}
}
});

Expand Down

0 comments on commit 78dc7e3

Please sign in to comment.