From 13e48298dae415ca71e5543b7fec15604d523396 Mon Sep 17 00:00:00 2001 From: Madalyn Redding <66138537+m-redding@users.noreply.github.com> Date: Mon, 11 Dec 2023 15:46:46 -0500 Subject: [PATCH] [Core] Remove netcoreapp2.1 and net5.0 targets (#40635) * update targets * Update Azure.Core.Expressions.DataFactory.csproj * remove netcoreapp2.1 preprocessor directives * update changelog * Update CHANGELOG.md --- .../Azure.Core.Expressions.DataFactory.csproj | 5 - sdk/core/Azure.Core/CHANGELOG.md | 1 + sdk/core/Azure.Core/src/Azure.Core.csproj | 14 +- .../Internal/RequestActivityPolicy.cs | 24 - .../Azure.Core/src/Shared/DiagnosticScope.cs | 624 +----------------- .../src/Shared/DiagnosticScopeFactory.cs | 22 +- 6 files changed, 5 insertions(+), 685 deletions(-) diff --git a/sdk/core/Azure.Core.Expressions.DataFactory/src/Azure.Core.Expressions.DataFactory.csproj b/sdk/core/Azure.Core.Expressions.DataFactory/src/Azure.Core.Expressions.DataFactory.csproj index 790fc3e67355..b6d669ff92af 100644 --- a/sdk/core/Azure.Core.Expressions.DataFactory/src/Azure.Core.Expressions.DataFactory.csproj +++ b/sdk/core/Azure.Core.Expressions.DataFactory/src/Azure.Core.Expressions.DataFactory.csproj @@ -9,11 +9,6 @@ $(RequiredTargetFrameworks) true false - - $(NoWarn);NETSDK1138 diff --git a/sdk/core/Azure.Core/CHANGELOG.md b/sdk/core/Azure.Core/CHANGELOG.md index b28d118051d4..f373e2b6e7f1 100644 --- a/sdk/core/Azure.Core/CHANGELOG.md +++ b/sdk/core/Azure.Core/CHANGELOG.md @@ -9,6 +9,7 @@ ### Bugs Fixed ### Other Changes +- Remove targets for .NET Core 2.1 and .NET 5 since they are out of support. Azure.Core is no longer compatible with .NET Core 2.1 after removal of target. The remaining targets are unchanged. ## 1.36.0 (2023-11-10) diff --git a/sdk/core/Azure.Core/src/Azure.Core.csproj b/sdk/core/Azure.Core/src/Azure.Core.csproj index ae010c15dbbf..3365f2e3004d 100644 --- a/sdk/core/Azure.Core/src/Azure.Core.csproj +++ b/sdk/core/Azure.Core/src/Azure.Core.csproj @@ -1,4 +1,4 @@ - + This is the implementation of the Azure Client Pipeline Microsoft Azure Client Pipeline @@ -8,15 +8,10 @@ Microsoft Azure Client Pipeline enable $(DefineConstants);AZURE_NULLABLE;HAS_INTERNALS_VISIBLE_CORE - $(RequiredTargetFrameworks);net461;net472;netcoreapp2.1;net5.0;net6.0 + $(RequiredTargetFrameworks);net461;net472;net6.0 true true false - - $(NoWarn);NETSDK1138 net461 @@ -34,11 +29,6 @@ - - - - - diff --git a/sdk/core/Azure.Core/src/Pipeline/Internal/RequestActivityPolicy.cs b/sdk/core/Azure.Core/src/Pipeline/Internal/RequestActivityPolicy.cs index 3c2cba9c5137..1e0aab46a717 100644 --- a/sdk/core/Azure.Core/src/Pipeline/Internal/RequestActivityPolicy.cs +++ b/sdk/core/Azure.Core/src/Pipeline/Internal/RequestActivityPolicy.cs @@ -20,11 +20,7 @@ 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) { @@ -135,11 +131,7 @@ private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory(T sourceArgs) { -#if NETCOREAPP2_1 - return new DiagnosticScope("Azure.Core.Http.Request", s_diagnosticSource, sourceArgs, s_activitySource, DiagnosticScope.ActivityKind.Client, false); -#else return new DiagnosticScope("Azure.Core.Http.Request", s_diagnosticSource, sourceArgs, s_activitySource, System.Diagnostics.ActivityKind.Client, false); -#endif } private static ValueTask ProcessNextAsync(HttpMessage message, ReadOnlyMemory pipeline, bool async) @@ -149,20 +141,12 @@ private static ValueTask ProcessNextAsync(HttpMessage message, ReadOnlyMemory _isDistributedTracingEnabled && -#if NETCOREAPP2_1 - (s_diagnosticSource.IsEnabled() || ActivityExtensions.ActivitySourceHasListeners(s_activitySource)); -#else (s_diagnosticSource.IsEnabled() || IsActivitySourceEnabled); -#endif -#if NETCOREAPP2_1 - private bool IsActivitySourceEnabled => _isDistributedTracingEnabled && ActivityExtensions.ActivitySourceHasListeners(s_activitySource); -#else private bool IsActivitySourceEnabled => _isDistributedTracingEnabled && s_activitySource.HasListeners(); -#endif } } diff --git a/sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs b/sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs index 0728952aad4b..01494122acf0 100644 --- a/sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs +++ b/sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs @@ -24,31 +24,18 @@ namespace Azure.Core.Pipeline // https://github.com/open-telemetry/semantic-conventions/blob/v1.23.0 internal const string OpenTelemetrySchemaVersion = "https://opentelemetry.io/schemas/1.23.0"; private static readonly object AzureSdkScopeValue = bool.TrueString; - private readonly ActivityAdapter? _activityAdapter; private readonly bool _suppressNestedClientActivities; [RequiresUnreferencedCode("The diagnosticSourceArgs are used in a call to DiagnosticSource.Write, all necessary properties need to be preserved on the type being passed in using DynamicDependency attributes.")] -#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 -#if NETCOREAPP2_1 - _suppressNestedClientActivities = (kind == ActivityKind.Client || kind == ActivityKind.Internal) ? suppressNestedClientActivities : false; -#else _suppressNestedClientActivities = (kind == ActivityKind.Client || kind == System.Diagnostics.ActivityKind.Internal) ? suppressNestedClientActivities : false; -#endif // outer scope presence is enough to suppress any inner scope, regardless of inner scope configuation. bool hasListeners; -#if NETCOREAPP2_1 - hasListeners = ActivityExtensions.ActivitySourceHasListeners(activitySource); -#else hasListeners = activitySource?.HasListeners() ?? false; -#endif IsEnabled = source.IsEnabled() || hasListeners; if (_suppressNestedClientActivities) @@ -168,40 +155,6 @@ public void Failed(string errorCode) _activityAdapter?.MarkFailed((Exception?)null, errorCode); } -#if NETCOREAPP2_1 - /// - /// Kind describes the relationship between the Activity, its parents, and its children in a Trace. - /// - public enum ActivityKind - { - /// - /// Default value. - /// Indicates that the Activity represents an internal operation within an application, as opposed to an operations with remote parents or children. - /// - Internal = 0, - - /// - /// Server activity represents request incoming from external component. - /// - Server = 1, - - /// - /// Client activity represents outgoing request to the external component. - /// - Client = 2, - - /// - /// Producer activity represents output provided to external components. - /// - Producer = 3, - - /// - /// Consumer activity represents output received from an external component. - /// - Consumer = 4, - } -#endif - private class DiagnosticActivity : Activity { #pragma warning disable 109 // extra new modifier @@ -215,39 +168,23 @@ public DiagnosticActivity(string operationName) : base(operationName) private class ActivityAdapter : IDisposable { -#if NETCOREAPP2_1 - private readonly object? _activitySource; -#else private readonly ActivitySource? _activitySource; -#endif private readonly DiagnosticSource _diagnosticSource; private readonly string _activityName; -#if NETCOREAPP2_1 - private readonly ActivityKind _kind; -#else private readonly System.Diagnostics.ActivityKind _kind; -#endif private readonly object? _diagnosticSourceArgs; private Activity? _currentActivity; private Activity? _sampleOutActivity; -#if NETCOREAPP2_1 - private ICollection>? _tagCollection; -#else private ActivityTagsCollection? _tagCollection; -#endif private DateTimeOffset _startTime; private List? _links; private string? _traceparent; private string? _tracestate; private string? _displayName; -#if NETCOREAPP2_1 - public ActivityAdapter(object? activitySource, DiagnosticSource diagnosticSource, string activityName, ActivityKind kind, object? diagnosticSourceArgs) -#else public ActivityAdapter(ActivitySource? activitySource, DiagnosticSource diagnosticSource, string activityName, System.Diagnostics.ActivityKind kind, object? diagnosticSourceArgs) -#endif { _activitySource = activitySource; _diagnosticSource = diagnosticSource; @@ -262,13 +199,8 @@ public void AddTag(string name, object value) { // Activity is not started yet, add the value to the collection // that is going to be passed to StartActivity -#if NETCOREAPP2_1 - _tagCollection ??= ActivityExtensions.CreateTagsCollection() ?? new List>(); - _tagCollection?.Add(new KeyValuePair(name, value!)); -#else _tagCollection ??= new ActivityTagsCollection(); _tagCollection[name] = value!; -#endif } else { @@ -276,45 +208,17 @@ public void AddTag(string name, object value) } } -#if NETCOREAPP2_1 - private IList? GetActivitySourceLinkCollection() -#else private List? GetActivitySourceLinkCollection() -#endif { if (_links == null) { return null; } -#if NETCOREAPP2_1 - var linkCollection = ActivityExtensions.CreateLinkCollection(); - if (linkCollection == null) - { - return null; - } -#else var linkCollection = new List(); -#endif foreach (var activity in _links) { -#if NETCOREAPP2_1 - ICollection>? linkTagsCollection = ActivityExtensions.CreateTagsCollection(); - if (linkTagsCollection != null) - { - foreach (var tag in activity.Tags) - { - linkTagsCollection.Add(new KeyValuePair(tag.Key, tag.Value!)); - } - } - - var link = ActivityExtensions.CreateActivityLink(activity.ParentId!, activity.GetTraceState(), linkTagsCollection); - if (link != null) - { - linkCollection.Add(link); - } -#else ActivityTagsCollection linkTagsCollection = new(); foreach (var tag in activity.Tags) { @@ -327,7 +231,6 @@ public void AddTag(string name, object value) var link = new ActivityLink(context, linkTagsCollection); linkCollection.Add(link); } -#endif } return linkCollection; @@ -337,13 +240,8 @@ public void AddLink(string traceparent, string? tracestate, IDictionary?)_links ?? Array.Empty(), }; -#if NETCOREAPP2_1 - _currentActivity.SetW3CFormat(); -#else _currentActivity.SetIdFormat(ActivityIdFormat.W3C); -#endif if (_startTime != default) { @@ -434,11 +324,7 @@ public void AddLink(string traceparent, string? tracestate, IDictionary - /// Until we can reference the 5.0 of System.Diagnostics.DiagnosticSource - /// - internal static class ActivityExtensions - { - static ActivityExtensions() - { - ResetFeatureSwitch(); - } - - private static bool SupportsActivitySourceSwitch; - - private static readonly Type? ActivitySourceType = Type.GetType("System.Diagnostics.ActivitySource, System.Diagnostics.DiagnosticSource"); - private static readonly Type? ActivityKindType = Type.GetType("System.Diagnostics.ActivityKind, System.Diagnostics.DiagnosticSource"); - private static readonly Type? ActivityTagsCollectionType = Type.GetType("System.Diagnostics.ActivityTagsCollection, System.Diagnostics.DiagnosticSource"); - private static readonly Type? ActivityLinkType = Type.GetType("System.Diagnostics.ActivityLink, System.Diagnostics.DiagnosticSource"); - private static readonly Type? ActivityContextType = Type.GetType("System.Diagnostics.ActivityContext, System.Diagnostics.DiagnosticSource"); - private static readonly Type? ActivityStatusCodeType = Type.GetType("System.Diagnostics.ActivityStatusCode, System.Diagnostics.DiagnosticSource"); - - private static Action? SetIdFormatMethod; - private static Func? GetTraceStateStringMethod; - private static Action? SetTraceStateStringMethod; - private static Action? SetErrorStatusMethod; - private static Func? GetIdFormatMethod; - private static Func? GetAllDataRequestedMethod; - private static Action? ActivityAddTagMethod; - private static Func>?, IList?, DateTimeOffset, Activity?>? ActivitySourceStartActivityMethod; - private static Func? ActivitySourceHasListenersMethod; - private static Func>?, object?>? CreateActivityLinkMethod; - private static Func>?>? CreateTagsCollectionMethod; - private static Func? GetCustomPropertyMethod; - private static Action? SetCustomPropertyMethod; - private static readonly ParameterExpression ActivityParameter = Expression.Parameter(typeof(Activity)); - private static MethodInfo? TryParseActivityContextMethod; - private static Action? SetDisplayNameMethod; - - public static object? GetCustomProperty(this Activity activity, string propertyName) - { - if (GetCustomPropertyMethod == null) - { - var method = typeof(Activity).GetMethod("GetCustomProperty"); - if (method == null) - { - GetCustomPropertyMethod = (_, _) => null; - } - else - { - var nameParameter = Expression.Parameter(typeof(string)); - - GetCustomPropertyMethod = Expression.Lambda>( - Expression.Convert(Expression.Call(ActivityParameter, method, nameParameter), typeof(object)), - ActivityParameter, nameParameter).Compile(); - } - } - - return GetCustomPropertyMethod(activity, propertyName); - } - - public static void SetCustomProperty(this Activity activity, string propertyName, object propertyValue) - { - if (SetCustomPropertyMethod == null) - { - var method = typeof(Activity).GetMethod("SetCustomProperty"); - if (method == null) - { - SetCustomPropertyMethod = (_, _, _) => { }; - } - else - { - var nameParameter = Expression.Parameter(typeof(string)); - var valueParameter = Expression.Parameter(typeof(object)); - - SetCustomPropertyMethod = Expression.Lambda>( - Expression.Call(ActivityParameter, method, nameParameter, valueParameter), - ActivityParameter, nameParameter, valueParameter).Compile(); - } - } - - SetCustomPropertyMethod(activity, propertyName, propertyValue); - } - - public static void SetW3CFormat(this Activity activity) - { - if (SetIdFormatMethod == null) - { - var method = typeof(Activity).GetMethod("SetIdFormat"); - if (method == null) - { - SetIdFormatMethod = (_, _) => { }; - } - else - { - var idParameter = Expression.Parameter(typeof(int)); - var convertedId = Expression.Convert(idParameter, method.GetParameters()[0].ParameterType); - - SetIdFormatMethod = Expression.Lambda>( - Expression.Call(ActivityParameter, method, convertedId), - ActivityParameter, idParameter).Compile(); - } - } - - SetIdFormatMethod(activity, 2 /* ActivityIdFormat.W3C */); - } - - public static bool IsW3CFormat(this Activity activity) - { - if (GetIdFormatMethod == null) - { - var method = typeof(Activity).GetProperty("IdFormat")?.GetMethod; - if (method == null) - { - GetIdFormatMethod = _ => -1; - } - else - { - GetIdFormatMethod = Expression.Lambda>( - Expression.Convert(Expression.Call(ActivityParameter, method), typeof(int)), - ActivityParameter).Compile(); - } - } - - - int result = GetIdFormatMethod(activity); - - return result == 2 /* ActivityIdFormat.W3C */; - } - - public static string? GetTraceState(this Activity activity) - { - if (GetTraceStateStringMethod == null) - { - var method = typeof(Activity).GetProperty("TraceStateString")?.GetMethod; - if (method == null) - { - GetTraceStateStringMethod = _ => null; - } - else - { - GetTraceStateStringMethod = Expression.Lambda>( - Expression.Call(ActivityParameter, method), - ActivityParameter).Compile(); - } - } - - return GetTraceStateStringMethod(activity); - } - - public static bool GetIsAllDataRequested(this Activity activity) - { - if (GetAllDataRequestedMethod == null) - { - var method = typeof(Activity).GetProperty("IsAllDataRequested")?.GetMethod; - if (method == null) - { - GetAllDataRequestedMethod = _ => true; - } - else - { - GetAllDataRequestedMethod = Expression.Lambda>( - Expression.Call(ActivityParameter, method), - ActivityParameter).Compile(); - } - } - return GetAllDataRequestedMethod(activity); - } - - public static void SetDisplayName(this Activity activity, string displayName) - { - if (displayName != null) - { - if (SetDisplayNameMethod == null) - { - var method = typeof(Activity).GetProperty("DisplayName")?.SetMethod; - if (method == null) - { - SetDisplayNameMethod = (_, _) => { }; - } - else - { - var displayNameParameter = Expression.Parameter(typeof(string)); - var convertedParameter = Expression.Convert(displayNameParameter, method.GetParameters()[0].ParameterType); - SetDisplayNameMethod = Expression.Lambda>( - Expression.Call(ActivityParameter, method, convertedParameter), - ActivityParameter, displayNameParameter).Compile(); - } - } - SetDisplayNameMethod(activity, displayName); - } - } - - public static void SetTraceState(this Activity activity, string? tracestate) - { - if (SetTraceStateStringMethod == null) - { - var method = typeof(Activity).GetProperty("TraceStateString")?.SetMethod; - if (method == null) - { - SetTraceStateStringMethod = (_, _) => { }; - } - else - { - var tracestateParameter = Expression.Parameter(typeof(string)); - var convertedParameter = Expression.Convert(tracestateParameter, method.GetParameters()[0].ParameterType); - SetTraceStateStringMethod = Expression.Lambda>( - Expression.Call(ActivityParameter, method, convertedParameter), - ActivityParameter, tracestateParameter).Compile(); - } - } - - SetTraceStateStringMethod(activity, tracestate); - } - - public static void SetErrorStatus(this Activity activity, string? errorDescription) - { - if (SetErrorStatusMethod == null) - { - if (ActivityStatusCodeType == null) - { - SetErrorStatusMethod = (_, _, _) => { }; - } - else - { - var method = typeof(Activity).GetMethod("SetStatus", BindingFlags.Instance | BindingFlags.Public, null, new Type[] - { - ActivityStatusCodeType, - typeof(string) - }, null); - if (method == null) - { - SetErrorStatusMethod = (_, _, _) => { }; - } - else - { - var methodParameters = method.GetParameters(); - - var statusParameter = Expression.Parameter(typeof(int)); - var descriptionParameter = Expression.Parameter(typeof(string)); - SetErrorStatusMethod = Expression.Lambda>( - Expression.Call(ActivityParameter, method, Expression.Convert(statusParameter, methodParameters[0].ParameterType), descriptionParameter), - ActivityParameter, statusParameter, descriptionParameter).Compile(); - } - } - } - SetErrorStatusMethod(activity, 2 /* Error */, errorDescription); - } - - public static bool SupportsActivitySource() - { - return SupportsActivitySourceSwitch && ActivitySourceType != null; - } - - public static ICollection>? CreateTagsCollection() - { - if (CreateTagsCollectionMethod == null) - { - var ctor = ActivityTagsCollectionType?.GetConstructor(Array.Empty()); - if (ctor == null) - { - CreateTagsCollectionMethod = () => null; - } - else - { - CreateTagsCollectionMethod = Expression.Lambda>?>>( - Expression.New(ctor)).Compile(); - } - } - - return CreateTagsCollectionMethod(); - } - - public static object? CreateActivityLink(string traceparent, string? tracestate, ICollection>? tags) - { - if (ActivityLinkType == null) - { - return null; - } - - if (CreateActivityLinkMethod == null) - { - var parseMethod = ActivityContextType?.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public); - var ctor = ActivityLinkType?.GetConstructor(new[] { ActivityContextType!, ActivityTagsCollectionType! }); - - if (parseMethod == null || - ctor == null || - ActivityTagsCollectionType == null || - ActivityContextType == null) - { - CreateActivityLinkMethod = (_, _, _) => null; - } - else - { - var traceparentParameter = Expression.Parameter(typeof(string)); - var tracestateParameter = Expression.Parameter(typeof(string)); - var tagsParameter = Expression.Parameter(typeof(ICollection>)); - - CreateActivityLinkMethod = Expression.Lambda>?, object?>>( - Expression.TryCatch( - Expression.Convert(Expression.New(ctor, - Expression.Call(parseMethod, traceparentParameter, tracestateParameter), - Expression.Convert(tagsParameter, ActivityTagsCollectionType)), typeof(object)), - Expression.Catch(typeof(Exception), Expression.Default(typeof(object)))), - traceparentParameter, tracestateParameter, tagsParameter).Compile(); - } - } - - return CreateActivityLinkMethod(traceparent, tracestate, tags); - } - - public static bool ActivitySourceHasListeners(object? activitySource) - { - if (!SupportsActivitySource()) - { - return false; - } - - if (activitySource == null) - { - return false; - } - - if (ActivitySourceHasListenersMethod == null) - { - var method = ActivitySourceType?.GetMethod("HasListeners", BindingFlags.Instance | BindingFlags.Public); - if (method == null || - ActivitySourceType == null) - { - ActivitySourceHasListenersMethod = _ => false; - } - else - { - var sourceParameter = Expression.Parameter(typeof(object)); - ActivitySourceHasListenersMethod = Expression.Lambda>( - Expression.Call(Expression.Convert(sourceParameter, ActivitySourceType), method), - sourceParameter).Compile(); - } - } - - return ActivitySourceHasListenersMethod.Invoke(activitySource); - } - - public static Activity? ActivitySourceStartActivity( - object? activitySource, - string activityName, - int kind, - DateTimeOffset startTime, - ICollection>? tags, - IList? links, - string? traceparent, - string? tracestate) - { - if (activitySource == null) - { - return null; - } - - object? activityContext = default; - - if (ActivitySourceStartActivityMethod == null) - { - if (ActivityLinkType == null || - ActivitySourceType == null || - ActivityContextType == null || - ActivityKindType == null) - { - ActivitySourceStartActivityMethod = (_, _, _, _, _, _, _) => null; - } - else - { - var method = ActivitySourceType?.GetMethod("StartActivity", BindingFlags.Instance | BindingFlags.Public, null, new[] - { - typeof(string), - ActivityKindType, - ActivityContextType, - typeof(IEnumerable>), - typeof(IEnumerable<>).MakeGenericType(ActivityLinkType), - typeof(DateTimeOffset) - }, null); - - if (method == null) - { - ActivitySourceStartActivityMethod = (_, _, _, _, _, _, _) => null; - } - else - { - var sourceParameter = Expression.Parameter(typeof(object)); - var nameParameter = Expression.Parameter(typeof(string)); - var kindParameter = Expression.Parameter(typeof(int)); - var contextParameter = Expression.Parameter(typeof(object)); - var startTimeParameter = Expression.Parameter(typeof(DateTimeOffset)); - var tagsParameter = Expression.Parameter(typeof(ICollection>)); - var linksParameter = Expression.Parameter(typeof(IList)); - var methodParameter = method.GetParameters(); - TryParseActivityContextMethod = ActivityContextType.GetMethod("TryParse", BindingFlags.Static | BindingFlags.Public); - - ActivitySourceStartActivityMethod = Expression.Lambda>?, IList?, DateTimeOffset, Activity?>>( - Expression.Call( - Expression.Convert(sourceParameter, method.DeclaringType!), - method, - nameParameter, - Expression.Convert(kindParameter, methodParameter[1].ParameterType), - Expression.Convert(contextParameter, methodParameter[2].ParameterType), - Expression.Convert(tagsParameter, methodParameter[3].ParameterType), - Expression.Convert(linksParameter, methodParameter[4].ParameterType), - Expression.Convert(startTimeParameter, methodParameter[5].ParameterType)), - sourceParameter, nameParameter, kindParameter, contextParameter, tagsParameter, linksParameter, startTimeParameter).Compile(); - } - } - } - - if (ActivityContextType != null && TryParseActivityContextMethod != null) - { - if (traceparent != null) - { - object?[] parameters = new object?[] { traceparent, tracestate, default }; - TryParseActivityContextMethod.Invoke(null, parameters); - activityContext = parameters[2]; - } - else - { - // because ActivityContext is a struct, we need to create a default instance rather than allowing the argument to be null - activityContext = Activator.CreateInstance(ActivityContextType); - } - } - - return ActivitySourceStartActivityMethod.Invoke(activitySource, activityName, kind, activityContext, tags, links, startTime); - } - - public static object? CreateActivitySource(string name) - { - if (ActivitySourceType == null) - { - return null; - } - return Activator.CreateInstance(ActivitySourceType, - name, // name - null // version - ); - } - - public static IList? CreateLinkCollection() - { - if (ActivityLinkType == null) - { - return null; - } - return Activator.CreateInstance(typeof(List<>).MakeGenericType(ActivityLinkType)) as IList; - } - - public static bool TryDispose(this Activity activity) - { - if (activity is IDisposable disposable) - { - disposable.Dispose(); - return true; - } - - return false; - } - - public static void ResetFeatureSwitch() - { - SupportsActivitySourceSwitch = AppContextSwitchHelper.GetConfigValue( - "Azure.Experimental.EnableActivitySource", - "AZURE_EXPERIMENTAL_ENABLE_ACTIVITY_SOURCE"); - } - } -#else #pragma warning disable SA1507 // File can not contain multiple types /// /// Until Activity Source is no longer considered experimental. @@ -1074,5 +453,4 @@ public static void ResetFeatureSwitch() "AZURE_EXPERIMENTAL_ENABLE_ACTIVITY_SOURCE"); } } -#endif } diff --git a/sdk/core/Azure.Core/src/Shared/DiagnosticScopeFactory.cs b/sdk/core/Azure.Core/src/Shared/DiagnosticScopeFactory.cs index d5b3077b644f..df4bac17a017 100644 --- a/sdk/core/Azure.Core/src/Shared/DiagnosticScopeFactory.cs +++ b/sdk/core/Azure.Core/src/Shared/DiagnosticScopeFactory.cs @@ -21,12 +21,7 @@ internal class DiagnosticScopeFactory private readonly DiagnosticListener? _source; private readonly bool _suppressNestedClientActivities; private readonly bool _isStable; - -#if NETCOREAPP2_1 - private static readonly ConcurrentDictionary ActivitySources = new(); -#else private static readonly ConcurrentDictionary ActivitySources = new(); -#endif /// /// Creates diagnostic scope factory. @@ -61,11 +56,7 @@ public DiagnosticScopeFactory(string clientNamespace, string? resourceProviderNa public bool IsActivityEnabled { get; } [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The DiagnosticScope constructor is marked as RequiresUnreferencedCode because of the usage of the diagnosticSourceArgs parameter. Since we are passing in null here we can suppress this warning.")] -#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) { @@ -94,18 +85,11 @@ public DiagnosticScope CreateScope(string name, System.Diagnostics.ActivityKind /// name: BlobClient.DownloadTo /// result Azure.Storage.Blobs.BlobClient /// -#if NETCOREAPP2_1 - private object? GetActivitySource(string ns, string name) -#else private ActivitySource? GetActivitySource(string ns, string name) -#endif { bool enabled = _isStable; -#if NETCOREAPP2_1 - enabled |= ActivityExtensions.SupportsActivitySource(); -#else enabled |= ActivityExtensions.SupportsActivitySource; -#endif + if (!enabled) { return null; @@ -114,11 +98,7 @@ public DiagnosticScope CreateScope(string name, System.Diagnostics.ActivityKind int indexOfDot = name.IndexOf(".", StringComparison.OrdinalIgnoreCase); string clientName = ns + "." + ((indexOfDot < 0) ? name : 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 } } }