Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment thread
m-redding marked this conversation as resolved.
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