Skip to content

Commit

Permalink
[Core] Add DiagnosticScope test and removing bug workarounds that a…
Browse files Browse the repository at this point in the history
…re no longer needed (#39274)

* new tests

* formatting
  • Loading branch information
m-redding authored Oct 16, 2023
1 parent 416a0fa commit 6c418fb
Showing 1 changed file with 55 additions and 65 deletions.
120 changes: 55 additions & 65 deletions sdk/core/Azure.Core/tests/ClientDiagnosticsTests.Net50.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ private static TestAppContextSwitch SetAppConfigSwitch()
return s;
}

[Test]
[NonParallelizable]
public void StartsActivityNoOpsWithoutSwitchWithListener()
{
using var activityListener = new TestActivitySourceListener("Azure.Clients.ClientName");
DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, false);
DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName");

Assert.IsFalse(scope.IsEnabled);

scope.Start();
scope.Dispose();

Assert.AreEqual(0, activityListener.Activities.Count);
}

[Test]
[NonParallelizable]
public void StartActivityNoOpsWithoutSwitch()
Expand All @@ -52,62 +68,49 @@ public void StartsActivitySourceActivity()
{
using var _ = SetAppConfigSwitch();

// Bug: there is no way to set activity type to W3C
// https://github.com/dotnet/runtime/issues/43853
var oldDefault = Activity.DefaultIdFormat;

Activity.DefaultIdFormat = ActivityIdFormat.W3C;

try
{
using var activityListener = new TestActivitySourceListener("Azure.Clients.ClientName");
using var activityListener = new TestActivitySourceListener("Azure.Clients.ClientName");

DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, false);
DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, false);

DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName");
DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName");

scope.SetDisplayName("custom display name");
scope.SetDisplayName("custom display name");

scope.AddAttribute("Attribute1", "Value1");
scope.AddAttribute("Attribute2", 2, i => i.ToString());
scope.AddAttribute("Attribute3", 3);
scope.AddAttribute("Attribute1", "Value1");
scope.AddAttribute("Attribute2", 2, i => i.ToString());
scope.AddAttribute("Attribute3", 3);

scope.AddLink("00-6e76af18746bae4eadc3581338bbe8b1-2899ebfdbdce904b-00", "foo=bar");
scope.AddLink("00-6e76af18746bae4eadc3581338bbe8b2-2899ebfdbdce904b-00", null, new Dictionary<string, string>()
{
{"linkAttribute", "linkAttributeValue"}
});
scope.AddLink("00-6e76af18746bae4eadc3581338bbe8b1-2899ebfdbdce904b-00", "foo=bar");
scope.AddLink("00-6e76af18746bae4eadc3581338bbe8b2-2899ebfdbdce904b-00", null, new Dictionary<string, string>()
{
{"linkAttribute", "linkAttributeValue"}
});

Assert.IsTrue(scope.IsEnabled);
Assert.IsTrue(scope.IsEnabled);

scope.Start();
scope.Start();

// Validate that the default activity kind is used
Assert.AreEqual(ActivityKind.Internal, Activity.Current.Kind);
Assert.AreEqual("custom display name", Activity.Current.DisplayName);
// Validate that the default activity kind is used
Assert.AreEqual(ActivityKind.Internal, Activity.Current.Kind);
Assert.AreEqual("custom display name", Activity.Current.DisplayName);

scope.Dispose();
scope.Dispose();

Assert.AreEqual(1, activityListener.Activities.Count);
var activity = activityListener.Activities.Dequeue();
Assert.AreEqual(1, activityListener.Activities.Count);
var activity = activityListener.Activities.Dequeue();

Assert.AreEqual("Value1", activity.TagObjects.Single(o => o.Key == "Attribute1").Value);
Assert.AreEqual("2", activity.TagObjects.Single(o => o.Key == "Attribute2").Value);
Assert.AreEqual("3", activity.TagObjects.Single(o => o.Key == "Attribute3").Value);
Assert.AreEqual("Value1", activity.TagObjects.Single(o => o.Key == "Attribute1").Value);
Assert.AreEqual("2", activity.TagObjects.Single(o => o.Key == "Attribute2").Value);
Assert.AreEqual("3", activity.TagObjects.Single(o => o.Key == "Attribute3").Value);

