Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ private void CreateAndFireCoreScope()
"Azure.Core.Http.Request",
new DiagnosticListener("Azure.Core"),
null,
ActivityExtensions.CreateActivitySource("Azure.Core.Http"),
DiagnosticScope.ActivityKind.Client,
new ActivitySource("Azure.Core.Http"),
ActivityKind.Client,
false);
coreScope.Start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ internal class RequestActivityPolicy : HttpPipelinePolicy
private const string RequestIdHeaderName = "Request-Id";

private static readonly DiagnosticListener s_diagnosticSource = new DiagnosticListener("Azure.Core");
#if NETCOREAPP2_1
private static readonly object? s_activitySource = ActivityExtensions.CreateActivitySource("Azure.Core.Http");
#else
private static readonly ActivitySource s_activitySource = new ActivitySource("Azure.Core.Http");
#endif

public RequestActivityPolicy(bool isDistributedTracingEnabled, string? resourceProviderNamespace, HttpMessageSanitizer httpMessageSanitizer)
{
Expand Down Expand Up @@ -54,7 +58,11 @@ public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePol

private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool async)
{
#if NETCOREAPP2_1
using var scope = new DiagnosticScope("Azure.Core.Http.Request", s_diagnosticSource, message, s_activitySource, DiagnosticScope.ActivityKind.Client, false);
#else
using var scope = new DiagnosticScope("Azure.Core.Http.Request", s_diagnosticSource, message, s_activitySource, System.Diagnostics.ActivityKind.Client, false);
#endif

bool isActivitySourceEnabled = IsActivitySourceEnabled;

Expand Down
6 changes: 6 additions & 0 deletions sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ namespace Azure.Core.Pipeline
private readonly ActivityAdapter? _activityAdapter;
private readonly bool _suppressNestedClientActivities;

#if NETCOREAPP2_1
internal DiagnosticScope(string scopeName, DiagnosticListener source, object? diagnosticSourceArgs, object? activitySource, ActivityKind kind, bool suppressNestedClientActivities)
#else
internal DiagnosticScope(string scopeName, DiagnosticListener source, object? diagnosticSourceArgs, ActivitySource? activitySource, System.Diagnostics.ActivityKind kind, bool suppressNestedClientActivities)
#endif
{
// ActivityKind.Internal and Client both can represent public API calls depending on the SDK
_suppressNestedClientActivities = (kind == ActivityKind.Client || kind == ActivityKind.Internal) ? suppressNestedClientActivities : false;
Expand Down Expand Up @@ -127,6 +131,7 @@ public void Failed(Exception? exception = default)
_activityAdapter?.MarkFailed(exception);
}

#if NETCOREAPP2_1
/// <summary>
/// Kind describes the relationship between the Activity, its parents, and its children in a Trace.
/// </summary>
Expand Down Expand Up @@ -158,6 +163,7 @@ public enum ActivityKind
/// </summary>
Consumer = 4,
}
#endif

