Skip to content

Commit

Permalink
Allow diag scopes names without dot (#30177)
Browse files Browse the repository at this point in the history
  • Loading branch information
lmolkova authored Jul 28, 2022
1 parent ecfddfe commit 6b7a014
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
7 changes: 3 additions & 4 deletions sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ internal DiagnosticScope(string scopeName, DiagnosticListener source, object? di
return null;
}

string clientName = ns;
int indexOfDot = name.IndexOf(".", StringComparison.OrdinalIgnoreCase);
if (indexOfDot == -1)
if (indexOfDot != -1)
{
return null;
clientName += "." + name.Substring(0, indexOfDot);
}

string clientName = ns + "." + name.Substring(0, indexOfDot);

return ActivitySources.GetOrAdd(clientName, static n => ActivityExtensions.CreateActivitySource(n));
}

Expand Down
35 changes: 35 additions & 0 deletions sdk/core/Azure.Core/tests/ClientDiagnosticsTests.Net50.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,41 @@ public void StartsActivitySourceActivity()
}
}

[Test]
[NonParallelizable]
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");

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

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

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

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

Assert.AreEqual("ActivityName", activity.DisplayName);
}
finally
{
Activity.DefaultIdFormat = oldDefault;
}
}

[TestCase(null)]
[TestCase(false)]
[TestCase(true)]
Expand Down

0 comments on commit 6b7a014

Please sign in to comment.