var links = activity.Links.ToArray();
Assert.AreEqual(2, links.Length);
Assert.AreEqual(ActivityContext.Parse("00-6e76af18746bae4eadc3581338bbe8b1-2899ebfdbdce904b-00", "foo=bar"), links[0].Context);
Assert.AreEqual(ActivityContext.Parse("00-6e76af18746bae4eadc3581338bbe8b2-2899ebfdbdce904b-00", null), links[1].Context);
var links = activity.Links.ToArray();
Assert.AreEqual(2, links.Length);
Assert.AreEqual(ActivityContext.Parse("00-6e76af18746bae4eadc3581338bbe8b1-2899ebfdbdce904b-00", "foo=bar"), links[0].Context);
Assert.AreEqual(ActivityContext.Parse("00-6e76af18746bae4eadc3581338bbe8b2-2899ebfdbdce904b-00", null), links[1].Context);

Assert.AreEqual(ActivityIdFormat.W3C, activity.IdFormat);
Assert.AreEqual(ActivityIdFormat.W3C, activity.IdFormat);

CollectionAssert.Contains(activity.Tags, new KeyValuePair<string, string>(DiagnosticScope.OpenTelemetrySchemaAttribute, DiagnosticScope.OpenTelemetrySchemaVersion));
}
finally
{
Activity.DefaultIdFormat = oldDefault;
}
CollectionAssert.Contains(activity.Tags, new KeyValuePair<string, string>(DiagnosticScope.OpenTelemetrySchemaAttribute, DiagnosticScope.OpenTelemetrySchemaVersion));
}

[Test]
Expand All @@ -116,34 +119,21 @@ public void StartsActivityShortNameSourceActivity()
{
using var _ = SetAppConfigSwitch();

// Bug: there is no way to set activity type to W3C
// https://github.com/dotnet/runtime/issues/43853
var oldDefault = Activity.DefaultIdFormat;

Activity.DefaultIdFormat = ActivityIdFormat.W3C;

try
{
using var activityListener = new TestActivitySourceListener("Azure.Clients.ClientName");
using var activityListener = new TestActivitySourceListener("Azure.Clients.ClientName");

DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients.ClientName", "Microsoft.Azure.Core.Cool.Tests", true, false);
DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients.ClientName", "Microsoft.Azure.Core.Cool.Tests", true, false);

DiagnosticScope scope = clientDiagnostics.CreateScope("ActivityName");
Assert.IsTrue(scope.IsEnabled);
DiagnosticScope scope = clientDiagnostics.CreateScope("ActivityName");
Assert.IsTrue(scope.IsEnabled);

scope.Start();
scope.Dispose();
scope.Start();
scope.Dispose();

Assert.AreEqual(1, activityListener.Activities.Count);
var activity = activityListener.Activities.Dequeue();
Assert.AreEqual(1, activityListener.Activities.Count);
var activity = activityListener.Activities.Dequeue();

Assert.AreEqual("ActivityName", activity.DisplayName);
CollectionAssert.Contains(activity.Tags, new KeyValuePair<string, string>(DiagnosticScope.OpenTelemetrySchemaAttribute, DiagnosticScope.OpenTelemetrySchemaVersion));
}
finally
{
Activity.DefaultIdFormat = oldDefault;
}
Assert.AreEqual("ActivityName", activity.DisplayName);
CollectionAssert.Contains(activity.Tags, new KeyValuePair<string, string>(DiagnosticScope.OpenTelemetrySchemaAttribute, DiagnosticScope.OpenTelemetrySchemaVersion));
}

[TestCase(null)]
Expand Down

0 comments on commit 6c418fb

Please sign in to comment.