From c3163441cd064dd715b9f46a5ea920278b04aca7 Mon Sep 17 00:00:00 2001 From: Alistair Evans Date: Sat, 11 Jul 2020 07:34:53 +0100 Subject: [PATCH] Remove the concept of IsTopLevelOperation; it is no longer required. --- .../Diagnostics/DefaultDiagnosticTracer.cs | 36 +++++++---------- .../Diagnostics/DiagnosticSourceExtensions.cs | 14 +------ .../Core/Diagnostics/DotDiagnosticTracer.cs | 38 +++++++----------- .../Core/Diagnostics/ITracingIdentifer.cs | 40 ------------------- .../Core/Resolving/ResolveOperation.cs | 11 ----- .../Core/Resolving/ResolveOperationBase.cs | 28 +------------ 6 files changed, 31 insertions(+), 136 deletions(-) delete mode 100644 src/Autofac/Core/Diagnostics/ITracingIdentifer.cs diff --git a/src/Autofac/Core/Diagnostics/DefaultDiagnosticTracer.cs b/src/Autofac/Core/Diagnostics/DefaultDiagnosticTracer.cs index 7ea070a16..e64f45897 100644 --- a/src/Autofac/Core/Diagnostics/DefaultDiagnosticTracer.cs +++ b/src/Autofac/Core/Diagnostics/DefaultDiagnosticTracer.cs @@ -23,7 +23,7 @@ public class DefaultDiagnosticTracer : FullOperationDiagnosticTracerBase { private const string RequestExceptionTraced = "__RequestException"; - private readonly ConcurrentDictionary _operationBuilders = new ConcurrentDictionary(); + private readonly ConcurrentDictionary _operationBuilders = new ConcurrentDictionary(); private static readonly string[] NewLineSplit = new[] { Environment.NewLine }; @@ -52,7 +52,7 @@ public override void OnOperationStart(OperationStartDiagnosticData data) return; } - var builder = _operationBuilders.GetOrAdd(data.Operation.TracingId, k => new IndentingStringBuilder()); + var builder = _operationBuilders.GetOrAdd(data.Operation, k => new IndentingStringBuilder()); builder.AppendFormattedLine(TracerMessages.ResolveOperationStarting); builder.AppendLine(TracerMessages.EntryBrace); @@ -67,7 +67,7 @@ public override void OnRequestStart(RequestDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { builder.AppendFormattedLine(TracerMessages.ResolveRequestStarting); builder.AppendLine(TracerMessages.EntryBrace); @@ -93,7 +93,7 @@ public override void OnMiddlewareStart(MiddlewareDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.RequestContext.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.RequestContext.Operation, out var builder)) { builder.AppendFormattedLine(TracerMessages.EnterMiddleware, data.Middleware.ToString()); builder.Indent(); @@ -108,7 +108,7 @@ public override void OnMiddlewareFailure(MiddlewareDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.RequestContext.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.RequestContext.Operation, out var builder)) { builder.Outdent(); builder.AppendFormattedLine(TracerMessages.ExitMiddlewareFailure, data.Middleware.ToString()); @@ -123,7 +123,7 @@ public override void OnMiddlewareSuccess(MiddlewareDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.RequestContext.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.RequestContext.Operation, out var builder)) { builder.Outdent(); builder.AppendFormattedLine(TracerMessages.ExitMiddlewareSuccess, data.Middleware.ToString()); @@ -138,7 +138,7 @@ public override void OnRequestFailure(RequestFailureDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { builder.Outdent(); builder.AppendLine(TracerMessages.ExitBrace); @@ -170,7 +170,7 @@ public override void OnRequestSuccess(RequestDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { builder.Outdent(); builder.AppendLine(TracerMessages.ExitBrace); @@ -186,7 +186,7 @@ public override void OnOperationFailure(OperationFailureDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { try { @@ -194,15 +194,11 @@ public override void OnOperationFailure(OperationFailureDiagnosticData data) builder.AppendLine(TracerMessages.ExitBrace); builder.AppendException(TracerMessages.OperationFailed, data.OperationException); - // If we're completing the root operation, raise the event. - if (data.Operation.IsTopLevelOperation) - { - OnOperationCompleted(new OperationTraceCompletedArgs(data.Operation, builder.ToString())); - } + OnOperationCompleted(new OperationTraceCompletedArgs(data.Operation, builder.ToString())); } finally { - _operationBuilders.TryRemove(data.Operation.TracingId, out var _); + _operationBuilders.TryRemove(data.Operation, out var _); } } } @@ -215,7 +211,7 @@ public override void OnOperationSuccess(OperationSuccessDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { try { @@ -223,15 +219,11 @@ public override void OnOperationSuccess(OperationSuccessDiagnosticData data) builder.AppendLine(TracerMessages.ExitBrace); builder.AppendFormattedLine(TracerMessages.OperationSucceeded, data.ResolvedInstance); - // If we're completing the root operation, raise the event. - if (data.Operation.IsTopLevelOperation) - { - OnOperationCompleted(new OperationTraceCompletedArgs(data.Operation, builder.ToString())); - } + OnOperationCompleted(new OperationTraceCompletedArgs(data.Operation, builder.ToString())); } finally { - _operationBuilders.TryRemove(data.Operation.TracingId, out var _); + _operationBuilders.TryRemove(data.Operation, out var _); } } } diff --git a/src/Autofac/Core/Diagnostics/DiagnosticSourceExtensions.cs b/src/Autofac/Core/Diagnostics/DiagnosticSourceExtensions.cs index c9310b075..bd38c2d49 100644 --- a/src/Autofac/Core/Diagnostics/DiagnosticSourceExtensions.cs +++ b/src/Autofac/Core/Diagnostics/DiagnosticSourceExtensions.cs @@ -1,4 +1,4 @@ -// This software is part of the Autofac IoC container +// This software is part of the Autofac IoC container // Copyright © 2020 Autofac Contributors // https://autofac.org // @@ -104,10 +104,6 @@ public static bool OperationDiagnosticsEnabled(this DiagnosticSource diagnosticS /// The diagnostic source to which events will be written. /// The pipeline resolve operation that is about to run. /// The request that is responsible for starting this operation. - /// - /// A single operation can in turn invoke other full operations (as opposed to requests). Check - /// to know if you're looking at the entry operation. - /// public static void OperationStart(this DiagnosticSource diagnosticSource, ResolveOperationBase operation, ResolveRequest initiatingRequest) { if (diagnosticSource.IsEnabled(DiagnosticEventKeys.OperationStart)) @@ -122,10 +118,6 @@ public static void OperationStart(this DiagnosticSource diagnosticSource, Resolv /// The diagnostic source to which events will be written. /// The resolve operation that failed. /// The exception that caused the operation failure. - /// - /// A single operation can in turn invoke other full operations (as opposed to requests). Check - /// to know if you're looking at the entry operation. - /// public static void OperationFailure(this DiagnosticSource diagnosticSource, ResolveOperationBase operation, Exception operationException) { if (diagnosticSource.IsEnabled(DiagnosticEventKeys.OperationFailure)) @@ -140,10 +132,6 @@ public static void OperationFailure(this DiagnosticSource diagnosticSource, Reso /// The diagnostic source to which events will be written. /// The resolve operation that succeeded. /// The resolved instance providing the requested service. - /// - /// A single operation can in turn invoke other full operations (as opposed to requests). Check - /// to know if you're looking at the entry operation. - /// public static void OperationSuccess(this DiagnosticSource diagnosticSource, ResolveOperationBase operation, object resolvedInstance) { if (diagnosticSource.IsEnabled(DiagnosticEventKeys.OperationSuccess)) diff --git a/src/Autofac/Core/Diagnostics/DotDiagnosticTracer.cs b/src/Autofac/Core/Diagnostics/DotDiagnosticTracer.cs index 9d05c3d2f..dea4d4ec3 100644 --- a/src/Autofac/Core/Diagnostics/DotDiagnosticTracer.cs +++ b/src/Autofac/Core/Diagnostics/DotDiagnosticTracer.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; @@ -24,7 +24,7 @@ public class DotDiagnosticTracer : FullOperationDiagnosticTracerBase { private const string RequestExceptionTraced = "__RequestException"; - private readonly ConcurrentDictionary _operationBuilders = new ConcurrentDictionary(); + private readonly ConcurrentDictionary _operationBuilders = new ConcurrentDictionary(); /// /// Initializes a new instance of the class. @@ -51,7 +51,7 @@ public override void OnOperationStart(OperationStartDiagnosticData data) return; } - var builder = _operationBuilders.GetOrAdd(data.Operation.TracingId, k => new DotGraphBuilder()); + var builder = _operationBuilders.GetOrAdd(data.Operation, k => new DotGraphBuilder()); } /// @@ -62,7 +62,7 @@ public override void OnRequestStart(RequestDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { builder.OnRequestStart( data.RequestContext.Service.ToString(), @@ -79,7 +79,7 @@ public override void OnMiddlewareStart(MiddlewareDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.RequestContext.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.RequestContext.Operation, out var builder)) { builder.OnMiddlewareStart(data.Middleware.ToString()); } @@ -93,7 +93,7 @@ public override void OnMiddlewareFailure(MiddlewareDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.RequestContext.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.RequestContext.Operation, out var builder)) { builder.OnMiddlewareFailure(); } @@ -107,7 +107,7 @@ public override void OnMiddlewareSuccess(MiddlewareDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.RequestContext.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.RequestContext.Operation, out var builder)) { builder.OnMiddlewareSuccess(); } @@ -121,7 +121,7 @@ public override void OnRequestFailure(RequestFailureDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { var requestException = data.RequestException; if (requestException is DependencyResolutionException && requestException.InnerException is object) @@ -150,7 +150,7 @@ public override void OnRequestSuccess(RequestDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { builder.OnRequestSuccess(data.RequestContext.Instance?.GetType().ToString()); } @@ -164,21 +164,17 @@ public override void OnOperationFailure(OperationFailureDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { try { builder.OnOperationFailure(data.OperationException); - // If we're completing the root operation, raise the event. - if (data.Operation.IsTopLevelOperation) - { - OnOperationCompleted(new OperationTraceCompletedArgs(data.Operation, builder.ToString())); - } + OnOperationCompleted(new OperationTraceCompletedArgs(data.Operation, builder.ToString())); } finally { - _operationBuilders.TryRemove(data.Operation.TracingId, out var _); + _operationBuilders.TryRemove(data.Operation, out var _); } } } @@ -191,21 +187,17 @@ public override void OnOperationSuccess(OperationSuccessDiagnosticData data) return; } - if (_operationBuilders.TryGetValue(data.Operation.TracingId, out var builder)) + if (_operationBuilders.TryGetValue(data.Operation, out var builder)) { try { builder.OnOperationSuccess(data.ResolvedInstance?.GetType().ToString()); - // If we're completing the root operation, raise the event. - if (data.Operation.IsTopLevelOperation) - { - OnOperationCompleted(new OperationTraceCompletedArgs(data.Operation, builder.ToString())); - } + OnOperationCompleted(new OperationTraceCompletedArgs(data.Operation, builder.ToString())); } finally { - _operationBuilders.TryRemove(data.Operation.TracingId, out var _); + _operationBuilders.TryRemove(data.Operation, out var _); } } } diff --git a/src/Autofac/Core/Diagnostics/ITracingIdentifer.cs b/src/Autofac/Core/Diagnostics/ITracingIdentifer.cs deleted file mode 100644 index e189ccde3..000000000 --- a/src/Autofac/Core/Diagnostics/ITracingIdentifer.cs +++ /dev/null @@ -1,40 +0,0 @@ -// This software is part of the Autofac IoC container -// Copyright © 2020 Autofac Contributors -// https://autofac.org -// -// Permission is hereby granted, free of charge, to any person -// obtaining a copy of this software and associated documentation -// files (the "Software"), to deal in the Software without -// restriction, including without limitation the rights to use, -// copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following -// conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES -// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT -// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR -// OTHER DEALINGS IN THE SOFTWARE. - -using System.Diagnostics.CodeAnalysis; - -namespace Autofac.Core.Diagnostics -{ - /// - /// Marker interface indicating objects that can function as a resolve operation tracing ID. - /// - [SuppressMessage( - "Design", - "CA1040:Avoid empty interfaces", - Justification = "Holding interface assigned to objects that can be used as a key for tracing dictionaries.")] - public interface ITracingIdentifer - { - } -} diff --git a/src/Autofac/Core/Resolving/ResolveOperation.cs b/src/Autofac/Core/Resolving/ResolveOperation.cs index 518145a19..5bdde89c7 100644 --- a/src/Autofac/Core/Resolving/ResolveOperation.cs +++ b/src/Autofac/Core/Resolving/ResolveOperation.cs @@ -45,17 +45,6 @@ public ResolveOperation(ISharingLifetimeScope mostNestedLifetimeScope) { } - /// - /// Initializes a new instance of the class. - /// - /// The most nested scope in which to begin the operation. The operation - /// can move upward to less nested scopes as components with wider sharing scopes are activated. - /// A parent resolve operation, used to maintain tracing between related operations. - public ResolveOperation(ISharingLifetimeScope mostNestedLifetimeScope, ResolveOperationBase parentOperation) - : base(mostNestedLifetimeScope, parentOperation) - { - } - /// /// Execute the complete resolve operation. /// diff --git a/src/Autofac/Core/Resolving/ResolveOperationBase.cs b/src/Autofac/Core/Resolving/ResolveOperationBase.cs index f082377de..93e066d46 100644 --- a/src/Autofac/Core/Resolving/ResolveOperationBase.cs +++ b/src/Autofac/Core/Resolving/ResolveOperationBase.cs @@ -35,7 +35,7 @@ namespace Autofac.Core.Resolving /// /// Defines the base properties and behaviour of a resolve operation. /// - public abstract class ResolveOperationBase : IResolveOperation, ITracingIdentifer + public abstract class ResolveOperationBase : IResolveOperation { private const int SuccessListInitialCapacity = 32; @@ -50,26 +50,10 @@ public abstract class ResolveOperationBase : IResolveOperation, ITracingIdentife /// can move upward to less nested scopes as components with wider sharing scopes are activated. protected ResolveOperationBase(ISharingLifetimeScope mostNestedLifetimeScope) { - TracingId = this; - IsTopLevelOperation = true; CurrentScope = mostNestedLifetimeScope ?? throw new ArgumentNullException(nameof(mostNestedLifetimeScope)); - IsTopLevelOperation = true; DiagnosticSource = mostNestedLifetimeScope.DiagnosticSource; } - /// - /// Initializes a new instance of the class. - /// - /// The most nested scope in which to begin the operation. The operation - /// can move upward to less nested scopes as components with wider sharing scopes are activated. - /// A tracing ID for the operation. - protected ResolveOperationBase(ISharingLifetimeScope mostNestedLifetimeScope, ITracingIdentifer tracingId) - : this(mostNestedLifetimeScope) - { - TracingId = tracingId; - IsTopLevelOperation = false; - } - /// /// Gets the active resolve request. /// @@ -85,11 +69,6 @@ protected ResolveOperationBase(ISharingLifetimeScope mostNestedLifetimeScope, IT /// public IEnumerable InProgressRequests => RequestStack; - /// - /// Gets the tracing identifier for the operation. - /// - public ITracingIdentifer TracingId { get; } - /// /// Gets the for the operation. /// @@ -100,11 +79,6 @@ protected ResolveOperationBase(ISharingLifetimeScope mostNestedLifetimeScope, IT /// public int RequestDepth { get; protected set; } - /// - /// Gets a value indicating whether this operation is a top-level operation (as opposed to one initiated from inside an existing operation). - /// - public bool IsTopLevelOperation { get; } - /// /// Gets or sets the that initiated the operation. Other nested requests may have been issued as a result of this one. ///