private class DiagnosticActivity : Activity
{
Expand Down
16 changes: 16 additions & 0 deletions sdk/core/Azure.Core/src/Shared/DiagnosticScopeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ internal class DiagnosticScopeFactory
private readonly DiagnosticListener? _source;
private readonly bool _suppressNestedClientActivities;

#if NETCOREAPP2_1
private static readonly ConcurrentDictionary<string, object?> ActivitySources = new();
#else
private static readonly ConcurrentDictionary<string, ActivitySource?> ActivitySources = new();
#endif

public DiagnosticScopeFactory(string clientNamespace, string? resourceProviderNamespace, bool isActivityEnabled, bool suppressNestedClientActivities)
{
Expand All @@ -45,7 +49,11 @@ public DiagnosticScopeFactory(string clientNamespace, string? resourceProviderNa

public bool IsActivityEnabled { get; }

#if NETCOREAPP2_1
public DiagnosticScope CreateScope(string name, DiagnosticScope.ActivityKind kind = DiagnosticScope.ActivityKind.Internal)
#else
public DiagnosticScope CreateScope(string name, System.Diagnostics.ActivityKind kind = ActivityKind.Internal)
#endif
{
if (_source == null)
{
Expand Down Expand Up @@ -74,7 +82,11 @@ public DiagnosticScope CreateScope(string name, DiagnosticScope.ActivityKind kin
/// name: BlobClient.DownloadTo
/// result Azure.Storage.Blobs.BlobClient
/// </summary>
#if NETCOREAPP2_1
private static object? GetActivitySource(string ns, string name)
#else
private static ActivitySource? GetActivitySource(string ns, string name)
#endif
{
if (!ActivityExtensions.SupportsActivitySource())
{
Expand All @@ -87,7 +99,11 @@ public DiagnosticScope CreateScope(string name, DiagnosticScope.ActivityKind kin
{
clientName += "." + name.Substring(0, indexOfDot);
}
#if NETCOREAPP2_1
return ActivitySources.GetOrAdd(clientName, static n => ActivityExtensions.CreateActivitySource(n));
#else
return ActivitySources.GetOrAdd(clientName, static n => new ActivitySource(n));
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public MessagingClientDiagnostics(string clientNamespace, string? resourceProvid
/// <returns>The created diagnostic scope containing the common set of messaging attributes that are knowable upon creation.</returns>
public DiagnosticScope CreateScope(
string activityName,
DiagnosticScope.ActivityKind kind,
ActivityKind kind,
MessagingDiagnosticOperation operation = default)
{
DiagnosticScope scope = _scopeFactory.CreateScope(activityName, kind);
Expand Down Expand Up @@ -167,7 +167,7 @@ public void InstrumentMessage(IDictionary<string, object> properties, string act
{
using DiagnosticScope messageScope = CreateScope(
activityName,
DiagnosticScope.ActivityKind.Producer);
ActivityKind.Producer);
messageScope.Start();

Activity? activity = Activity.Current;
Expand Down
28 changes: 14 additions & 14 deletions sdk/core/Azure.Core/tests/ClientDiagnosticsTests.Net50.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,21 @@ public void NestedClientActivitiesConfigurationClientOptions(bool? suppressNeste
CollectionAssert.DoesNotContain(Activity.Current.Tags, new KeyValuePair<string, string>(DiagnosticScope.OpenTelemetrySchemaAttribute, DiagnosticScope.OpenTelemetrySchemaVersion));
}

[TestCase(DiagnosticScope.ActivityKind.Internal, true)]
[TestCase(DiagnosticScope.ActivityKind.Server, false)]
[TestCase(DiagnosticScope.ActivityKind.Client, true)]
[TestCase(DiagnosticScope.ActivityKind.Producer, false)]
[TestCase(DiagnosticScope.ActivityKind.Consumer, false)]
[TestCase(ActivityKind.Internal, true)]
[TestCase(ActivityKind.Server, false)]
[TestCase(ActivityKind.Client, true)]
[TestCase(ActivityKind.Producer, false)]
[TestCase(ActivityKind.Consumer, false)]
[NonParallelizable]
public void NestedClientActivitiesSuppressed(int kind, bool expectSuppression)
{
using var testListener = new TestDiagnosticListener("Azure.Clients");
DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, true);

using DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", (DiagnosticScope.ActivityKind)kind);
using DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", (ActivityKind)kind);
scope.Start();

DiagnosticScope nestedScope = clientDiagnostics.CreateScope("ClientName.NestedActivityName", (DiagnosticScope.ActivityKind)kind);
DiagnosticScope nestedScope = clientDiagnostics.CreateScope("ClientName.NestedActivityName", (ActivityKind)kind);
nestedScope.Start();
if (expectSuppression)
{
Expand All @@ -214,11 +214,11 @@ public void NestedClientActivitiesSuppressed(int kind, bool expectSuppression)
CollectionAssert.DoesNotContain(Activity.Current.Tags, new KeyValuePair<string, string>(DiagnosticScope.OpenTelemetrySchemaAttribute, DiagnosticScope.OpenTelemetrySchemaVersion));
}

[TestCase(DiagnosticScope.ActivityKind.Internal, true)]
[TestCase(DiagnosticScope.ActivityKind.Server, false)]
[TestCase(DiagnosticScope.ActivityKind.Client, true)]
[TestCase(DiagnosticScope.ActivityKind.Producer, false)]
[TestCase(DiagnosticScope.ActivityKind.Consumer, false)]
[TestCase(ActivityKind.Internal, true)]
[TestCase(ActivityKind.Server, false)]
[TestCase(ActivityKind.Client, true)]
[TestCase(ActivityKind.Producer, false)]
[TestCase(ActivityKind.Consumer, false)]
[NonParallelizable]
public void NestedClientActivitiesSuppressionActivitySource(int kind, bool expectSuppression)
{
Expand All @@ -227,10 +227,10 @@ public void NestedClientActivitiesSuppressionActivitySource(int kind, bool expec

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

using DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", (DiagnosticScope.ActivityKind)kind);
using DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", (ActivityKind)kind);
scope.Start();

DiagnosticScope nestedScope = clientDiagnostics.CreateScope("ClientName.NestedActivityName", (DiagnosticScope.ActivityKind)kind);
DiagnosticScope nestedScope = clientDiagnostics.CreateScope("ClientName.NestedActivityName", (ActivityKind)kind);
nestedScope.Start();
if (expectSuppression)
{
Expand Down
18 changes: 9 additions & 9 deletions sdk/core/Azure.Core/tests/ClientDiagnosticsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,22 +305,22 @@ public void CreatesASingleListenerPerNamespace()
Assert.AreEqual(2, testListener.Sources.Count);
}

[TestCase(DiagnosticScope.ActivityKind.Internal)]
[TestCase(DiagnosticScope.ActivityKind.Server)]
[TestCase(DiagnosticScope.ActivityKind.Client)]
[TestCase(DiagnosticScope.ActivityKind.Producer)]
[TestCase(DiagnosticScope.ActivityKind.Consumer)]
[TestCase(ActivityKind.Internal)]
[TestCase(ActivityKind.Server)]
[TestCase(ActivityKind.Client)]
[TestCase(ActivityKind.Producer)]
[TestCase(ActivityKind.Consumer)]
[NonParallelizable]
public void NestedClientActivitiesNotSuppressed(int kind)
{
using var testListener = new TestDiagnosticListener("Azure.Clients");
DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, false);

using DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", (DiagnosticScope.ActivityKind)kind);
using DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", (ActivityKind)kind);
scope.SetDisplayName("Activity Display Name");
scope.Start();

DiagnosticScope nestedScope = clientDiagnostics.CreateScope("ClientName.NestedActivityName", (DiagnosticScope.ActivityKind)kind);
DiagnosticScope nestedScope = clientDiagnostics.CreateScope("ClientName.NestedActivityName", (ActivityKind)kind);
nestedScope.SetDisplayName("Nested Activity Display Name");
nestedScope.Start();

Expand All @@ -339,7 +339,7 @@ public void NestedActivitiesNoSuppressionSameSourceServerClient()
using var testListener = new TestDiagnosticListener("Azure.Clients");

DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, false);
DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", DiagnosticScope.ActivityKind.Server);
DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", ActivityKind.Server);
Assert.IsTrue(scope.IsEnabled);
scope.Start();
Assert.AreEqual("ClientName.ActivityName", Activity.Current.OperationName);
Expand All @@ -365,7 +365,7 @@ public void NestedActivitiesNoSuppressionDifferentSourcesServerClient()
using var testListener = new TestDiagnosticListener("Azure.Clients");
DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, false);
;
DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", DiagnosticScope.ActivityKind.Server);
DiagnosticScope scope = clientDiagnostics.CreateScope("ClientName.ActivityName", ActivityKind.Server);
scope.Start();

using var activityListener2 = new TestDiagnosticListener("Azure.Clients2");
Expand Down
2 changes: 1 addition & 1 deletion sdk/core/Azure.Core/tests/RequestActivityPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public async Task HttpActivityNeverSuppressed()

using var clientListener = new TestActivitySourceListener("Azure.Clients.ClientName");
DiagnosticScopeFactory clientDiagnostics = new DiagnosticScopeFactory("Azure.Clients", "Microsoft.Azure.Core.Cool.Tests", true, true);
using DiagnosticScope outerScope = clientDiagnostics.CreateScope("ClientName.ActivityName", DiagnosticScope.ActivityKind.Internal);
using DiagnosticScope outerScope = clientDiagnostics.CreateScope("ClientName.ActivityName", ActivityKind.Internal);
outerScope.Start();

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
<!-- This is required to prevent version conflict stemming from The CloudNative.CloudEvents.SystemTestJson depending on -->
<!-- version 5.0.0.-->
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" VersionOverride="5.0.0" />
<PackageReference Include="Azure.Core"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -163,7 +164,7 @@ private async Task<HttpResponseMessage> ProcessEventsAsync(JArray events, string

private async Task<FunctionResult> ExecuteWithTracingAsync(string functionName, TriggeredFunctionData triggerData)
{
using DiagnosticScope scope = _diagnosticScopeFactory.CreateScope(DiagnosticScopeName, DiagnosticScope.ActivityKind.Consumer);
using DiagnosticScope scope = _diagnosticScopeFactory.CreateScope(DiagnosticScopeName, ActivityKind.Consumer);
if (scope.IsEnabled)
{
if (triggerData.TriggerValue is JArray evntArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.Azure.WebJobs" />
<PackageReference Include="Microsoft.Extensions.Azure" />
<PackageReference Include="Azure.Messaging.EventGrid" />
<PackageReference Include="Azure.Core" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -890,7 +891,7 @@ protected override Task UpdateCheckpointAsync(string partitionId,

Logger.UpdateCheckpointStart(partitionId, Identifier, EventHubName, ConsumerGroup);

using var scope = ClientDiagnostics.CreateScope(DiagnosticProperty.EventProcessorCheckpointActivityName, DiagnosticScope.ActivityKind.Internal);
using var scope = ClientDiagnostics.CreateScope(DiagnosticProperty.EventProcessorCheckpointActivityName, ActivityKind.Internal);
scope.Start();

try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -622,7 +623,7 @@ internal virtual async Task ProcessEventBatchAsync(TPartition partition,

// Create the diagnostics scope used for distributed tracing and instrument the events in the batch.

using var diagnosticScope = ClientDiagnostics.CreateScope(DiagnosticProperty.EventProcessorProcessingActivityName, DiagnosticScope.ActivityKind.Consumer, MessagingDiagnosticOperation.Process);
using var diagnosticScope = ClientDiagnostics.CreateScope(DiagnosticProperty.EventProcessorProcessingActivityName, ActivityKind.Consumer, MessagingDiagnosticOperation.Process);

if ((diagnosticScope.IsEnabled) && (eventBatch.Any()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -1295,7 +1296,7 @@ private async Task InitializePartitionStateAsync(PartitionPublishingState partit
///
private DiagnosticScope CreateDiagnosticScope(IEnumerable<(string TraceParent, string TraceState)> traceContexts, int eventCount)
{
DiagnosticScope scope = ClientDiagnostics.CreateScope(DiagnosticProperty.ProducerActivityName, DiagnosticScope.ActivityKind.Client, MessagingDiagnosticOperation.Publish);
DiagnosticScope scope = ClientDiagnostics.CreateScope(DiagnosticProperty.ProducerActivityName, ActivityKind.Client, MessagingDiagnosticOperation.Publish);

if (scope.IsEnabled)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.Pipeline;
Expand Down Expand Up @@ -138,7 +139,7 @@ public virtual void UpdatePrefetchCount(int prefetchCount)

protected async Task ProcessOneMessageWithinScopeAsync(ServiceBusReceivedMessage message, string activityName, CancellationToken cancellationToken)
{
using DiagnosticScope scope = _clientDiagnostics.CreateScope(activityName, DiagnosticScope.ActivityKind.Consumer, MessagingDiagnosticOperation.Process);
using DiagnosticScope scope = _clientDiagnostics.CreateScope(activityName, ActivityKind.Consumer, MessagingDiagnosticOperation.Process);
scope.SetMessageAsParent(message);
scope.Start();

Expand Down
Loading