Skip to content

Commit

Permalink
Trim W3C ids to parent/span Id (#1498)
Browse files Browse the repository at this point in the history
* Trim W3C ids to match spanid
  • Loading branch information
Liudmila Molkova authored Dec 20, 2019
1 parent e821aab commit e9e9347
Show file tree
Hide file tree
Showing 41 changed files with 306 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void CreatingOperationHolderWithDetachedOriginalActivityRestoresIt()
var operation = new OperationHolder<DependencyTelemetry>(client, new DependencyTelemetry(), originalActivity);

var newActivity = new Activity("new").SetParentId("detached-parent").Start();
operation.Telemetry.Id = $"|{newActivity.TraceId.ToHexString()}.{newActivity.SpanId.ToHexString()}.";
operation.Telemetry.Id = newActivity.SpanId.ToHexString();

operation.Dispose();
Assert.AreEqual(Activity.Current, originalActivity);
Expand All @@ -71,7 +71,7 @@ public void CreatingOperationHolderWithNullOriginalActivityDoesNotRestoreIt()
var operation = new OperationHolder<DependencyTelemetry>(client, new DependencyTelemetry(), null);

var newActivity = new Activity("new").SetParentId("detached-parent").Start();
operation.Telemetry.Id = $"|{newActivity.TraceId.ToHexString()}.{newActivity.SpanId.ToHexString()}.";
operation.Telemetry.Id = newActivity.SpanId.ToHexString();

operation.Dispose();
Assert.IsNull(Activity.Current);
Expand All @@ -87,7 +87,7 @@ public void CreatingOperationHolderWithParentActivityRestoresIt()

// child of original
var newActivity = new Activity("new").Start();
operation.Telemetry.Id = $"|{newActivity.TraceId.ToHexString()}.{newActivity.SpanId.ToHexString()}.";
operation.Telemetry.Id = newActivity.SpanId.ToHexString();
operation.Dispose();
Assert.AreEqual(Activity.Current, originalActivity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public void InitializePopulatesOperationContextFromActivity()

// Validate
Assert.AreEqual(activity.TraceId.ToHexString(), telemetry.Context.Operation.Id, "OperationCorrelationTelemetryInitializer is expected to populate OperationID from Activity");
Assert.AreEqual(W3CUtilities.FormatTelemetryId(activity.TraceId.ToHexString(), activity.SpanId.ToHexString()),
telemetry.Context.Operation.ParentId,
Assert.AreEqual(activity.SpanId.ToHexString(), telemetry.Context.Operation.ParentId,
"OperationCorrelationTelemetryInitializer is expected to populate Operation ParentID as |traceID.SpanId. from Activity");
Assert.AreEqual(originalTelemetryId, telemetry.Id, "OperationCorrelationTelemetryInitializer is not expected to modify Telemetry ID");
activity.Stop();
Expand Down Expand Up @@ -256,7 +255,7 @@ public void InitializeSetsBaggage()
(new OperationCorrelationTelemetryInitializer()).Initialize(telemetry);

Assert.AreEqual(currentActivity.TraceId.ToHexString(), telemetry.Context.Operation.Id);
Assert.AreEqual(W3CUtilities.FormatTelemetryId(currentActivity.TraceId.ToHexString(), currentActivity.SpanId.ToHexString()), telemetry.Context.Operation.ParentId);
Assert.AreEqual(currentActivity.SpanId.ToHexString(), telemetry.Context.Operation.ParentId);
Assert.AreEqual("operation", telemetry.Context.Operation.Name);

Assert.AreEqual(3, telemetry.Properties.Count);
Expand All @@ -278,7 +277,7 @@ public void InitializeWithActivityWinsOverCallContext()
(new OperationCorrelationTelemetryInitializer()).Initialize(telemetry);

Assert.AreEqual(currentActivity.TraceId.ToHexString(), telemetry.Context.Operation.Id);
Assert.AreEqual(W3CUtilities.FormatTelemetryId(currentActivity.TraceId.ToHexString(), currentActivity.SpanId.ToHexString()), telemetry.Context.Operation.ParentId);
Assert.AreEqual(currentActivity.SpanId.ToHexString(), telemetry.Context.Operation.ParentId);

Assert.AreEqual("operation", telemetry.Context.Operation.Name);
Assert.AreEqual(1, telemetry.Properties.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void BasicStartOperationWithActivityInScopeOfUnrelatedActivity()
var request = this.sendItems.Single() as RequestTelemetry;
Assert.IsNotNull(request);
Assert.AreEqual(activity.TraceId.ToHexString(), request.Context.Operation.Id);
Assert.AreEqual($"|{activity.TraceId.ToHexString()}.{activity.SpanId.ToHexString()}.", request.Id);
Assert.AreEqual(activity.SpanId.ToHexString(), request.Id);
Assert.AreEqual("parentId", request.Context.Operation.ParentId);
}

Expand Down Expand Up @@ -203,7 +203,7 @@ public void BasicStartOperationWithStartedActivityInScopeOfUnrelatedActivity()
var request = this.sendItems.Single() as RequestTelemetry;
Assert.IsNotNull(request);
Assert.AreEqual(activity.TraceId.ToHexString(), request.Context.Operation.Id);
Assert.AreEqual($"|{activity.TraceId.ToHexString()}.{activity.SpanId.ToHexString()}.", request.Id);
Assert.AreEqual(activity.SpanId.ToHexString(), request.Id);
}

/// <summary>
Expand Down Expand Up @@ -419,18 +419,13 @@ private async Task ProcessAsync(Activity activity, Activity parentActivity)
private void ValidateTelemetry<T>(T telemetry, Activity activity, bool isW3C = true, string legacyParentId = null) where T : OperationTelemetry
{
Assert.AreEqual(activity.OperationName, telemetry.Name);
Assert.AreEqual(
isW3C
? W3CUtilities.FormatTelemetryId(activity.TraceId.ToHexString(), activity.SpanId.ToHexString())
: activity.Id, telemetry.Id);
Assert.AreEqual(isW3C ? activity.SpanId.ToHexString() : activity.Id, telemetry.Id);

if (isW3C)
{
if (activity.ParentSpanId != default && activity.ParentSpanId.ToHexString() != "0000000000000000")
{
Assert.AreEqual(
W3CUtilities.FormatTelemetryId(activity.TraceId.ToHexString(),
activity.ParentSpanId.ToHexString()), telemetry.Context.Operation.ParentId);
Assert.AreEqual(activity.ParentSpanId.ToHexString(), telemetry.Context.Operation.ParentId);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Microsoft.ApplicationInsights
using System.Diagnostics;

namespace Microsoft.ApplicationInsights
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -48,10 +50,9 @@ public void TestInitialize()
/// Ensure that context being propagated via async/await.
/// </summary>
[TestMethod]
public void ContextPropagatesThroughAsyncAwait()
public async Task ContextPropagatesThroughAsyncAwait()
{
var task = this.TestAsync();
task.Wait();
await this.TestAsync();
}

/// <summary>
Expand All @@ -60,8 +61,11 @@ public void ContextPropagatesThroughAsyncAwait()
/// <returns>Task to await.</returns>
public async Task TestAsync()
{
Activity activity;
using (var op = this.telemetryClient.StartOperation<RequestTelemetry>("request"))
{
activity = Activity.Current;

var id1 = Thread.CurrentThread.ManagedThreadId;
this.telemetryClient.TrackTrace("trace1");

Expand All @@ -75,20 +79,32 @@ public async Task TestAsync()
}

Assert.AreEqual(3, this.sendItems.Count);
var id = ((RequestTelemetry)this.sendItems[this.sendItems.Count - 1]).Id;
Assert.IsFalse(string.IsNullOrEmpty(id));
var requestTelemetry = ((RequestTelemetry) this.sendItems[this.sendItems.Count - 1]);
var requestId = requestTelemetry.Id;
var requestOperationId = requestTelemetry.Context.Operation.Id;
Assert.IsFalse(string.IsNullOrEmpty(requestTelemetry.Id));
Assert.IsFalse(string.IsNullOrEmpty(requestOperationId));

foreach (var item in this.sendItems)
{
if (item is TraceTelemetry)
{
Assert.AreEqual(id, item.Context.Operation.ParentId);
Assert.AreEqual(GetRootOperationId(id), item.Context.Operation.Id);
Assert.AreEqual(requestId, item.Context.Operation.ParentId);
Assert.AreEqual(requestOperationId, item.Context.Operation.Id);
}
else
{
Assert.AreEqual(id, ((RequestTelemetry)item).Id);
Assert.AreEqual(GetRootOperationId(id), item.Context.Operation.Id);
if (activity.IdFormat == ActivityIdFormat.W3C)
{
Assert.AreEqual(activity.TraceId.ToHexString(), requestOperationId);
Assert.AreEqual(activity.SpanId.ToHexString(), requestId);
}
else
{
Assert.AreEqual(activity.RootId, requestOperationId);
Assert.AreEqual(activity.Id, requestId);
}

Assert.IsNull(item.Context.Operation.ParentId);
}
}
Expand All @@ -101,6 +117,7 @@ public async Task TestAsync()
public void ContextPropagatesThroughBeginEnd()
{
var op = this.telemetryClient.StartOperation<RequestTelemetry>("request");
var activity = Activity.Current;
var id1 = Thread.CurrentThread.ManagedThreadId;
int id2 = 0;
this.telemetryClient.TrackTrace("trace1");
Expand All @@ -126,30 +143,35 @@ public void ContextPropagatesThroughBeginEnd()

Assert.AreNotEqual(id1, id2);
Assert.AreEqual(3, this.sendItems.Count);
var id = ((RequestTelemetry)this.sendItems[this.sendItems.Count - 1]).Id;
Assert.IsFalse(string.IsNullOrEmpty(id));
var requestTelemetry = ((RequestTelemetry)this.sendItems[this.sendItems.Count - 1]);
var requestId = requestTelemetry.Id;
var requestOperationId = requestTelemetry.Context.Operation.Id;
Assert.IsFalse(string.IsNullOrEmpty(requestTelemetry.Id));
Assert.IsFalse(string.IsNullOrEmpty(requestOperationId));

foreach (var item in this.sendItems)
{
if (item is TraceTelemetry)
{
Assert.AreEqual(id, item.Context.Operation.ParentId);
Assert.AreEqual(GetRootOperationId(id), item.Context.Operation.Id);
Assert.AreEqual(requestId, item.Context.Operation.ParentId);
Assert.AreEqual(requestOperationId, item.Context.Operation.Id);
}
else
{
Assert.AreEqual(id, ((RequestTelemetry)item).Id);
Assert.AreEqual(GetRootOperationId(id), item.Context.Operation.Id);
Assert.IsNull(item.Context.Operation.ParentId);
if (activity.IdFormat == ActivityIdFormat.W3C)
{
Assert.AreEqual(activity.TraceId.ToHexString(), requestOperationId);
Assert.AreEqual(activity.SpanId.ToHexString(), requestId);
}
else
{
Assert.AreEqual(activity.RootId, requestOperationId);
Assert.AreEqual(activity.Id, requestId);
}

Assert.IsNull(item.Context.Operation.ParentId);
}
}
}

private string GetRootOperationId(string operationId)
{
Assert.IsTrue(operationId.StartsWith("|"));
return operationId.Substring(1, operationId.IndexOf('.') - 1);
}
}
}
Loading

0 comments on commit e9e9347

Please sign in to comment.